In: Computer Science
Please write a main method that creates 2 pizzas and uses the toString() method to test this program. public class PizzaOrder { private Pizza[] order; private int numPizzas; public PizzaOrder() { order = new Pizza[1]; order[0] = new Pizza("Cheese", 0); numPizzas = 1; } public PizzaOrder(int count) { order = new Pizza[count]; this.numPizzas = 0; } public void addPizza(Pizza pizza) { for (int i = 0; i < order.length; i++) { if (order[i] == null) { order[i] = pizza; System.out.println("Pizza added successfully"); numPizzas += 1; return; } } System.out.println("Pizza could not be added, unsuccess"); } public double calcTotal() { double totalPrice = 0.0; int index = 0; while (order[index] != null && index < order.length) { totalPrice += order[index++].calcCost(); } return totalPrice; } @Override public String toString() { return "Total Pizza Ordered : " + numPizzas + ", Total order price: $" + calcTotal(); } }
public class PizzaOrder {
private Pizza[] order;
private int numPizzas;
public PizzaOrder() {
order = new
Pizza[1];
order[0] = new
Pizza("Cheese", 0);
numPizzas = 1;
}
public PizzaOrder(int count) {
order = new
Pizza[count];
this.numPizzas =
0;
}
public void addPizza(Pizza pizza) {
for (int i = 0; i
< order.length; i++) {
if (order[i] == null) {
order[i] = pizza;
System.out.println("Pizza added successfully");
numPizzas += 1;
return;
}
}
System.out.println("Pizza could not be added, unsuccess");
}
public double calcTotal() {
double totalPrice =
0.0;
int index = 0;
while (order[index] !=
null && index < order.length) {
totalPrice += order[index++].calcCost();
}
return totalPrice;
}
@Override
public String toString() {
return "Total Pizza
Ordered : " + numPizzas + ", Total order price: $" +
calcTotal();
}
}
public class Main {
public static void main(String args[]) {
Pizza pizza1 = new Pizza("Cheese",
10);
Pizza pizza2 = new Pizza("ABC",
20);
PizzaOrder order = new
PizzaOrder(2);
order.addPizza(pizza1);
order.addPizza(pizza2);
System.out.println(order);
}
}