我正在try 优化如下代码段:
public class Kvarteret {
public static final String ONE_BEER = "hansa";
public static final String ONE_CIDER = "grans";
public static final String REAL_BEER = "vestkyst";
public static final String GT = "gt";
public static final String BACARDI_SPECIAL = "bacardi_special";
public int calculatePrice(String drink, boolean student, int amount) {
if (amount > 2 && (drink == GT || drink == BACARDI_SPECIAL)) {
throw new RuntimeException("Too many drinks, max 2.");
}
int price;
if (drink.equals(ONE_BEER)) { price = 74;}
else if (drink.equals(ONE_CIDER)) { price = 103;}
else if (drink.equals(REAL_BEER)) { price = 110; }
else if (drink.equals(GT)) { price = ingredient6() + ingredient5() + ingredient4();}
else if (drink.equals(BACARDI_SPECIAL)) {
price = ingredient6()/2 + ingredient1() + ingredient2() + ingredient3();
} else { throw new RuntimeException("Item not in menu"); }
if (student && (drink == ONE_BEER || drink == ONE_CIDER || drink == REAL_BEER)) { price = price - price/10;}
return price*amount;
}
//rom unit
private int ingredient1() { return 65;}
// grenadine unit
private int ingredient2() { return 10;}
//lime-juice unit
private int ingredient3() { return 10;}
//mint-leafs unit
private int ingredient4() { return 10; }
//tonic-water unit
private int ingredient5() { return 20; }
//gin unit
private int ingredient6() { return 85; }
}
该代码是一个计算饮料价格的简单程序.我用this个代码作为起点,因为看起来我们正在try 做类似的事情.
到目前为止,我的代码如下所示:
import java.util.*;
public class Kvarteret2 {
private static double price;
private static double order;
// Enums defines common values
private enum Beer_Cider {
BEER("Hansa", 74), CIDER("Grans", 103), REAL_BEER("Vestkyst", 110);
private static String order;
Beer_Cider(String order, double cost) {
order = order;
price = cost;
}
private static String getOrder() {return order;}
private static double getCost() {return price;}
}
private enum DrinkIngredients {
ROM("Rom", 65), GRENADINE("Grenadine", 10), LIME("Lime-juice", 10),
MINT("Mint", 10), TONIC("Tonic-water", 20), GIN("Gin", 85);
private static String ingredient;
DrinkIngredients(String ingredient, double cost){
ingredient = ingredient;
price = cost;
}
private static String getIngredient() {return ingredient;}
private static double getCost() {return price;}
}
// Dictionary with the drink as Key and a list of the ingredients and prices as value
Map<String, List<DrinkIngredients>> Drinks = new HashMap<String, List<DrinkIngredients>>();
public Kvarteret2() {
Drinks.put("GT", [DrinkIngredients.GIN, DrinkIngredients.TONIC, DrinkIngredients.MINT]);
}
private static double calculate_price(String drink, boolean student, int amount){
if (amount > 2) {
throw new IllegalArgumentException("Too many drinks! You can order max 2 drinks at a time.");
}
double student_discount = 0.1;
if (student){
if (drink == "BEER" || drink == "CIDER" || drink == "REAL BEER") {
price = amount * (price - price * student_discount);
}
}
return price;
}
}
由于以下代码,我收到一个错误:
public Kvarteret2() {
Drinks.put("GT", [DrinkIngredients.GIN, DrinkIngredients.TONIC, DrinkIngredients.MINT]);
}
我正在try 向字典中添加元素,但我认为这不是正确的方法.
我知道我的代码可能还没有得到100%的优化,但我只是想看看我是否可以让这个解决方案发挥作用,然后再try 让它变得更好(因为很可能有更好的方法可以做到这一点).
如何使用枚举正确创建要创建的词典?