Java 如何避免在代码中使用if-else?
作为我学习过程的一部分,我正在用Java创建一个小程序,帮助一家虚构的公司销售汉堡 在下面的代码中,我为一个汉堡创建了一个类,并添加了一些选项来向汉堡中添加诸如生菜、胡萝卜等 我所面临的问题是,如何去检查每一个额外添加到基本汉堡中的汉堡基本汉堡只意味着面包和肉,而不使用if-else太多。我尝试过的一种方法是在代码中为每个加法赋值,例如,生菜1,胡萝卜2等等。加上1和2,我们得到3;我们可以找3来确定是否添加了生菜和胡萝卜,以便计算价格。我也有其他选择 然而,出现了一些问题: 有些情况下,当通过添加生成的数字从不同的添加中创建两次时,可以通过将该数字乘以某个数字或在创建更多边缘情况时添加1来解决此问题 通过创建这样的案例,我的主要问题是需要大量if-else语句,我正试图避免这些语句,以便有效地编写我需要的代码 请建议是否有任何其他方法来做这件事。请注意代码还不完整,因为我不想在hamburger类中创建更多if-else语句;方法cal_price检查添加内容,但它足以完全理解代码的性质。代码如下:Java 如何避免在代码中使用if-else?,java,oop,optimization,Java,Oop,Optimization,作为我学习过程的一部分,我正在用Java创建一个小程序,帮助一家虚构的公司销售汉堡 在下面的代码中,我为一个汉堡创建了一个类,并添加了一些选项来向汉堡中添加诸如生菜、胡萝卜等 我所面临的问题是,如何去检查每一个额外添加到基本汉堡中的汉堡基本汉堡只意味着面包和肉,而不使用if-else太多。我尝试过的一种方法是在代码中为每个加法赋值,例如,生菜1,胡萝卜2等等。加上1和2,我们得到3;我们可以找3来确定是否添加了生菜和胡萝卜,以便计算价格。我也有其他选择 然而,出现了一些问题: 有些情况下,当通过
public class Main {
public static void main(String[] args) {
Breadroll breadroll_type = new Breadroll("Sesame Seed Bun");
meat meat_type = new meat("Beef");
Hamburger my_burger = new Hamburger("Hamburger",breadroll_type,meat_type);
my_burger.select_items(1,2,3,4);
my_burger.cal_price();
my_burger.getBreadroll_type();
my_burger.getMeat_type();
}
}
public class Breadroll {
private String breadroll_type;
public Breadroll(String breadroll_type) {
this.breadroll_type = breadroll_type;
}
public String getBreadroll_type() {
return breadroll_type;
}
}
public class meat {
private String meat_type;
public meat(String meat_type) {
this.meat_type = meat_type;
}
public String getMeat_type() {
return meat_type;
}
}
public class Hamburger {
private String name;
Breadroll breadroll_type;
meat meat_type;
private double base_price; //Price without addons
private int lettuce;
private int carrot;
private int tomato;
private int cheese;
private int hot_sauce;
private int mustard;
private int total_select;
public Hamburger(String name, Breadroll breadroll_type, meat meat_type) {
this.name = name;
this.breadroll_type = breadroll_type;
this.meat_type = meat_type;
this.base_price = 2.75;
}
public void select_items(int lettuce, int carrot, int tomato, int cheese) {
this.lettuce = lettuce;
this.carrot = carrot;
this.tomato = tomato;
this.cheese = cheese;
this.total_select = lettuce + carrot + tomato + cheese;
}
public void cal_price()
{
double final_price;
double lettuce_price = 0.50;
double carrots_price = 0.60;
double tomatos_price = 0.70;
double cheese_price = 0.85;
if(total_select == 0) {
System.out.println("Order Placed : Hamburger with no additions " + getBase_price() + "$");
}
else if (total_select == 1) {
final_price = getBase_price() + lettuce_price;
System.out.println("Order Placed : Hamburger with all lettuce " + (float) final_price + "$");
}
else if (total_select == 2) {
final_price = getBase_price() + carrots_price;
System.out.println("Order Placed : Hamburger with all carrot " + (float) final_price + "$");
}
else if (total_select == 3) {
final_price = getBase_price() + tomatos_price;
System.out.println("Order Placed : Hamburger with all tomato " + (float) final_price + "$");
}
else if (total_select == 4) {
final_price = getBase_price() +cheese_price;
System.out.println("Order Placed : Hamburger with all cheese " + (float) final_price + "$");
}
else if (total_select*100 == 1000) {
final_price = getBase_price() + lettuce_price + carrots_price + tomatos_price + cheese_price;
System.out.println("Order Placed : Hamburger with all additions " + (float) final_price + "$");
}
}
public String getName() {
return name;
}
public void getBreadroll_type() {
System.out.println(breadroll_type.getBreadroll_type());
}
public void getMeat_type() {
System.out.println(meat_type.getMeat_type());
}
public double getBase_price() {
return base_price;
}
public int getLettuce() {
return lettuce;
}
public int getCarrot() {
return carrot;
}
public int getTomato() {
return tomato;
}
public int getCheese() {
return cheese;
}
}
您根本不需要使用条件语句,只需将每个项目的价格乘以其价格即可:
final_price = getBase_price()
+ lettuce_num * lettuce_price
+ carrots_num * carrots_price
+ tomatos_num * tomatos_price
+ cheese_num * cheese_price;
在设置方法中,您应该设置项目的数量/数量,例如莴苣,而不是1==莴苣和2==xxx:
my_burger.select_items(5,0,0,0); // 5 lettuce, 0 all other
my_burger.select_items(1,0,0,90); // 1 lettuce, 90 cheese, 0 all other
将所有这些放在一起缩小:
public class Main {
public static void main(String[] args) {
Hamburger my_burger = new Hamburger("Hamburger");
my_burger.select_items(
100, // 100 lettuce
0, // 0 carrot
2, // 2 tomato
1); // 1 cheese
my_burger.cal_price();
}
}
public class Hamburger {
private String name;
private double base_price = 2.75;
private int lettuce;
private double lettuce_price = 0.50;
private int carrot;
private double carrots_price = 0.60;
private int tomato;
private double tomatos_price = 0.70;
private int cheese;
private double cheese_price = 0.85;
public Hamburger(String name) {
this.name = name;
}
public void select_items(int lettuce, int carrot, int tomato, int cheese) {
this.lettuce = lettuce;
this.carrot = carrot;
this.tomato = tomato;
this.cheese = cheese;
}
public void cal_price()
{
double final_price = getBase_price()
+ lettuce * lettuce_price
+ carrots * carrots_price
+ tomato * tomatos_price
+ cheese * cheese_price;
// TODO print price
}
}
“汉堡包”对象的实现似乎有点过于复杂。通常,面向对象编程来表示实际的物理对象,应该尽可能选择最简单、最原子的属性,以避免出现这种情况。汉堡要么有生菜,要么没有生菜。它要么有要么没有西红柿。我的建议是为每个浇头使用单独的布尔变量,或者如果愿意的话,使用一个表示浇头的布尔数组。然后,在汉堡包中添加一个get_price方法,该方法有一个if语句块用于计算价格。如果您真的想进入OOP,那么每个topping都可以是一个带有价格的对象,您可以将其附加到汉堡上的Toppings ArrayList中。然后,您的get_price方法将使用for each来合计顶级对象的所有价格,以及汉堡的price属性。如果您想要多个数量的相同浇头,此方法可能会更好。这是编程中有趣的部分-您可以选择对您最有意义的实现并进行尝试 制作了一个汉堡包,上面有名字、面包类型、肉类型,并添加了额外的,以后,你还可以为面包类型和肉分别制作方法。在extra方法中,我们预先计算额外的价格,因此调用getPrice将所选面包/肉类型的所有额外价格相加。还有其他方法可以做到这一点,但通过这种方法,您还将有一个合同和开关语句的示例。去掉了一些成分,但你明白了 还可以看看命名约定与您的命名约定有何不同
class HamburgerContract {
// Specify your constants in this class so all classes are able to communicate with the same values
// Change the name to something shorter like HBC for easier reference, ie HBC.BR_TYPE_SESAM
public static final int BR_TYPE_SESAM = 1;
public static final int BR_TYPE_HONEY = 2;
public static final int MEAT_TYPE_COW = 1;
public static final int MEAT_TYPE_CHICKEN = 2;
}
public class Hamburger {
public static void main(String[] args) {
// Made a hamburger with name, breadtype, meattype and added the extra's later on
// You can make seperate methods for the breadtype and meat as well
// In the extra methods we calculate the extra price beforehand
// So calling getPrice() sums up all the extras with chosen bread/meat type
// There are other ways to do this but this way you'll also have an example of a contract and switch statement
// Removed a few ingredients but you get the point
Hamburger myHamburger = new Hamburger("Varadero Burger", HamburgerContract.BR_TYPE_SESAM, HamburgerContract.MEAT_TYPE_CHICKEN, 2.50);
myHamburger.addCarrot();
myHamburger.addLettuce();
myHamburger.removeCarrot();
System.out.print("The price of your " + myHamburger.getName() + "is $ " + myHamburger.getPrice());
}
private String burgerName;
private int breadrollType;
private int meatType;
private boolean lettuce;
private boolean carrot;
private double basePrice;
private double extraPrice;
public Hamburger(String burgerName, int breadrollType, int meatType, double basePrice) {
this.burgerName = burgerName;
this.breadrollType = breadrollType;
this.meatType = meatType;
this.basePrice = basePrice;
this.extraPrice = 0;
this.lettuce = false;
this.carrot = false;
}
public void addLettuce() {
// extra check if lettuce is not already added, so you wont add to the price twice
// same goes for removing the lettuce or the carrot methods
if (!lettuce) {
letuce = true;
extraPrice += 0.25;
}
}
public removeLettuce() {
if (lettuce) {
letuce = false;
extraPrice -= 0.25;
}
}
public void addCarrot() {
if (!carrot) {
carrot = true;
extraPrice += 0.20;
}
}
public void removeCarrot() {
if (carrot) {
carrot = false;
extraPrice -= 0.20;
}
}
public String getName() {
return burgerName;
}
public double getPrice() {
switch (breadrollType) {
case HamburgerContract.BR_TYPE_HONEY: extraPrice += 0.25; break;
case HamburgerContract.BR_TYPE_SESAM: extraPrice += 0.50; break;
}
switch (meatType) {
case HamburgerContract.MEAT_TYPE_COW: extraPrice += 0; break;
case HamburgerContract.MEAT_TYPE_CHICKEN: extraPrice += 0.50; break;
}
return basePrice + extraPrice;
}
}
看看switch语句。@JacobG。你能添加一个与我的代码类似的例子吗?这会对我有很大帮助。为什么你的一些声明会打印信息而不是返回?为什么你的物品价格不是固定不变的?为什么不必要地将总选择数乘以100?为什么您的基准价格是一个实例变量?为什么只有一个类以小写字母开头?如果人们订购无限量的物品会发生什么?你可能想考虑在你的代码上发布反馈。