import java.text.NumberFormat; /* * This class represents a single company that is part of the * Dow Jones Industrial Average. Note that there are exacly 30 * companies on the DOW. For a list of all the companies on the DOW, * see http://www.djindexes.com/mdsidx/index.cfm?event=showAverages * * NOTE: DO NOT CHANGE THIS FILE */ public class DJIACompany implements Comparable { // COMPANY NAME private String name; // TICKER SYMBOL private String symbol; // STOCK PRICE private double pricePerShare; public DJIACompany( String initName, String initSymbol, double initPricePerShare) { name = initName; symbol = initSymbol; pricePerShare = initPricePerShare; } public String toString() { NumberFormat nf = NumberFormat.getCurrencyInstance(); String symbolColumn = symbol; while (symbolColumn.length() < 5) symbolColumn = " " + symbolColumn; String priceColumn = nf.format(pricePerShare); while (priceColumn.length() < 10) priceColumn = " " + priceColumn; return symbolColumn + priceColumn; } // ACCESSOR METHODS public String getName() { return name; } public String getSymbol() { return symbol; } public double getPricePerShare() { return pricePerShare; } // MUTATOR METHODS public void setName(String initName) { name = initName; } public void setSymbol(String initSymbol) { symbol = initSymbol; } public void setPricePerShare(double initPricePerShare) { pricePerShare = initPricePerShare; } /* * We only want to compare companies by their ticker symbol * for the purpose of keeping them sorted. */ public int compareTo(Object obj) { DJIACompany otherStock = (DJIACompany)obj; return symbol.compareTo(otherStock.symbol); } }