Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Fiendz Coffee™ is in the process of redesigning their order-taking system.

ID: 3625056 • Letter: J

Question

Java Fiendz Coffee™ is in the process of redesigning their order-taking system. The
new system will require customers to ?rst visit one of several computer kiosks at the
store entrance. Customers will enter their name and then order items from a series of
menus. A customer may order one or more coffee beverages. For each beverage, the
customer can select the size (small, medium, or large) and choose to add options
(cream, sugar, and ?avoring shots). When the order is complete, the system will print
out a receipt with the order number. The customer can then take the receipt to the
counter to pay for and pick up the order.
For this assignment, complete the classes described in the following steps:
1. Create an abstract class named CoffeeOption. This class should have the
following components:
a. A protected double instance variable named cost.
b. A protected String instance variable named description.
c. A public method named price. This method does not take any arguments, and
it returns a double value.
d. A public method named toString. This method does not take any arguments.
It returns the String “add”, followed by name, followed by several spaces,
followed by cost.
2. Create a subclass of CoffeeOption named Sugar, with the following method:
a. A no-argument constructor that sets name to “sugar” and cost to 0.05.
3. Create a subclass of CoffeeOption named Cream, with the following method:
a. A no-argument constructor that sets name to “cream” and cost to 0.10.
4. Create a subclass of CoffeeOption named Flavoring, with the following
variables and methods:
A private ArrayList that can hold String objects, called flavors.
b. A private String named selectedFlavor.
c. A private method named loadFlavors. This method does not take any
arguments, and it does not return any value. The loadFlavors() method
attempts to open a ?le on disk named “?avors.txt”. It reads in the complete ?le line
by line, storing each line in the ?avors ArrayList. You may assume that this ?le
always exists, and always contains at least one line (HINT: use Scanner's
hasNextLine() method in your loop condition).
d. A no-argument constructor. The constructor should perform the following steps, in
order:
i. Set name to “?avor shot” and cost to 0.25.
ii. Call loadFlavors() to ?ll the ArrayList.
iii. Create a Scanner to read from the keyboard.
iv.Use a loop to display the contents of flavors as a numbered menu, as in:
Please select a ?avor:
1) blueberry
2) raspberry
3) french vanilla
4) cinnamon
5) mocha
v. Prompt the user to enter a number.
vi.Read an integer from the user (you may assume that the user always chooses
a valid menu option).
vii.Set selectedFlavor to the selected element of the ArrayList.
e. An overridden version of toString(). This version of toString() should call
super.toString(), but it should include the value of selectedFlavor on the
following line, as in:
add ?avor shot 0.25
blueberry



flavors
chocolate
chocolate mint
raspberry
caramel
vanilla
hazelnut
blueberry
cinnamon
butter pecan
amaretto
coconut
pumpkin spice

Please use the following dreiver.

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(" ");
System.out.println("Main Menu ");
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;
}
}

Explanation / Answer

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()+" "+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;
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote