// File: PurchaseListTester.java (requires Java 1.5 or newer) // The Purchase class: each object has the number of items purchased, // a one-word description of the item, and the price of the item. Note: // Purchase class depends on Align class to format output. // The PurchaseList class: maintains a list of Purchases (using an ArrayList) // The test class creates a PurchaseList object and calls the readData() // method which reads data for any number of purchases until eof. Each line // read is scanned and a Purchase object is created from the tokens and added // to the list. At end-of-file, the list is printed along with the total cost // of all purchases. Note: Test class depends on Align class to format output. import java.util.* ; // for ArrayList and Scanner /** * A class to represent a purchase - number of items, description, and price * per item */ class Purchase { // instance vars private int quantity ; // number of each item purchased private String item ; // description of the item private double price ; // price of the item /** * Creates a purchase object * @param quantity number of items bought * @param item one-word description of item * @param price cost per item */ public Purchase(int quantity, String item, double price) { this.quantity = quantity ; this.item = item ; this.price = price ; } /** * Converts a Purchase object to a String. * @return a String containing the quantity, description, price per item, * and total cost of a purchase. * Example: " 5 widgets @ 0.45 = $2.25" */ public String toString() { return " " + quantity + " " + item + "(s) @ " + price + " = " + Align.currency(quantity * price,0) ; } /** * Compute and return total cost of a purchase. * @return the cost of a purchase (quantity * price) */ public double getCost() // return cost of a purchase { return quantity * price ; } } // end of Purchase class definition ======================================== /** * Maintains a list of purchases. */ class PurchaseList { private ArrayList list ; // a list of Purchase objects private double totalCost ; // total cost of all purchases listed private String purchases ; // a multi-line String containing one // purchase per line /** * Creates an empty list */ public PurchaseList() { list = new ArrayList() ; totalCost = 0.0 ; purchases = "" ; // the "null" (empty) String } /** * Reads lines of input from keyboard until eof. Each line has number of * items purchased, a one-word description of the item, and the price per * item, separated by spaces */ public void readData() { Scanner input = new Scanner(System.in) ; // "priming" read System.out.println ("Enter quantity, description, and price of first item:") ; while ( input.hasNext() ) // while another line entered... { // read number of items bought int quantity = input.nextInt() ; // read item description String item = input.next() ; // read price per item double price = input.nextDouble() ; // create a Purchase object from data read Purchase current = new Purchase(quantity, item, price) ; // add Purchase object to list addToList(current) ; // "echo print" current purchase System.out.println( "\n\t" + current.toString() ) ; // prompt for next purchase System.out.println ("\nEnter quantity, description, and price of next item " + "(or ^Z to quit):" ) ; } // loop postcondition: at end-of-file } /** * Add a new purchase to the list, and concatenate it to the String * containing all purchases. */ public void addToList(Purchase aPurchase) { list.add(aPurchase) ; purchases = purchases + aPurchase.toString() + "\n" ; } /** * Returns the list of Purchases as a String. * @return a multi-line String containing each Purchase object on the list */ public String toString() { return purchases ; } /** * Computes and returns the total cost of all purchases on the list. * @return the total cost of all purchases */ public double getTotalCost() { // for each Purchase object on list... for (int i = 0 ; i < list.size() ; i++) { Purchase current = list.get(i) ; // get next Purchase totalCost += current.getCost() ; // add cost to total } return totalCost ; } } // end of PurchaseList class definition ===================================== /** * Test class for Purchase and PurchaseList classes */ public class PurchaseListTester { public static void main(String[] args) { PurchaseList list = new PurchaseList() ; list.readData() ; // print list of purchases and total cost System.out.println("\nYour purchases:\n" + list.toString() ) ; System.out.println( "Your subtotal is: " + Align.currency( list.getTotalCost(),0 ) ) ; } } /* sample output: Enter quantity, description, and price of first item: 1 Hummer 53500 1 Hummer(s) @ 53500.0 = $53,500.00 Enter quantity, description, and price of next item (or ^Z to quit): 2 Porsche 65500 2 Porsche(s) @ 65500.0 = $131,000.00 Enter quantity, description, and price of next item (or ^Z to quit): 10 Yugo 6999.98 10 Yugo(s) @ 6999.98 = $69,999.80 Enter quantity, description, and price of next item (or ^Z to quit): ^Z Your purchases: 1 Hummer(s) @ 53500.0 = $53,500.00 2 Porsche(s) @ 65500.0 = $131,000.00 10 Yugo(s) @ 6999.98 = $69,999.80 Your subtotal is: $254,499.80 */