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

JAVA PROBLEM package patt.Coffee; public class CoffeeFactory { public static enu

ID: 3730742 • Letter: J

Question

JAVA PROBLEM

package patt.Coffee;

public class CoffeeFactory {

   public static enum Type {

       LONG_BLACK(4.0),

       FLAT_WHITE(4.75),

       MOCHA(5.5);

       private double price;

       Type(double price) {

           this.price = price;

       }

       public double getPrice() {

           return price;

       }

   }

   public static enum Ingredient {

       ESPRESSO(0.5),

       MILK(1),

       CHOCOLATE(1.5);

       private double cost;

       Ingredient(double cost) {

           this.cost = cost;

       }

       public double getCost() {

           return cost;

       }

   }

}

package patt.Coffee;

import java.awt.Window.Type;
import java.util.ArrayList;

import patt.Coffee.CoffeeFactory.Ingredient;

public class Coffee {
   Type type;
   double cost;
   ArrayList<String> ingredients;

   public Coffee(ArrayList<String> ingredients, Type type) {
       this.type = type;

       this.ingredients = ingredients;

       double sum = 0;
       for (String ingredient : ingredients) {
           if (ingredient == "espresso") {
               sum += 0.5;
           } else if (ingredient == "milk") {
               sum += 1.0;
           } else if (ingredient == "chocolate") {
               sum += 1.5;
           } else {
               sum += 0;
           }
       }
       this.cost = sum;

   }

   public double getCost() {
       return cost;
   }

   public double getPrice() {
      
       return this.type.hashCode();
      

   }

   public String listIngredients() {
       String string = "";
      
      
       for (String ingredient : ingredients) {
           string += ingredient;
           string += " ";
       }
       return string;
   }
}

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

Given below is the modified code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


package patt.Coffee;

import java.util.ArrayList;

public class CoffeeFactory {

public static enum Type {

LONG_BLACK(4.0),

FLAT_WHITE(4.75),

MOCHA(5.5);

private double price;

Type(double price) {

this.price = price;

}

public double getPrice() {

return price;

}

}

public static enum Ingredient {

ESPRESSO(0.5),

MILK(1),

CHOCOLATE(1.5);

private double cost;

Ingredient(double cost) {

this.cost = cost;

}

public double getCost() {

return cost;

}

}

public static Coffee CreateCoffee(Type type){
ArrayList<Ingredient> ingredients = new ArrayList<Ingredient>();

if(type == Type.FLAT_WHITE)
{
ingredients.add(Ingredient.ESPRESSO);
ingredients.add(Ingredient.MILK);
return new Coffee(ingredients, Type.FLAT_WHITE);
}
else if(type == Type.LONG_BLACK)
{
ingredients.add(Ingredient.ESPRESSO);
ingredients.add(Ingredient.ESPRESSO);
return new Coffee(ingredients, Type.LONG_BLACK);
}
else if(type == Type.MOCHA)
{
ingredients.add(Ingredient.ESPRESSO);
ingredients.add(Ingredient.MILK);
ingredients.add(Ingredient.CHOCOLATE);

return new Coffee(ingredients, Type.MOCHA);
}
else
return null;
}

}


package patt.Coffee;

import java.util.ArrayList;

public class CoffeeFactory {

public static enum Type {

LONG_BLACK(4.0),

FLAT_WHITE(4.75),

MOCHA(5.5);

private double price;

Type(double price) {

this.price = price;

}

public double getPrice() {

return price;

}

}

public static enum Ingredient {

ESPRESSO(0.5),

MILK(1),

CHOCOLATE(1.5);

private double cost;

Ingredient(double cost) {

this.cost = cost;

}

public double getCost() {

return cost;

}

}

public static Coffee CreateCoffee(Type type){
ArrayList<Ingredient> ingredients = new ArrayList<Ingredient>();

if(type == Type.FLAT_WHITE)
{
ingredients.add(Ingredient.ESPRESSO);
ingredients.add(Ingredient.MILK);
return new Coffee(ingredients, Type.FLAT_WHITE);
}
else if(type == Type.LONG_BLACK)
{
ingredients.add(Ingredient.ESPRESSO);
ingredients.add(Ingredient.ESPRESSO);
return new Coffee(ingredients, Type.LONG_BLACK);
}
else if(type == Type.MOCHA)
{
ingredients.add(Ingredient.ESPRESSO);
ingredients.add(Ingredient.MILK);
ingredients.add(Ingredient.CHOCOLATE);

return new Coffee(ingredients, Type.MOCHA);
}
else
return null;
}

}