1. Create a Coffee class to represent a single hot beverage. Every Coffee object
contains the following instance fields:
a. A protected double variable named basePrice. This variable holds the cost
of the beverage without accounting for any special options (cream, sugar, etc.).
b. A protected ArrayList variable named options. This variable should only store
CoffeeOption objects that have been added to the given beverage.
c. A protected String variable named size. This variable represents the size of
the beverage.
d. A protected boolean variable named isDecaf. This variable will be true if the
beverage is decaffeinated, and false otherwise.
e. A public constructor that takes two arguments: a String followed by a
boolean value. The constructor should create a new, empty ArrayList and
assign it to options. The constructor should set the value of size to the String
argument. The constructor should set the value of basePrice depending on the
value of the String argument (“small” = 1.50, “medium” = 2.00, “large” = 2.50,
“extra large” = 3.00). You may assume that the String argument will always be
one of these four choices, with that spelling and capitalization. Finally, the
constructor should set the value of isDecaf to that of its boolean parameter.
f. A public method named addOption(). This method takes a CoffeeOption
as its argument, and does not return anything. This method adds its argument to
the end of the options ArrayList.
g. A public method named price(). This method returns a double value, and
does not take any arguments. The price() method returns the sum of
basePrice and the prices of all of the elements in options. Note that decaffeinated and regular
beverages are the same price.
h. A public toString() method. This method returns a String, but does not
take any arguments. Your toString() method should return a String that
contains a description of the Coffee object, in the following format:
Coffee base-price
Total: $total-price
should be either “Regular” or “”Decaf”
For example, a small decaffeinated coffee with no added options would produce
the following output:
small Decaf Coffee 1.50
Total: $1.50
A medium regular coffee with one cream and one sugar would produce the
following output:
medium Regular Coffee 2.00
add cream 0.10
add sugar 0.05
Total: $2.15
(do not worry about formatting the price to exactly two decimal places; 1.5 and
1.499999999 are equally acceptable substitutes for 1.50)
Hint: use the toString() method(s) that you developed for Homework 3.
i. You may add any additional instance variables or methods to this class that you
wish.
2. Create a class named Order. This class maintains a list of Coffee objects, and
contains the following fields:
a. A private ArrayList named items that holds Coffee objects.
b. A private int named orderNumber.
c. A public constructor that assigns a new, empty ArrayList to items and
assigns a random integer value (between 1 and 2000) to orderNumber (see the
end of this document for information about Java's Random class).
d. A public method named add() that takes a Coffee object as its argument and
does not return any value. This method adds its argument to the end of the items
ArrayList.
e. A public method named getNumber() that returns the order number. This
method does not take any arguments.
f. A public method named getTotal(). This method returns a double value
and does not take any arguments. This method returns the total price of the items
in the current order, including 8.625% sales tax for Suffolk County.
g. A public toString() method that returns a String and does not take any
arguments. This method should return a String that lists the order number, the
current number of Coffee objects in items, and the total price for the order.
For example, toString() might return a String like the following:
Order #212 3 item(s) $8.25
h. A public method named receipt(). This method does not take any
arguments. It returns a String containing a neatly-formatted order receipt that
includes the following information:
i. The order number, with an appropriate label
ii. The total number of items in the order, with an appropriate label. Note that this
value only includes whole beverages; do not include coffee options as
separate items!
iii.A detailed list of the items in the order (HINT: use Coffee's toString()
method)
iv.The subtotal for the order, with an appropriate label
v. The tax amount, with an appropriate label
vi.The total price of the order, with an appropriate label.
i. You may add any additional instance variables or methods to this class that you
wish.
3. Create a subclass of Coffee named IcedCoffee. An IcedCoffee object is
available in the same sizes as a regular Coffee object, except it costs $0.50 more
for the equivalent size (for example, a large IcedCoffee has a base price of $3.00
instead of $2.50). Be sure to provide the following functionality for your IcedCoffee
class:
a. A public constructor that, like the Coffee constructor, takes a size (a String)
and a regular/decaf value (a boolean) as its arguments.
b. An overridden version of toString() that replaces the word “Coffee” with “Iced
Coffee”. Otherwise, toString() provides exactly the same output as its
superclass version.
4. Using the sample driver , create a menu-based program
that allows the user to perform the following actions:
a. Create a new Order. If there is an order currently in progress, it is discarded/
replaced without warning.
b. Add a new Coffee object to the current order. This option should let the user do
the following:
i. Specify a size for the beverage
ii. Specify whether the beverage is regular or decaffeinated
iii.Add coffee options (cream, sugar, and flavor shots) to the beverage until the
user indicates that he/she is done. (HINT: use a while or do-while loop)
This option is only available if there is a current order.
c. Add a new IcedCoffee object to the order. This option should provide the same
functionality as the preceding option. (HINT: instead of writing the same code
twice, can you avoid duplication by using a method or if statement(s)?)
d. Display the contents of the current order. This option is only available if there is an
order in progress.
e. Place the current order. Selecting this option causes the program to print out the
receipt for the current order, and then delete it (by setting any references to this
Order to null). This option is only available if there is an order in progress.
f. Cancel the current order. Selecting this option causes any references to the
current Order to be set to null. This option is only available if there is an order
in progress.
g. Quit the program.
Each time the menu is displayed, if there is an order currently in progress, a short
summary of the order should be displayed as well (use Order's toString()
method for this).
Here's the sample driver:
import java.util.*;
public class ProjectDriver
{
private static Order myOrder;
private static Scanner sc;
private static double totalCharge;
public static void main (String [] args)
{
myOrder = null;
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. New order");
System.out.println("2. Add Coffee");
System.out.println("3. Add Add Iced Coffee");
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: newOrder();
break;
case 2: addCoffee();
break;
case 3: addIcedCoffee();
break;
case 4: printOrder();
break;
case 5: resetOrder();
break;
}
}
private static newOrder()
{
myOrder = new Order();
totalCharge = 0;
}
private static void addCoffee()
{
System.out.print("Enter size: ");
String size = sc.nextLine();
System.out.print("Decaf? ");
String d = sc.nextLine();
Coffee c;
if (d.equals("yes"))
{
c = new Coffee(size, true);
}
else
{
c = new Coffee(size, false);
}
// Add submenu for coffee options
int choice = -1;
while (choice > 0)
{
System.out.println("Options\n");
System.out.println("1. Add sugar");
...
if (choice == 1)
{
c.add(new Sugar());
totalCharge += 0.05;
}
}
myOrder.add(c);
}
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 == null)
{
System.out.println("NO current order!");
}
else
{
System.out.println(myOrder.receipt());
System.out.println();
}
System.out.println();
System.out.println("Total charge so far: " + totalCharge);
}
private static void resetOrder ()
{
myOrder = new Order(); // Get rid of the current contents
totalCharge = 0.0;
}
}
---------------------------------------------------------------------------
Other classes:
Flavoring.java
import java.util.*;
import java.io.*;
public class Flavoring extends CoffeeOption {
private List<String> flavors = new ArrayList<String>();
private String selectedFlavor;
private void loadFlavors() {
Scanner sc;
try {
sc = new Scanner(new File("flavors.txt"));
} catch (Exception e) {
System.err.println(e);
return;
}
while (sc.hasNextLine()) {
flavors.add(sc.nextLine());
}
}
public Flavoring() {
description = "flavor shot";
cost = 0.25;
loadFlavors();
Scanner sc = new Scanner(System.in);
// Print the menu of flavors
System.out.println("Please select a flavor:");
for (int i=0;i<flavors.size();i++) {
System.out.println((i+1)+") "+flavors.get(i));
}
System.out.print("Your choice? ");
selectedFlavor = flavors.get(sc.nextInt()-1);
System.out.println();
}
public String toString() {
return super.toString()+"\n"+selectedFlavor;
}
}
-------------------------------------------------
Cream.java
public class Cream extends CoffeeOption {
public Cream() {
description="cream";
cost = 0.10;
}
}
--------------------------------------------------
Sugar.java
public class Sugar extends CoffeeOption {
public Sugar() {
description = "sugar";
cost = 0.05;
}
}
-------------------------------------------------
CoffeeOption.java
public abstract class CoffeeOption {
protected double cost;
protected String description;
public double price() {
return cost;
}
public String toString() {
return "add "+description+" "+cost;
}