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;
}
}
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;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.