In java laguage (Eclipse: Neon) create an application that takes and prints fast
ID: 3814972 • Letter: I
Question
In java laguage (Eclipse: Neon) create an application that takes and prints fast food orders using inheritance and polymorphism. You will implement the following 5 classes:
FoodItem : Superclass containing the common states and behaviors of Hamburger, Pizza and MilkShake
Member variables:
numberOfItems : int (Static) - Hold the number of food items that have been created.
double: price
Member methods:
FoodItem(double) - Constructor that sets the price of the item.
double getPrice() - Getter for price variable.
print() - void method that doesn't do anything. (virtual method)
Hamburger : Subclass of FoodItem
Member variables:
cheese: boolean
Member methods:
Hamburger(boolean) : Constructor that sets the cheese variable and calls super(3.50).
print() : void method that prints the hamburger order.
Pizza : Subclass of FoodItem
Member variables:
toppings: String
Member methods:
Pizza(String) : Constructor that sets the toppings and calls super(7.75).
print() : void method that prints the pizza order.
Milkshake : Subclass of FoodItem
Member variables:
flavor: int
Member methods:
Milkshake(int) : Constructor that sets the flavor of the milkshake and calls super(2.25).
print() : void method that prints the milkshake order.
Assignment4 : Driver class containing the main method.
Your main method will do the following:
1) Ask the user for the number of items being ordered.
2) Create an array of FoodItems with that number of items.
3) For each item to be ordered: ask which type of item, ask for the details of the order, create the object, and store it in the array.
4) Print a summary of the order.
5) Print the order total as closely as possible to the Sample I/O below.
Sample I/O:
Enter number of items: 3
Order menu: 1-Cheeseburger, 2-Pizza, 3-Milkshake
What is your order for item 1: 1
Would you like cheese? 1-Yes, 2-No : 1
Order menu: 1-Cheeseburger, 2-Pizza, 3-Milkshake
What is your order for item 2: 2
What would you like on your pizza? : Pineapple and Onion
Order menu: 1-Cheeseburger, 2-Pizza, 3-Milkshake
What is your order for item 3: 3
Flavor? 1-Vanilla, 2-Chocolate, 3-Strawberry : 1
Order Summary:
You ordered 3 items.
1) (3.50) Hamburger +cheese
2) (7.75) Pizza +Pineapple and Onion
3) (2.25) Milkshake +Vanilla
Order Total: $13.50
Explanation / Answer
FoodItem.java
public abstract class FoodItem {
public static int numberOfItems=0;
double price;
public FoodItem(double price)
{
this.price=price;
}
double getPrice()
{
return price;
}
public abstract void print();
}
______________
Hamburger.java
public class Hamburger extends FoodItem {
private boolean cheese;
public Hamburger(boolean cheese)
{
super(3.50);
this.cheese=cheese;
}
@Override
public void print() {
if(cheese==true)
System.out.println(++numberOfItems+") ("+getPrice()+")"+"Hamburger+cheese");
else
System.out.println(++numberOfItems+") ("+getPrice()+")"+"Hamburger");
}
}
____________________
Pizza.java
public class Pizza extends FoodItem {
private String toppings;
public Pizza(String toppings)
{
super(7.75);
this.toppings=toppings;
}
@Override
public void print() {
System.out.println(++numberOfItems+") ("+getPrice()+")"+"Pizza "+toppings);
}
}
________________
Milkshake.java
public class Milkshake extends FoodItem {
private int flavor;
public Milkshake(int flavor) {
super(2.25);
this.flavor = flavor;
}
@Override
public void print() {
if(flavor==1)
System.out.println(++numberOfItems+") ("+getPrice()+")"+"MilkShake + Vanilla ");
else if(flavor==2)
System.out.println(++numberOfItems+") ("+getPrice()+")"+"MilkShake + Chocolate ");
else if(flavor==3)
System.out.println(++numberOfItems+") ("+getPrice()+")"+"MilkShake + Strawberry ");
}
}
__________________
Driver.java
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
int no_of_items;
Hamburger h=null;
Pizza p=null;
Milkshake ms=null;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of Items :");
no_of_items=sc.nextInt();
int item_num;
FoodItem foodItems[]=new FoodItem[no_of_items];
for(int i=0;i<no_of_items;i++)
{
System.out.println("Order menu: 1-Cheeseburger, 2-Pizza, 3-Milkshake");
System.out.print("What is your order for item "+(i+1)+":");
item_num=sc.nextInt();
switch(item_num)
{
case 1:{
int choice;
System.out.print("Would you like cheese? 1-Yes, 2-No :");
choice=sc.nextInt();
if(choice==1)
h=new Hamburger(true);
else if(choice==2)
h=new Hamburger(false);
foodItems[i]=h;
break;
}
case 2:{
String toppings;
sc.nextLine();
System.out.print("What would you like on your pizza? : ");
toppings=sc.nextLine();
p=new Pizza(toppings);
foodItems[i]=p;
break;
}
case 3:{
int flav;
System.out.println("Flavor? 1-Vanilla, 2-Chocolate, 3-Strawberry :");
flav=sc.nextInt();
ms=new Milkshake(flav);
foodItems[i]=ms;
break;
}
default:{
System.out.println("Invalid.Must be 1 or 2 or 3");
continue;
}
}
}
double totalPrice=0.0;
for(int i=0;i<3;i++)
{
foodItems[i].print();
totalPrice+=foodItems[i].getPrice();
}
System.out.println("Order Total: $"+totalPrice);
}
}
____________________
Output:
Enter the number of Items :3
Order menu: 1-Cheeseburger, 2-Pizza, 3-Milkshake
What is your order for item 1:1
Would you like cheese? 1-Yes, 2-No :1
Order menu: 1-Cheeseburger, 2-Pizza, 3-Milkshake
What is your order for item 2:2
What would you like on your pizza? : Pineapple and Onion
Order menu: 1-Cheeseburger, 2-Pizza, 3-Milkshake
What is your order for item 3:3
Flavor? 1-Vanilla, 2-Chocolate, 3-Strawberry :
1
1) (3.5)Hamburger+cheese
2) (7.75)Pizza Pineapple and Onion
3) (2.25)MilkShake + Vanilla
Order Total: $13.5
_______________thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.