import york.*;
import cosc1020.*;
import java.util.StringTokenizer;

public class A1
{   public static void main(String[] args)
    {   York.print("Enter the name of the portfolio file ... ");
        String pfFile = York.readLine();
        YorkReader yr = new YorkReader(pfFile);
        
        double curTotal = 0;
                
        Portfolio pf = new Portfolio();
        
        for (String inputLine = yr.readLine(); !yr.eof(); inputLine = yr.readLine())              
        {   StringTokenizer tokenizer = new StringTokenizer(inputLine);
            while (tokenizer.hasMoreTokens())
            {   Stock s = new Stock(tokenizer.nextToken());
                int qty = Integer.parseInt(tokenizer.nextToken());
                double priceNow = s.getPrice();
                
                if (qty > 0 && s.getName() != null)
                {   Investment inv = new Investment(s, qty, priceNow);
                    pf.add(inv);
                
                    curTotal = curTotal + qty * priceNow;
                }
            }
        }        
        yr.close();

        final int SYMBOL_WIDTH = 9; 
        final int PERCENTAGE_WIDTH = 2; 
        final int PERCENTS_VALUE = 100;
        final String CURENCY_FORMAT = ".2,";
        
        char dot = '.';
        York.fillChar = dot;  
         
        int count = pf.getCount();
        
        York.print("Portfolio count = " + count + "; value = ");
        York.print(curTotal, CURENCY_FORMAT);
        York.println("; holdings:");      
          
        for (int i = 0; i < count; i++)
        {   Investment inv = pf.getInvestment(i);
            String symbol = inv.getStock().getSymbol();
            double percentage = Math.round((inv.getStock().getPrice() * inv.getQty()/curTotal) * PERCENTS_VALUE);
               
            York.print((symbol + York.repeat(SYMBOL_WIDTH,'.')).substring(0,SYMBOL_WIDTH));
            York.print(percentage, "" + PERCENTAGE_WIDTH);
            York.println("%");
        }
    }
}
