Question

In: Computer Science

Please write a main method that creates 2 pizzas and uses the toString() method to test...

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();
    }
}

Solutions

Expert Solution

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);
   }
}


Related Solutions

/* Work to do: 1) add toString method to BankAccount and SavingsAccount classes 2) Override the...
/* Work to do: 1) add toString method to BankAccount and SavingsAccount classes 2) Override the withdraw() in SavingsAccount so that it will not withdraw more money than is currently in the account. 3) Provide constructors for SavingsAccount 4) Add this feature to SavingsAccount: If you withdraw more than 3 times you are charged $10 fee and the fee is immediately withdrawn from your account.once a fee is deducted you get another 3 free withdrawals. 5) Implement the Comparable Interface...
Please implement Sample string toString()method for each class and return itself a string, not the output....
Please implement Sample string toString()method for each class and return itself a string, not the output. import java.util.ArrayList; public class Customer extends User{ private ArrayList orders; public Customer(String display_name, String password, String email) { super(display_name, password, email); } @Override public String getPermissionLevel() { return "CUSTOMER"; } public void addOrder(Order order){ this.orders.add(order); } public ArrayList listOrders(){ return this.orders; }; } ---------------- public class ElectronicProduct extends Product{ private long SNo; private String warranty_period; public ElectronicProduct(long SNo, String warranty_period, String productId, String productName,...
Define Loan Class – Add to your project. And write program in main method to test...
Define Loan Class – Add to your project. And write program in main method to test it. Note: Assume year is number of years, rate is the annual interest rate and P is principle is loan amount, then the total payment is Total payment = P *(1+ rate/12)^ year*12; Monthly Payment = TotalPayment/(year*12); java
Please write in Python code please Write a program that creates a dictionary containing course numbers...
Please write in Python code please Write a program that creates a dictionary containing course numbers and the room numbers of the rooms where the courses meet. The dictionary should have the following key-value pairs: Course Number (key) Room Number (value) CS101 3004 CS102 4501 CS103 6755 NT110 1244 CM241 1411 The program should also create a dictionary containing course numbers and the names of the instructors that teach each course. The dictionary should have the following key-value pairs: Course...
Error: Main method is not static in class ArrayReview, please define the main method as: public...
Error: Main method is not static in class ArrayReview, please define the main method as: public static void main(String[] args) please help me fast: import java.util. Random; import java.util.Scanner; //ArrayReview class class ArrayReview { int array[];    //constructor ArrayReview (int n) { array = new int[n]; //populating array Random r = new Random(); for (int i=0;i<n; i++) array[i] = r.nextInt (); } //getter method return integer at given index int getElement (int i) { return array[i]; }    //method to...
Java - Write a test program that creates an Account object with an account number of...
Java - Write a test program that creates an Account object with an account number of AC1111, a balance of $25,000, and an annual interest rate of 3.5. Use the withdraw method to withdraw $3,500, use the deposit method to deposit $3,500, and print the balance, the monthly interest, and the date when this account was created.
Write a static method called max that creates a Scanner and connects to a file “data.txt”....
Write a static method called max that creates a Scanner and connects to a file “data.txt”. Use an exception handling  mechanism of your choice. The method should read the file until the end of time and consume tokens that could be integer,  double or string type. It should count and return the number of words that are not integer or doubles.
T or F The main reason a research uses a t-test instead of a z-test is...
T or F The main reason a research uses a t-test instead of a z-test is when little is known about population (mean and standard deviation). Values in the t‐table are not actually listed by sample size but by degrees of freedom (df). A 90 percent confidence level is equivalent to an alpha level of 0.10 The critical value of t‐table for a study with 6 participants with alpha set @ .1 one tail is 1.711
Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these...
Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these 2 Arrays are equal or not, and also if the elements are all even numbers. Describe under what conditions these 2 Arrays would be considered equal.
Write a java method that creates a two dimensional char array after asking the user to...
Write a java method that creates a two dimensional char array after asking the user to input a String text (for example, "Sara" which is entered by the user)  and String key consisting of integers (for example, 2314) only, such that int rows=(int)Math.ceil(text.length()/key.length())+1; int columns= key.length(); The method fills the 2d array with letters a String entered by the use (column by column). The method then shifts the columns of the array based on key. For example, if the user enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT