【发布时间】:2018-08-15 21:30:00
【问题描述】:
作为我学习过程的一部分,我正在用 Java 创建一个小程序,该程序将帮助一家虚构的公司帮助销售汉堡。
在下面的代码中,我为汉堡创建了类,并添加了选项以向该汉堡添加添加物(如生菜、胡萝卜等)。
我面临的问题是如何在不过多使用 if-else 的情况下检查基本汉堡包(基本汉堡包仅表示面包和肉)的每个额外添加。我尝试的一种方法(您也可以在代码中看到)是为每个添加分配一个数字,例如,1 到生菜,2 个胡萝卜等等。通过添加 1 和 2 我们得到 3;我们可以寻找 3 来确定是否添加了生菜和胡萝卜,以便我们可以计算价格。我也用其他选项做到了这一点。
但是出现了一些问题:
-
在某些情况下,通过加法生成的数字会从不同的加法中创建两次,这可以通过将数字乘以某个数字或在创建更多边缘情况时加 1 来解决。
通过创建此类案例,我的主要问题是它需要大量 if-else 语句,我试图避免这些语句,以便有效地编写我需要的代码。
请建议是否有其他方法可以做到这一点。请注意代码尚未完成,因为我不想创建更多 if-else 语句(在汉堡类中;方法 cal_price)来检查添加内容,但足以完全理解代码的性质。代码如下:
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;
}
}
【问题讨论】:
-
看看
switch声明。 -
@JacobG。您能否添加一个类似于我的代码的示例?这对我有很大帮助。
-
为什么您的某些 get 语句打印信息而不是返回信息?为什么您的商品价格不是静态常量?为什么你不必要地将总选择乘以
100?为什么你的底价是一个实例变量?为什么只有一个类以小写字母开头?如果人们订购的商品数量不限,会发生什么? -
您可能需要考虑在CodeReview StackExchange 上发帖以获取有关您代码的反馈。
标签: java oop optimization