如何正确使用枚举进行简单的价格计算 (Java)
How to properly use Enums for simple price calculation (Java)
我正在尝试优化如下所示的代码片段:
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 代码作为起点,因为看起来我们正在尝试做类似的事情。
到目前为止我的代码如下所示:
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]);
}
我正在尝试向字典中添加元素,但我认为这不是正确的方法。
我知道我的代码可能还没有 100% 优化,但我只是想看看我是否可以让这个解决方案工作,然后再尝试让它变得更好(因为很可能有更好的方法来做到这一点) .
如何正确创建我尝试使用枚举创建的字典?
虽然你没有提到你得到了什么错误,但这是你如何填充你的 Kvarteret2
构造函数:
// 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>>();
List<DrinkIngredients> list=new ArrayList<DrinkIngredients>(); // define ArrayList outside constructor
public Kvarteret2() {
list.add(DrinkIngredients.GIN);
list.add(DrinkIngredients.TONIC);
list.add(DrinkIngredients.MINT);
Drinks.put("GT", ls);
}
你的代码有很多错误。
您在 Java 中使用了错误的语法来创建 list(它的工作方式与 Java脚本):
[DrinkIngredients.GIN, DrinkIngredients.TONIC, DrinkIngredients.MINT] // <- that's not correct
List.of(DrinkIngredients.GIN, DrinkIngredients.TONIC, DrinkIngredients.MINT) // <- correct
必须与特定 enum-constant 关联的 Enum-fields 不应标记为 static
。修饰符static
表示一个字段在class的所有实例之间共享,一天结束时enum
是class。如果您希望字符串 order
对所有种类的饮料都是唯一的,则此行不正确:
private static String order;
构造函数中的另一个错误:
Beer_Cider(String order, double cost) {
order = order;
price = cost;
}
第 order = order;
行将无法正常工作。它不会分配对象 属性,而只是将参数 order
重新分配给自己。您应该使用关键字 this
来引用对象字段顺序。
private enum BeerCider {
BEER("Hansa", 74), CIDER("Grans", 103), REAL_BEER("Vestkyst", 110);
private String order;
private double cost;
Beer_Cider(String order, double cost) {
this.order = order;
this.cost = cost;
}
private String getOrder() {return order;}
private double getCost() {return cost;}
}
有关enums的更多信息,请查看 this tutorial
旁注: 根据 Java naming conventions 下划线应仅用于常量名称(如 unum-members、static final
字段),但是enum
的名称必须写成 camel-case BeerCider
,就像 class 和接口的名称一样。
我正在尝试优化如下所示的代码片段:
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 代码作为起点,因为看起来我们正在尝试做类似的事情。
到目前为止我的代码如下所示:
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]);
}
我正在尝试向字典中添加元素,但我认为这不是正确的方法。
我知道我的代码可能还没有 100% 优化,但我只是想看看我是否可以让这个解决方案工作,然后再尝试让它变得更好(因为很可能有更好的方法来做到这一点) .
如何正确创建我尝试使用枚举创建的字典?
虽然你没有提到你得到了什么错误,但这是你如何填充你的 Kvarteret2
构造函数:
// 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>>();
List<DrinkIngredients> list=new ArrayList<DrinkIngredients>(); // define ArrayList outside constructor
public Kvarteret2() {
list.add(DrinkIngredients.GIN);
list.add(DrinkIngredients.TONIC);
list.add(DrinkIngredients.MINT);
Drinks.put("GT", ls);
}
你的代码有很多错误。
您在 Java 中使用了错误的语法来创建 list(它的工作方式与 Java脚本):
[DrinkIngredients.GIN, DrinkIngredients.TONIC, DrinkIngredients.MINT] // <- that's not correct
List.of(DrinkIngredients.GIN, DrinkIngredients.TONIC, DrinkIngredients.MINT) // <- correct
必须与特定 enum-constant 关联的 Enum-fields 不应标记为 static
。修饰符static
表示一个字段在class的所有实例之间共享,一天结束时enum
是class。如果您希望字符串 order
对所有种类的饮料都是唯一的,则此行不正确:
private static String order;
构造函数中的另一个错误:
Beer_Cider(String order, double cost) {
order = order;
price = cost;
}
第 order = order;
行将无法正常工作。它不会分配对象 属性,而只是将参数 order
重新分配给自己。您应该使用关键字 this
来引用对象字段顺序。
private enum BeerCider {
BEER("Hansa", 74), CIDER("Grans", 103), REAL_BEER("Vestkyst", 110);
private String order;
private double cost;
Beer_Cider(String order, double cost) {
this.order = order;
this.cost = cost;
}
private String getOrder() {return order;}
private double getCost() {return cost;}
}
有关enums的更多信息,请查看 this tutorial
旁注: 根据 Java naming conventions 下划线应仅用于常量名称(如 unum-members、static final
字段),但是enum
的名称必须写成 camel-case BeerCider
,就像 class 和接口的名称一样。