Homework3Driver

// NOTE: This driver will NOT compile until you have 

// created (at minimum) skeleton implementations of

// the CoffeeOption class and its subclasses from HW 3

import java.util.*;

public class Homework3Driver

{

    private static ArrayList<CoffeeOption> myOrder;

    private static Scanner sc;

    private static double totalCharge;

    

    public static void main (String [] args)

    {

        myOrder = new ArrayList<CoffeeOption> ();

        totalCharge = 0.0;

        sc = new Scanner(System.in);

        

        int userChoice = -1;

        

        do

        {

            userChoice = displayMenu();

            handle(userChoice);

        } while (userChoice != 0);

    }

    

    private static int displayMenu ()

    {

        System.out.println("\n\n");

        System.out.println("Main Menu\n\n");

        System.out.println("1. Add sugar");

        System.out.println("2. Add cream");

        System.out.println("3. Add a flavoring shot");

        System.out.println("4. Print the current order");

        System.out.println("5. Clear the current order");

        System.out.println();

        System.out.println("0. Exit");

        System.out.println();

        System.out.print("Please select an option: ");

        

        int result = sc.nextInt();

        sc.nextLine(); // consume extraneous newline character

        

        System.out.println(); // Add an extra line for formatting

        

        return result;

    }

    

    private static void handle (int choice)

    {

        switch (choice)

        {

            case 1: addSugar();

                    break;

            case 2: addCream();

                    break;

            case 3: addFlavoring();

                    break;

            case 4: printOrder();

                    break;

            case 5: resetOrder();

                    break;

        }

    }

    

    private static void addSugar ()

    {

        myOrder.add(new Sugar());

        totalCharge += 0.05;

    }

    

    private static void addCream ()

    {

        myOrder.add(new Cream());

        totalCharge += 0.10;

    }

    

    private static void addFlavoring ()

    {

        myOrder.add(new Flavoring());

        totalCharge += 0.25;

    }

    

    private static void printOrder ()

    {

        if (myOrder.size() < 1)

        {

            System.out.println("The order is currently empty");

        }

        else

        {

            System.out.println("Current list of CoffeeOptions:");

            System.out.println();

            

            for (int i = 0; i < myOrder.size(); i++)

            {

                System.out.println(myOrder.get(i));

            }

        }

        

        System.out.println();

        System.out.println("Total charge so far: " + totalCharge);

    }

    

    private static void resetOrder ()

    {

        myOrder.clear(); // Get rid of the current contents

        totalCharge = 0.0;

    }

}

This page was last modified on 8/25/09