Computer Science 210
Data Structures

Fall 2016, Siena College

PurchaseTrackerAll BlueJ Project

Click here to download a BlueJ project for PurchaseTrackerAll.


PurchaseTrackerAll Source Code

The Java source code for PurchaseTrackerAll is below. Click on a file name to download it.


PurchaseTrackerAll.java

/*
 * Example PurchaseTrackerAll: test out a class that stores items
 * purchased and their quantities, this time remembering all
 * of the items we read in and determining the "winners" at the end.
 *
 * Jim Teresco, The College of Saint Rose, CSC 523, Summer 2014
 *
 * $Id: PurchaseTrackerAll.java 2377 2014-06-10 03:12:01Z terescoj $
 */

import java.util.ArrayList;
import java.util.Scanner;

public class PurchaseTrackerAll {

    // A private method to prompt for and read in a valid item
    // from the given Scanner, returning a PurchasedItem object,
    // returns null if no item is entered.
    // The existence of this method avoids some repeated code in
    // main and also would be able to be used for any Scanner,
    // not just ones based on the standard input, and would be
    // a convenient place to add error checking on the input
    // values (for example, ensure positive values for price
    // and quantity or the addition of some exception handling
    // to recover from invalid inputs).
    private static PurchasedItem readPurchasedItem(Scanner s) {

        System.out.println("Enter the item name, unit price, and quantity purchased, \"done\" to stop:");
        String itemName = s.next();
        // if someone typed "done", we return null and don't try to read the other fields
        if (itemName.equals("done")) return null;
        double unitPrice = s.nextDouble();
        int quantity = s.nextInt();

        return new PurchasedItem(itemName, unitPrice, quantity);
    }

    public static void main(String[] args) {

        // create a keyboard Scanner to read lines that describe the items
        Scanner keyboard = new Scanner(System.in);

        // create an ArrayList of PurchasedItem objects to store all of them
        ArrayList<PurchasedItem> items = new ArrayList<PurchasedItem>();

        PurchasedItem item;
        // now read them all in
        do {
            item = readPurchasedItem(keyboard);
            if (item != null) {
                items.add(item);
            }
        } while (item != null);

        // if there was no valid item entered, we just quit
        if (items.isEmpty()) {
            System.out.println("No valid item purchases entered, exiting...");
            System.exit(0);
        }

        // so we know we have one valid item in the ArrayList, so we
        // initialize the "leader" in each category to be the first item (position 0)
        item = items.get(0);
        PurchasedItem leastExpensiveItem = item;
        PurchasedItem mostExpensiveItem = item;
        PurchasedItem largestQuantity = item;
        PurchasedItem highestTotalCost = item;

        System.out.println("All items:");
        System.out.println(item);

        // now we can loop over the remaining, printing each and deciding
        // which are the "winners"
        for (int itemNum = 1; itemNum < items.size(); itemNum++) {

            item = items.get(itemNum);
            System.out.println(item);
            // do we have any new leaders?
            if (item.getUnitPrice() < leastExpensiveItem.getUnitPrice()) {
                leastExpensiveItem = item;
            }
            if (item.getUnitPrice() > mostExpensiveItem.getUnitPrice()) {
                mostExpensiveItem = item;
            }
            if (item.getQuantity() > largestQuantity.getQuantity()) {
                largestQuantity = item;
            }
            if (item.getTotalCost() > highestTotalCost.getTotalCost()) {
                highestTotalCost = item;
            }

        }

        // now report our winners:
        System.out.println("Least expensive item:    " + leastExpensiveItem);
        System.out.println("Most expensive item:     " + mostExpensiveItem);
        System.out.println("Largest quantity item:   " + largestQuantity);
        System.out.println("Highest total cost item: " + highestTotalCost);

    }
}

PurchasedItem.java


/**
 * A custom class to represent a purchased item based on name,
 * unit price, and a quantity purchased.
 * 
 * @author Jim Teresco
 * 
 * $Id: PurchasedItem.java 2377 2014-06-10 03:12:01Z terescoj $
 */

import java.text.DecimalFormat;

public class PurchasedItem {

    // we need instance variables to remember the
    // name, unit price, and quantity
    private String name;
    private double unitPrice;
    private int quantity;
    
    // we can create a static class variable that refers to
    // a single DecimalFormat object that can be shared among
    // all instances of this class
    private static DecimalFormat currency = new DecimalFormat("$0.00");
    
    // our constructor needs the initial values for the three
    // fields in our object
    public PurchasedItem(String name, double unitPrice, int quantity) {
    
        this.name = name;
        this.unitPrice = unitPrice;
        this.quantity = quantity;
    }
    
    // we'll need a toString method
    public String toString() {
        
        return name + ": " + quantity + " @ " + currency.format(unitPrice) +
            " = " + currency.format(unitPrice * quantity);
    }
    
    // a variety of accessors are needed here, and here they are
    public double getUnitPrice() {
        
        return unitPrice;
    }
    
    public int getQuantity() {
        
        return quantity;
    }
    
    public double getTotalCost() {
        
        return unitPrice * quantity;
    }
}