如何避免在此代码中使用if-else?

时间:2018-08-15 21:30:01

标签: java oop optimization

作为学习过程的一部分,我正在用Java创建一个小程序,该程序将帮助虚构的公司销售汉堡。

在下面的代码中,我为一个汉堡包创建了一个类,并添加了一些选项以向汉堡包添加其他食物(如生菜,胡萝卜等)。

我面临的问题是如何在不过多使用if-else的情况下检查基本汉堡包(基本汉堡包仅指面包和肉类)中的每一个额外添加物。我尝试的一种方法(您也可以在代码中看到)是为每个添加项分配一个数字,例如,生菜1,胡萝卜2等。将1和2相加得到3;我们可以寻找3来查找是否添加了生菜和胡萝卜,以便我们可以计算价格。我也用其他选择做到了这一点。

但是出现了一些问题:

  1. 在某些情况下,通过加法生成的数字是由不同的加法创建的两次,这可以通过将数字乘以某个数字或在我创建更多边沿案例时加1来解决。

    < / li>
  2. 通过创建此类案例,我的主要问题是,它将需要大量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;
    }
}

3 个答案:

答案 0 :(得分:0)

您对“汉堡包”对象的实现似乎有些复杂。通常,面向对象的程序设计代表实际的物理对象,应选择最简单,最原子的属性,以避免出现此类情况。一个汉堡包有或没有生菜。它有或没有番茄。我的建议是为每个浇头使用单独的布尔变量,或者根据需要使用布尔数组表示浇头。然后,将get_price()方法添加到您的Hamburger中,该方法只有一个if语句块用于计算价格。如果您真的想加入OOP,则每个浇头都可以是一个带有价格的对象,您可以将该价格附加到汉堡包的Toppings ArrayList上。然后,您的get_price()方法将使用for-each来总计Topping对象的所有价格以及汉堡包的price属性。如果您想要多个相同的浇头,则此方法可能更好。这是编程中最有趣的部分-您可以选择最适合自己的实现,然后尝试一下!

答案 1 :(得分:0)

制作一个具有名称,面包类型,肉类类型的汉堡包,并在以后添加额外的汉堡包,您也可以对面包类和肉类进行单独的选择。在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;

} 
}

答案 2 :(得分:-1)

您根本不需要使用条件语句,只需将每个商品的价格乘以其价格即可:

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
    }
}