CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Collections

Collections and Generics
Enhanced Invoice app
Code

Code for the InvoiceApp class

import java.util.Scanner;
import java.text.NumberFormat;

public class InvoiceApp
{
    private static GenericQueue invoices
        = new GenericQueue();

    private static Invoice invoice;

    private static Scanner sc;

    public static void main(String args[])
    {
        System.out.println("Welcome to the Invoice application.\n");
        getInvoices();
        displayInvoices();
    }

    public static void getInvoices()
    {
        sc = new Scanner(System.in);
        int invoiceNumber = 1;
        String anotherInvoice = "y";
        while (anotherInvoice.equalsIgnoreCase("y"))
        {
            invoice = new Invoice();
            System.out.println("\nEnter line items for invoice "
                + invoiceNumber);
            getLineItems();
            invoices.push(invoice);

            // see if the user wants to continue
            anotherInvoice = Validator.getString(sc,
                "Another invoice? (y/n): ");
            System.out.println();
            invoiceNumber++;
        }
    }

    public static void getLineItems()
    {
        String anotherItem = "y";
        while (anotherItem.equalsIgnoreCase("y"))
        {
            // get the input from the user
            String productCode = Validator.getString(sc,
                "Enter product code: ");
            int quantity = Validator.getInt(sc,
                "Enter quantity:     ", 0, 1000);

            Product product = ProductDB.getProduct(productCode);
            invoice.addItem(new LineItem(product, quantity));

            // see if the user wants to continue
            anotherItem = Validator.getString(sc,
                "Another line item? (y/n): ");
            System.out.println();
        }
     }

    public static void displayInvoices()
    {
        System.out.println("You entered the following invoices:\n");
        System.out.println("Number\tTotal");
        System.out.println("------\t-----");
        double batchTotal = 0;
        int invoiceNumber = 1;
        while (invoices.size() > 0)
        {
            Invoice invoice = invoices.pull();
            System.out.println(invoiceNumber + "\t"
               + invoice.getFormattedTotal());
            invoiceNumber++;
            batchTotal += invoice.getInvoiceTotal();
        }
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        System.out.println("Total for all invoices: "
            + currency.format(batchTotal));
    }
}
Previous | Overview | Code | Next