In java code, I have been given the task to create a computer system to take ord
ID: 3714948 • Letter: I
Question
In java code, I have been given the task to create a computer system to take orders from a pizza restaurant. You need to store the following information about the pizzas: name, array of ingredients (maximum 5), crust type, and price. The price of a pizza is calculated with a base price of $2 and $0.50 cents for each ingredient. You are going to start by implementing the class Pizza that will contain the following:
• Attributes, based on the description above.
• Default constructor
• Constructor that receives only the name of the pizza.
• Setter methods for the attributes name, crust type and price
• Getter methods for the attributes name, crust type and price.
• Method called addIngredient() that receives an ingredient at a time and stores it in the array of ingredients. Make sure you keep track of the ingredients you have so far so it does not exceed 5.
• Method called calculatePrice() that returns the total price of the pizza based on the number of ingredients.
Once you create the Pizza class do the Order class. The Order class will contain the following.
• Attributes: name of client, array of Pizza(capacity 10), and the total price.
• Default constructor.
• Constructor that only receives the name of the client
• Setter method for the name of the client
• Getter method for the name of the client and the total price.
• Create a method called addPizza() that receives an object of type Pizza and stores it in the array of Pizza. Make sure you keep track of the number of pizzas added.
• Create a method called calculateTotal() that calculates the total amount to be paid based on the pizzas ordered. This method should return the total price which includes an 8.25% of taxes
In your Order class include a main method that executes the following intructions:
1. Ask the client for his/her name
2. Create an object of type Order
3. Ask the client if they would like to order a pizza, if the client answers yes ask for the ingredients and add the pizza to the order.
4. Repeat step 2 until the client answers no.
5. When the client answers no, calculate the total and display the following message: “Thank you! Your total is ____” (add the calculated total to this message)
Explanation / Answer
Hello. I have created the required classes Pizza and Order (with main method) as needed, with comments explaining everything. Drop a comment if you have any doubts. Thanks.
// Pizza.java
public class Pizza {
/**
* attributes
*/
private String name;
private String ingredients[];
private int ingredientsCount;
private String crustType;
private double price;
/**
* constructor with no arguments
*/
public Pizza() {
name = "";
ingredients = new String[5];
ingredientsCount = 0;
crustType = "";
price = 0;
}
/**
* constructor with one argument
*/
public Pizza(String name) {
this.name = name;
ingredients = new String[5];
ingredientsCount = 0;
crustType = "";
price = 0;
}
/**
* getters and setters
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getIngredients() {
return ingredients;
}
public void setIngredients(String[] ingredients) {
this.ingredients = ingredients;
}
public int getIngredientsCount() {
return ingredientsCount;
}
public void setIngredientsCount(int ingredientsCount) {
this.ingredientsCount = ingredientsCount;
}
public String getCrustType() {
return crustType;
}
public void setCrustType(String crustType) {
this.crustType = crustType;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
/**
* method to add an ingredient
*/
public void addIngredient(String ing) {
if (ingredientsCount < ingredients.length) {
ingredients[ingredientsCount] = ing;
ingredientsCount++;
} else {
//array is full
System.out.println("Maximum number of ingredients reached");
}
}
/**
* method to calculate total price of this pizza
*/
public double calculatePrice() {
double basePrice = 2.0;
double ingredientsPrice = 0.5;
price = basePrice + (ingredientsPrice * ingredientsCount);
return price;
}
}
// Order.java
import java.util.Scanner;
public class Order {
/**
* attributes
*/
private String name;
private Pizza[] pizzas;
private int pizzaCount;
private double totalPrice;
/**
* constructor with no arguments
*/
public Order() {
name = "";
pizzas = new Pizza[10];
totalPrice = 0;
pizzaCount = 0;
}
/**
* constructor with one argument
*/
public Order(String name) {
this.name = name;
pizzas = new Pizza[10];
totalPrice = 0;
pizzaCount = 0;
}
/**
* getters and setters
*/
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public double getTotalPrice() {
return totalPrice;
}
/**
* method to add a pizza to the order
*/
public void addPizza(Pizza p) {
if (pizzaCount < pizzas.length) {
pizzas[pizzaCount] = p;
pizzaCount++;
} else {
System.out.println("Maximum number of pizzas reached");
}
}
/**
* method to calculate total order price including tax
*/
public double calculateTotal() {
double tax = 8.25;
totalPrice = 0;
/**
* adding up prices of all pizzas
*/
for (int i = 0; i < pizzaCount; i++) {
totalPrice += pizzas[i].calculatePrice();
}
// adding tax
totalPrice += totalPrice * (tax / 100.0);
return totalPrice;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
/**
* getting client name
*/
String name = scanner.nextLine();
Order order = new Order(name);
boolean moreOrders = true;
String input;
/**
* looping until user wishes to stop
*/
while (moreOrders) {
System.out.print("Do you want to add a pizza? (yes/no): ");
input = scanner.nextLine();
if (input.equalsIgnoreCase("yes")) {
/**
* Creating and adding a pizza
*/
System.out.print("Enter pizza name: ");
input = scanner.nextLine();
Pizza pizza = new Pizza(input);
System.out.print("Enter crust type: ");
input = scanner.nextLine();
pizza.setCrustType(input);
/**
* loop until user wishes to stop adding ingredients
*/
do {
System.out.print("Enter an ingredient "
+ "(or enter 'done' to finish): ");
input = scanner.nextLine();
if (!input.equalsIgnoreCase("done")) {
pizza.addIngredient(input);
}
} while (!input.equalsIgnoreCase("done"));
order.addPizza(pizza);
} else {
//end of loop
moreOrders = false;
}
}
/**
* Displaying the stats
*/
System.out.println(" Thank you " + order.getName() + "!");
System.out.println("Your total is $" + order.calculateTotal());
}
}
/*OUTPUT*/
Enter your name: Alice
Do you want to add a pizza? (yes/no): yes
Enter pizza name: pepperoni
Enter crust type: cheese
Enter an ingredient (or enter 'done' to finish): pepper
Enter an ingredient (or enter 'done' to finish): olives
Enter an ingredient (or enter 'done' to finish): ginger
Enter an ingredient (or enter 'done' to finish): garlic
Enter an ingredient (or enter 'done' to finish): tomato sauce
Enter an ingredient (or enter 'done' to finish): some more
Maximum number of ingredients reached
Enter an ingredient (or enter 'done' to finish): done
Do you want to add a pizza? (yes/no): yes
Enter pizza name: Chicken Pizza
Enter crust type: cheese+chicken
Enter an ingredient (or enter 'done' to finish): chicken
Enter an ingredient (or enter 'done' to finish): ginger
Enter an ingredient (or enter 'done' to finish): done
Do you want to add a pizza? (yes/no): no
Thank you Alice!
Your total is $8.11875
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.