0
public class TimsOrder {

    private int size;
    private String name;
    private static TimsProduct[] items;

    private TimsOrder(String name, int size) {
        this.name = name;
        this.size = size;
    }

    @Override
    public double getRetailPrice(){
        return price;
    }

    private static void orderItem(TimsProduct item) {
        Donut chocolate = Donut.create();
        item = chocolate;
    }

    public static TimsOrder create() {
        items = new TimsProduct[size];
        for (int i = 0; i < items.length; i++) {
            orderItem(items[i]);
        }
        TimsOrder order = new TimsOrder("OrderName", 1); //Where 1 is the # of items
    }

    public double getAmountDue() {
        double total = 0;
        System.out.println("Testpoint");
        for (int i = 0; i < items.length; i++) {
            total = total + (((TimsProduct) items[i]).getRetailPrice()); //Line with issue
        }
        return total;
    }
}

public abstract class TimsProduct extends Commodity {
    private String name;
    private double cost;
    private double price;

    @Override
    public double getRetailPrice(){
        System.out.println("Testpoint2");
        return price;
    }
}

public class Donut extends TimsProduct {
    private String description;
    private int calorieCount;

    private Donut(String name, String description, double cost, double price, int calorieCount) {
        super(name, cost, price);
        this.description = description;
        this.calorieCount = calorieCount;
    }

    public static Donut create() {
        Donut chocolate = new Donut("Chocolate", "Glazed", 0.30, 0.99, 500);
        return chocolate;
    }
}

Test Code:

TimsOrder t = TimsOrder.create();
System.out.println(t);
System.out.printf("Total Price: $%.2f\n", t.getAmountDue());

I realize that t.items has 0 values which is the problem here. What I do not know is why these values are not there.

If anyone wants to see the files: Commodity.java https://pastebin.com/raw/9sWbDWV8

TimsProduct.java extends commodity https://pastebin.com/raw/jzgfkd0P

TimsOrder.java https://pastebin.com/raw/vc0VtDq6

Donut.java extends TimsProduct https://pastebin.com/raw/w7iEQG1H

Sri
  • 2,281
  • 2
  • 17
  • 24
  • It is a duplicate sorry. I have edited that post but it is still on hold or something. Initially my question was worded poorly. – Sri Apr 07 '18 at 21:01
  • The problem has nothing to do with calling method from inherited class. You pass `items[i]` to `orderItem` and probably expect `items[i]` to be set there by `item = chocolate`. But it only changes the value of the `item` variable. It does not assign value to the element of array which you passed. – lexicore Apr 07 '18 at 21:08
  • TimsOrder class does't has any parents and not implemented any interfaces but have overriden method, why? – Igor S Apr 07 '18 at 21:09
  • @lexicore Thank you! I think this is the issue. I will modify it and see where it goes. Cheers! I simply passed the entire array and the location to orderItem and it works :) – Sri Apr 07 '18 at 21:14

0 Answers0