/* * Copyright (c) 2001 Matthew Feldt. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided the copyright notice above is * retained. * * THIS SOFTWARE IS PROVIDED ''AS IS'' AND WITHOUT ANY EXPRESSED OR * IMPLIED WARRANTIES. */ /** * Portfolio.java * * Java Examples In A Nutshell Copyright (c) 2000 David Flanagan * Exercise 7-2: * Modify the Portfolio class of Example 7-3 to remove all hardcoded display * strings. Instead, use the ResourceBundle and MessageFormat classes as * demonstrated in Example 7-4 and 7-5. * * @author Matthew Feldt * @version 1.0 */ package com.feldt.examples.i18n; import java.text.*; import java.util.*; import java.io.*; public class Portfolio { EquityPosition[] positions; Date lastQuoteTime = new Date(); ResourceBundle bundle; public Portfolio(EquityPosition[] pos, Date lastQuote, ResourceBundle rb) { positions = pos; lastQuoteTime = lastQuote; bundle = rb; } public void print(PrintWriter out) { // create NumberFormat and DateFormat objects to format 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); out.println(bundle.getString("heading.date.text") + " " + fulldate.format(lastQuoteTime) + ":"); out.println(bundle.getString("heading.symbol.text") + "\t" + bundle.getString("heading.shares.text") + "\t" + bundle.getString("heading.purchased.text") + "\t" + bundle.getString("heading.at.text") + "\t" + bundle.getString("heading.quote.text") +"\t" + bundle.getString("heading.change.text")); // display table using the format() methods from the Format objects for(int i = 0; i < positions.length; i++) { out.print(positions[i].name + "\t"); out.print(number.format(positions[i].shares) + "\t"); out.print(shortdate.format(positions[i].purchased) + "\t"); out.print(price.format(positions[i].bought) + "\t"); out.print(price.format(positions[i].current) + "\t"); double change = (positions[i].current-positions[i].bought)/positions[i].bought; out.println(percent.format(change)); out.flush(); } } static class EquityPosition { String name; // stock name int shares; // shares held Date purchased; // purchase date double bought; // purchase price per share double current; // current price per share EquityPosition(String n, int s, Date when, double then, double now) { name = n; shares = s; purchased = when; bought = then; current = now; } } /** * This is a test program that demonstrates the class **/ public static void main(String[] args) { final String RESOURCES = "portfolio"; EquityPosition[] positions = new EquityPosition[] { new EquityPosition("XXX", 400, new GregorianCalendar(2001, 1, 3).getTime(), 11.90, 13.00), new EquityPosition("YYY", 1100, new GregorianCalendar(2001, 2, 2).getTime(), 71.09,27.25), new EquityPosition("ZZZ", 6000, new GregorianCalendar(2001, 4, 17).getTime(), 23.37, 89.12) }; Locale locale; // parse the command line for locale if specified if (args.length == 2) locale = new Locale(args[0], args[1]); else locale = Locale.getDefault(); // fetch the appropriate resource bundle ResourceBundle bundle = ResourceBundle.getBundle(RESOURCES, locale); // Create the portfolio from these positions Portfolio portfolio = new Portfolio(positions, new Date(), bundle); // Now print the portfolio portfolio.print(new PrintWriter(System.out)); } }