FileDocCategorySizeDatePackage
Portfolio.javaAPI DocExample5467Sat Jan 24 10:44:30 GMT 2004je3.i18n

Portfolio

public class Portfolio extends Object
A partial implementation of a hypothetical stock portfolio class. We use it only to demonstrate number and date internationalization.

Fields Summary
EquityPosition[]
positions
Date
lastQuoteTime
Constructors Summary
public Portfolio(EquityPosition[] positions, Date lastQuoteTime)

   // Time for current quotes

    // Create a Portfolio
         
	this.positions = positions;
	this.lastQuoteTime = lastQuoteTime;
    
Methods Summary
public static voidmain(java.lang.String[] args)
This is a test program that demonstrates the class

	Currency dollars = Currency.getInstance("USD");
	Currency pounds = Currency.getInstance("GBP");
	Currency euros = Currency.getInstance("EUR");
	Currency yen = Currency.getInstance("JPY");

	// This is the portfolio to display.
	EquityPosition[] positions = new EquityPosition[] {
	    new EquityPosition("WWW", 400, "2003-01-03", dollars, 11.90,13.00),
	    new EquityPosition("XXX", 1100, "2003-02-02", pounds, 71.09,27.25),
	    new EquityPosition("YYY", 6000, "2003-04-17", euros, 23.37,89.12),
	    new EquityPosition("ZZZ", 100, "2003-8-10", yen, 100000,121345)
	};

	// Create the portfolio from these positions
	Portfolio portfolio = new Portfolio(positions, new Date());

        // Set the default locale using the language code and country code
	// specified on the command line.
        if (args.length == 2) Locale.setDefault(new Locale(args[0], args[1]));

	// Now display the portfolio.
	// We use a Swing dialog box to display it because the console may
	// not be able to display non-ASCII characters like currency symbols
	// for Pounds, Euros, and Yen.
	javax.swing.JOptionPane.showMessageDialog(null, portfolio,
			       Locale.getDefault().getDisplayName(),
			       javax.swing.JOptionPane.INFORMATION_MESSAGE);

	// The modal dialog starts another thread running, so we have to exit
	// explictly when the user dismisses it.
	System.exit(0);
    
public java.lang.StringtoString()

	StringBuffer b = new StringBuffer();

        // Obtain NumberFormat and DateFormat objects to format our data.
        NumberFormat number = NumberFormat.getInstance();
        NumberFormat price = NumberFormat.getCurrencyInstance();
        NumberFormat percent = NumberFormat.getPercentInstance();
        DateFormat shortdate = DateFormat.getDateInstance(DateFormat.MEDIUM);
        DateFormat fulldate = DateFormat.getDateTimeInstance(DateFormat.LONG,
							     DateFormat.LONG);


        // Print some introductory data.
	b.append("<html><body>");
        b.append("<i>Portfolio value at ").
	    append(fulldate.format(lastQuoteTime)).append("</i>");
	b.append("<table border=1>");
        b.append("<tr><th>Symbol<th>Shares<th>Purchased<th>At<th>" +
		 "Quote<th>Change</tr>");
	
        // Display the table using the format() methods of the Format objects.
        for(int i = 0; i < positions.length; i++) {
	    b.append("<tr><td>");
            b.append(positions[i].name).append("<td>");
            b.append(number.format(positions[i].shares)).append("<td>");
            b.append(shortdate.format(positions[i].purchased)).append("<td>");
	    // Set the currency to use when printing the following prices
	    price.setCurrency(positions[i].currency);
            b.append(price.format(positions[i].bought)).append("<td>");
            b.append(price.format(positions[i].current)).append("<td>");
            double change =
                (positions[i].current-positions[i].bought)/positions[i].bought;
            b.append(percent.format(change)).append("</tr>");
        }
	b.append("</table></body></html>");
	return b.toString();