JAVA PROBLEM Requirements Susan owns a café and hired a programmer to build a sy
ID: 3730228 • Letter: J
Question
JAVA PROBLEM
Requirements Susan owns a café and hired a programmer to build a system for her baristas to enter customer orders and maintain inventory. One of the classes the programmer has written as part of the larger system is Coffee (provided) You should inspect this class yourself to understand how it works. Note that cost" refers to the ingredient cost on Susan's behalf, and price" refers to the price a customer pays for a particular coffee. After Susan took one look at the class, she fired the programmer and hired you to refactor it. This class uses a lot of bad practice. Most notably, it relies on the usage of strings. For instance, when interfacing with Coffee, a long black has to be constructed like ArrayList ingredients = new ArrayList(); ingredients.add("espresso"); ingredients.add ("espresso") Coffee cnew Coffee(ingredients, "long black") Although this works, if one of the strings in the construction were anything but exactly what they are, there would be an error, or worse, the price or cost could be calculated incorrectly. Using enums, we can restrict the parameters to a select few. Susan wrote the class CoffeeEnums to help you get started. In Eclipse, right click CoffeeEnums.java, select Refactor -> Rename... , and change the name to CoffeeFactory (because we will be using the factory pattern later). This tool will automatically update all references, so it's very handy when you want to rename files, variables, methods, etc.Explanation / Answer
package example;
import java.util.Scanner;
//Class which gets type of coffee from user n shows ingredients
public class CreateCoffee{
private static int amount=0;
public static void main(String[] args) {
System.out.println("Please select number for type of coffee 1.Flat white 2.Long black 3.Mocha");
Scanner sc =new Scanner(System.in);
int number=sc.nextInt();//takes number selected by user
CreateCoffee m=new createCoffee();//object
if(number<=3)
m.coffeeFactory(number);
else {
//If user selects number other than. 1,2 or3 then calls the methou again
System.out.flush();
System.out.println("Please enter correct number");
number=sc.nextInt();
m.coffeeFactory(number);}
System.out.println("Total price="+amount);
number=sc.nextInt();
if(number<=3)
m.coffeeFactory(number);
else {
System.out.flush();
System.out.println("Please enter correct number");
number=sc.nextInt();
m.coffeeFactory(number);}
System.out.println("total amount="+amount);//amount will add
}
public void coffeeFactory(int number){
switch (number){
case 1: System.out.println("Flat white One unit of expresso and one unit of milk. ");
amount+=2;
break;
case 2: System.out.println("Long black Two unit of expresso.");
amount+=3;
break;
case 3: System.out.println("Mocha One unit of expresso , one unit of milk and one unit of chocolate.");
amount+=5;
break;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.