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

Write a Java test program, all the code should be in a single main method, that...
Write a Java test program, all the code should be in a single main method, that prompts the user for a single character. Display a message indicating if the character is a letter (a..z or A..Z), a digit (0..9), or other. Java's Scanner class does not have a nextChar method. You can use next() or nextLine() to read the character entered by the user, but it is returned to you as a String. Since we are only interested in the...
/* 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,...
Write a program that contains a main method and another method named userName. The main method...
Write a program that contains a main method and another method named userName. The main method should prompt the user to enter a full name. The userName method takes the full name as an argument and prints the name in reverse order and returns the number of characters in the name. See Sample Output (input shown in blue). Sample Output Please enter your FULL name Billy Joe McCallister Here is the name Billy Joe McCallister in reverse: retsillaCcM eoJ ylliB...
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
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...
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...
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.
PLEASE USE ARRAYS IN JAVA TO ANSWER THIS Write a 'main' method that examines its command-line...
PLEASE USE ARRAYS IN JAVA TO ANSWER THIS Write a 'main' method that examines its command-line arguments and calls the (add) method if the first parameter is a "+" calls the (subtract) method if the first parameter is a "-" calls the (doubled) method if the first parameter is a "&" add should add the 2 numbers and print out the result. subtract should subtract the 2 numbers and print out the results. Double should add the number to itself...
TwoNumbersAddTo 1. write a class called TwoNumbersAddTo with a main method 2. the program asks for...
TwoNumbersAddTo 1. write a class called TwoNumbersAddTo with a main method 2. the program asks for a number (lets call it "goal"). 3. then the program reads at most 20 integers (i.e. any number of integers from 0 to 20) 4. the program stops reading when user enters 0 or user has entered 20 numbers. 5. program prints all the pairs that add up to goal. 6. Notice that if goal is 5, for example, 1 + 4 and 4...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT