0

I've got two classes, second onne extends the first one, there are couple of overridden methods and a constructor with all of the variables working through super().

public class Hamburger {

private Meat meat;
private Roll roll;
private double price;
private double total;
private String additionPrices;

private Addition addition;

public Hamburger(Meat meat, Roll roll, double price) {
    this.meat = meat;
    this.roll = roll;
    this.price = price;
}

public void getTotal(Addition addition1, Addition addition2, Addition addition3, Addition addition4){
    this.additionPrices = ("First addition - " + addition1.getName() + " costs " + addition1.getPrice() + "\n" +
                          "Second addition - " + addition2.getName() + " costs " + addition2.getPrice() + "\n" +
                          "Third addition - " + addition3.getName() + " costs " + addition3.getPrice() + "\n" +
                          "Fourth addition - " + addition4.getName() + " costs " + addition4.getPrice());
    this.total = this.price + addition1.getPrice() + addition2.getPrice() + addition3.getPrice() + addition4.getPrice();
}

public void getTotal(Addition addition1, Addition addition2, Addition addition3){
    this.additionPrices = ("First addition - " + addition1.getName() + " costs " + addition1.getPrice() + "\n" +
            "Second addition - " + addition2.getName() + " costs " + addition2.getPrice() + "\n" +
            "Third addition - " + addition3.getName() + " costs " + addition3.getPrice());
    this.total = this.price + addition1.getPrice() + addition2.getPrice() + addition3.getPrice();

}

public void getTotal(Addition addition1, Addition addition2){
    this.additionPrices = ("First addition - " + addition1.getName() + " costs " + addition1.getPrice() + "\n" +
            "Second addition - " + addition2.getName() + " costs " + addition2.getPrice());
    this.total = this.price + addition1.getPrice() + addition2.getPrice();

}

public void getTotal(Addition addition1){
    this.additionPrices = ("First addition - " + addition1.getName() + " costs " + addition1.getPrice());
    this.total = this.price + addition1.getPrice();

}

public void getTotal(){
    this.additionPrices = ("No additions selected");
    this.total = this.price;

}


public void showPrice(){
    System.out.println("Base price equals: " + this.price);
    System.out.println("Total price equals: " + this.total);
    System.out.println(this.additionPrices);
}

And the second class:

public class HealthyBurger extends Hamburger {

private Meat meat;
private Roll roll;
private double price;
private double total;
private String additionPrices;

private Addition addition;

public HealthyBurger(Meat meat, Roll roll, double price) {
    super(meat, roll, price);
}



public void getTotal(Addition addition1, Addition addition2, Addition addition3, Addition addition4, Addition addition5, Addition addition6){
    this.additionPrices = ("First addition - " + addition1.getName() + " costs " + addition1.getPrice() + "\n" +
            "Second addition - " + addition2.getName() + " costs " + addition2.getPrice() + "\n" +
            "Third addition - " + addition3.getName() + " costs " + addition3.getPrice() + "\n" +
            "Fourth addition - " + addition4.getName() + " costs " + addition4.getPrice() + "\n" +
            "Fifth addition - " + addition5.getName() + " costs " + addition5.getPrice() + "\n" +
            "Sixth addition - " + addition6.getName() + " costs " + addition6.getPrice());
    this.total = this.price + addition1.getPrice() + addition2.getPrice() + addition3.getPrice() + addition4.getPrice() + addition5.getPrice() + addition6.getPrice();

}

public void getTotal(Addition addition1, Addition addition2, Addition addition3, Addition addition4, Addition addition5){
    this.additionPrices = ("First addition - " + addition1.getName() + " costs " + addition1.getPrice() + "\n" +
            "Second addition - " + addition2.getName() + " costs " + addition2.getPrice() + "\n" +
            "Third addition - " + addition3.getName() + " costs " + addition3.getPrice() + "\n" +
            "Fourth addition - " + addition4.getName() + " costs " + addition4.getPrice() + "\n" +
            "Fifth addition - " + addition5.getName() + " costs " + addition5.getPrice());
    this.total = this.price + addition1.getPrice() + addition2.getPrice() + addition3.getPrice() + addition4.getPrice() + addition5.getPrice();

}
@Override
public void getTotal(Addition addition1, Addition addition2, Addition addition3, Addition addition4){
    super.getTotal(addition1,addition2,addition3,addition4);

}
@Override
public void getTotal(Addition addition1, Addition addition2, Addition addition3){
   super.getTotal(addition1,addition2,addition3);

}
@Override
public void getTotal(Addition addition1, Addition addition2){
    super.getTotal(addition1,addition2);

}
@Override
public void getTotal(Addition addition1){
    super.getTotal(addition1);

}
@Override
public void getTotal(){
    super.getTotal();

}

@Override
public void showPrice(){
    System.out.println("Base price equals: " + this.price);
    System.out.println("Total price equals: " + this.total);
    System.out.println(this.additionPrices);

}

When I'm using method showPrice from the Hamburger class it works just fine, however with HealthyBurger it's giving me results like this:

Base price equals: 0.0
Total price equals: 0.0
null

It's happening when i'm using one of the overridden getTotal methods with 0-4 additions. When I'm using one of the two methods which are not overridden (5 or 6 additions) then it gives me detailed results for addition prices but it's still not taking base price from the super constractor.

Should I not use overriding here? Should I change the constructor and assign price manually instead of using super()?

I've got a feeling that super constrictor is not assigning the value locally which is causing the base price to come up as zero but i'm not sure why is it not working.

Thanks

CowOO
  • 31
  • 1
  • 6
  • 1
    So much unnecessary duplication of code in your post. Why not use a single method that uses a varargs parameter? – DontKnowMuchBut Getting Better Aug 09 '20 at 12:21
  • If this were my program, I'd use a collection of Additions: `private List additions = new ArrayList<>();` and then within the getTotal() simply iterate through the list using a for loop or stream. This would work for the parent class and any and all child classes the same. – DontKnowMuchBut Getting Better Aug 09 '20 at 12:24
  • Thanks for advices, I'll keep that in mind. Im a beginner doing some online courses and I didnt get to lists yet. – CowOO Aug 09 '20 at 12:31
  • You should remove the private variables from HealthyBurger class. You currently have 2 copies for price, one inherited from Burger and another one in HealthyBurger. If you need acces, you can make the fields in Burger protected instead of private. – Hitobat Aug 09 '20 at 12:43

0 Answers0