Java please. Hamburger Class Write a Hamburger class with the following six attr
ID: 3716905 • Letter: J
Question
Java please.
Hamburger Class Write a Hamburger class with the following six attributes: bun-the type of bun the burger will be on patties- the number of patties on the burger cheese- whether or not the burger will have cheese price the price of the burger toppings- an array of five toppings; it will need to start empty Write getters/setters for all attributes except the toppings array. Your constructor should create a single on white with no cheese and no toppings with a price of 1.99. Your class should include the following methods in this order, please: addCheese- this method has no parameters that adds cheese to the burger and increases the price by 50 cents superSizeIt- this method has no parameters and adds a patty to the burger and increases the price by 1.00 addToppings- this method would present the user with at least five toppings (your choice) stored in an array of no change in price burgerDetails this method prints the details of the burger order. It should include whether or not it has cheese, the number of patties, the type of bun, the toppings and the final price Driver Class Write a driver class to test your Hamburger class. Assume you have two visitors to your burger joint. Use your creativity to welcome the customers and take their order. Create an instance of the hamburger class for each customer. For each customer do the following: ask if he would like cheese, if so, call the addcheese method ask if he would like to super size the meal, if so, call the superSizeIt method ask what type of bun he would like and pass that to the setter for your bun attribute call your addToppings method call your burgerDetails method to display the details of the burger order Be sure to compile and run your application. Take a screenshot of your successful test run.Explanation / Answer
Here is the required classes Hamburger and Driver, implemented as per the needs. Everything is explained in comments. Let me know if you have any doubts. Thanks.
// Hamburger.java
import java.util.Scanner;
public class Hamburger {
/**
* attributes
*/
private String bun;
private int patties;
private boolean cheese;
private double price;
private String[] toppings;//array to store chosen toppings
private int toppingsCount;//count of chosen toppings
private static String[] toppingsAvailable = { "tomatoes", "red onions",
"spinach", "pepper", "Olives" };// array of available toppings
public Hamburger() {
/**
* creating a hamburger with white bun, 1 patty, no cheese, empty
* toppings and price of 199
*/
bun = "white";
patties = 1;
cheese = false;
toppings = new String[5];
toppingsCount = 0;
price = 1.99;
}
// getters and setters
public String getBun() {
return bun;
}
public void setBun(String bun) {
this.bun = bun;
}
public int getPatties() {
return patties;
}
public void setPatties(int patties) {
this.patties = patties;
}
public boolean hasCheese() {
return cheese;
}
public void setCheese(boolean cheese) {
this.cheese = cheese;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
/**
* method to add cheese to the burger
*/
public void addCheese() {
cheese = true;
price += 0.50;
}
/**
* method to super size the hamburger
*/
public void superSizeIt() {
patties += 1;
price += 1.00;
}
/**
* method to display the topppings menu and let the user choose
*/
public void addToppings() {
Scanner scanner = new Scanner(System.in);
for (String s : toppingsAvailable) {
System.out.print("Do you want to add " + s + " topping? (y/n): ");
if (scanner.nextLine().equalsIgnoreCase("y")) {
toppings[toppingsCount] = s;
toppingsCount++;
}
}
}
/**
* method to display the burger details
*/
public void burgerDetails() {
System.out.println("...BURGER DETAILS...");
System.out.println("Bun type: " + bun);
System.out.println("Number of patties: " + patties);
System.out.println("Has cheese? : " + cheese);
System.out.print("Toppings: ");
for (int i = 0; i < toppingsCount; i++) {
System.out.print(toppings[i]);
if (i != toppingsCount - 1) {
System.out.print(", ");
}
}
System.out.printf(" Price: $%.2f ", price);
}
}
// Driver.java
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
/**
* creating an array of two hamburgers for two customers
*/
Hamburger hamburgers[] = new Hamburger[2];
Scanner scanner = new Scanner(System.in);
/**
* Reading user input, creating specified hamburger, displaying the burger details
*/
for (int i = 0; i < hamburgers.length; i++) {
System.out.println(" Hamburger for customer: " + (i + 1));
hamburgers[i] = new Hamburger();
System.out.print("Do you want to add cheese? (y/n): ");
if (scanner.nextLine().equalsIgnoreCase("y")) {
hamburgers[i].addCheese();
}
System.out.print("Do you want to super size the meal? (y/n): ");
if (scanner.nextLine().equalsIgnoreCase("y")) {
hamburgers[i].superSizeIt();
}
System.out.print("Enter the bun type: ");
hamburgers[i].setBun(scanner.nextLine());
hamburgers[i].addToppings();
hamburgers[i].burgerDetails();
}
}
}
/*OUTPUT*/
Hamburger for customer: 1
Do you want to add cheese? (y/n): y
Do you want to super size the meal? (y/n): y
Enter the bun type: Kaiser roll
Do you want to add tomatoes topping? (y/n): n
Do you want to add red onions topping? (y/n): y
Do you want to add spinach topping? (y/n): n
Do you want to add pepper topping? (y/n): y
Do you want to add Olives topping? (y/n): n
...BURGER DETAILS...
Bun type: Kaiser roll
Number of patties: 2
Has cheese? : true
Toppings: red onions, pepper
Price: $3.49
Hamburger for customer: 2
Do you want to add cheese? (y/n): n
Do you want to super size the meal? (y/n): n
Enter the bun type: Potato roll
Do you want to add tomatoes topping? (y/n): y
Do you want to add red onions topping? (y/n): n
Do you want to add spinach topping? (y/n): n
Do you want to add pepper topping? (y/n): n
Do you want to add Olives topping? (y/n): y
...BURGER DETAILS...
Bun type: Potato roll
Number of patties: 1
Has cheese? : false
Toppings: tomatoes, Olives
Price: $1.99
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.