vendredi 10 août 2018

If statement in constructor not working as intended? Java [on hold]

this is the constructor I am using in my parent class:

    public Base(String breadType, String meat) {
    this.breadType = breadType;
    this.meat = meat;

    if(!this.breadType.equals("baguette")|| !this.breadType.equals("brown rye")) {
        this.additionalCost+= 1.99;
        System.out.println("Bread neither baguette or brown rye.");
    }
    if(!this.breadType.equals("beef") || !this.breadType.equals("low-fat meat")) {
        this.additionalCost+= 2.99;
        System.out.println("Meat neither beef or low-fat meat.");

    }


    this.finalCost = baseCost+additionalCost;

}

So to clarify, the logic I am trying to create is that it checks if the input was either 'baguette' or 'brown rye'. If it is NOT either of these, then the additionalCost field will be incremented by 1.99. A similar logic has been implemented for the type of meat being inputted.

I have a child class with the following Constructor:

    public Healthy_Burger() {
        super("brown rye","low-fat meat");
        //addCost(this.baseCost-super.getBaseCost());
    }

Unless I have understood super() in constructors incorrectly, I would have thought that the arguments I entered would be then passed in the base Constructor 'Base' which is followed by the if statements, which then checks if the bread or meat is not the ones it is looking for and if it isn't, it should increment additionalCost with the respective value.

Ultimately, my issue is that when I instantiate a 'Healthy_Burger' like so:

    Healthy_Burger burger = new Healthy_Burger();

I even used getters to ensure the breadType is 'brown rye', but the issue I am having is that upon checking the final Cost, I am seeing that the additionalCosts of both the breadType and Meat are being added, even though they aren't meant to because the bread type is 'brown rye' and the meat 'low-fat meat' and my if statement should therefore not return true.

Thank you.

Aucun commentaire:

Enregistrer un commentaire