Please I am trying to get the items of an order, the items are coffee muffin and timBits, by using the method
public String toString()
Every thing is working properly except that I do not get the items of the order I get null for them instead of the following:
Muffin "Bran" ,3
Coffee "Latte" , 1
TimBits "Assorted" , 24
Muffin "Chocolate" , 1
Coffee "Decaf" , 2
TimBits "Chocolate" , 12
Muffin "PeanutButter" , 2
Muffin "Blueberry" , 5
the numbers above represents the quantity of each item in the order.
class Item
{
protected String description;
protected int quantity;
protected String kind;
private double cost;
public double getCost()
{
return this.cost;
}
public Item (String description, int quantity)
{
this.description = description;
this.quantity = quantity;
}
public String toString()
{
return "Item: " + " " +kind + " " + ": description: " + " " +description +"quantity:" +" " + quantity ;
}
class Coffee extends Item
{
protected double cost1Coffee;
String kind = "Coffee";
public Coffee (String description, int quantity)
{
super(description, quantity);
cost1Coffee = 4 ;
}
}
}
class Muffin extends Item
{
protected double cost1Muffin;
protected double cost2Muffin;
protected double cost3Muffin;
String kind = "Muffin";
public Muffin (String description, int quantity)
{
super(description,quantity);
cost1Muffin = 1;
cost2Muffin = 0.75;
cost3Muffin = 0.50;
}
}
class TimBits extends Item
{
protected double cost1TimBits ;
String kind = "TimBits";
public TimBits (String description, int quantity)
{
super(description, quantity);
cost1TimBits = 0.25;
}
}
/***************************************************************/
/***************************************************************/
class A4Q1Util
{
private static ArrayList<Item> order;
private static int count = 0;
public static Item getItem()
{
Item item;
if (order==null)
{
order = new ArrayList<Item>();
order.add(new Muffin("Bran", 3));
order.add(new Coffee("Latte", 1));
order.add(new TimBits("Assorted", 24));
order.add(new Muffin("Chocolate", 1));
order.add(new Coffee("Decaf", 2));
order.add(new TimBits("Chocolate", 12));
order.add(new Muffin("PeanutButter", 2));
order.add(new Muffin("Blueberry", 5));
}
item = null;
if (count<order.size())
{
item = order.get(count);
count++;
}
{
return item;
}
}
}
output:
Item: null : description: Branquantity: 3
Item: null : description: Lattequantity: 1
Item: null : description: Assortedquantity: 24
Item: null : description: Chocolatequantity: 1
Item: null : description: Decafquantity: 2
Item: null : description: Chocolatequantity: 12
Item: null : description: PeanutButterquantity: 2
Item: null : description: Blueberryquantity: 5
Program completed normally.