0

I have a class that is abstract, which also has 4 child classes, I would like to know how could I create a object array so when the program runs, it will ask the user if they want to place another order and store the first order in the object array and so on till the user does not want to place anymore orders.

double TP = b1.getTotal() + s1.getTotal() + d1.getTotal() + dr.getTotal();
System.out.print("Would you like to place another order?");
user = scan.nextInt();
scan.nextLine();
}
}while(user == 1);

//here is where I would like to create the object array and store these variables in them,

 b1.display();
    s1.display();
    d1.display();
    dr.display();

here is the abstract class

import java.util.*;
public abstract class Order
{

protected String meal;
protected double price, total;
protected int  amount;
public Order()
{
setItem(meal);

}
public String getItem()
{
return meal;
}

public  void setItem(String i)
{
meal = i;
}
public double getPrice()
{
return price;
}

public  void setPrice(double p)
{
price = p;
}
public int getAmt()
{
return amount;
}

public  void setAmt(int a)
{
amount = a;
}
public double getTotal()
{
return total;
}


public abstract void display();

public abstract void setTotal(double p, int a);

}
3
  • Please give your code some format so it can be more readable. Also, select the code that addresses your problem and don't paste you're entire program Commented Dec 1, 2012 at 0:06
  • Where is the abstract class? Commented Dec 1, 2012 at 0:08
  • updated my original post Commented Dec 1, 2012 at 0:15

1 Answer 1

2

I assume your abstract class is Order. Something like

abstract class Order {
   public abstract void getSomething();
   public abstract void setSomething();
}

class Burger extends Order {
  // implement getSomething() and setSomething()
}

...
List<Order> orders = new ArrayList<Order>();
...
Burger b = new Burger();
b.setSomething(0);
orders.add(b);
...
for (Order o : orders) {
   o.getSomething();
}

is likely what you're looking for.

Sign up to request clarification or add additional context in comments.

5 Comments

I don't get how i would be able to add a method from each child class? EX: Burgers class has a display(), and Sides has a display(), how would I add all of the child methods so that if the user wants to place another order, it will store the first and the second orders then display each order.
@user1067332 -- You have to actually write code to "store the first and second orders the display each other".
Thanks this works for the most part, but I would want it to calculate the total per order as well as total for all of the orders. I am confused on how I would do this?
Keep two totals. When you finish the per-order total, add it to the master total, or just add everything to both totals but reset the per-order total at the start of an order and leave the master total.
okay, but because i have 4 child classes, when i try to print them using the for loop Dave provided with adding the Total, it prints it on every line. But I want it to print the total after each order then the master total of all the orders at the end. example: Burger 3.99 Fries 1.99 total: 5.98 next order Burger 3.99 Fires 1.99 total: 5.98 master total: 11.96

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.