I need help writing this program in Java, any help is greatly appriciated :) You
ID: 3738211 • Letter: I
Question
I need help writing this program in Java, any help is greatly appriciated :)
You will be writing software to support Tom’s Diner. Tom’s Diner has the following menu:
Drinks:
Coffee 99 cents
Small soda (12 oz) 99 cents
Large soda (18 oz) $1.50
Side dishes
Salad $1.99
Green Beans $1.99
Rice $1.99
Entrees
8 oz Steak 99 cents an ounce
12 oz Steak 99 cents an ounce
16 oz Steak $1.20 an ounce
Chicken Breast $3.99 each
Desserts
Cake $1.99 a piece
Ice Cream by the scoop 99 cents per scoop
Sundae (Ice cream by the scoop + toppings)
All items for sale at the diner will implement the Restaurant interface
The getPrice() method – returns the cost of an item in cents, as an integer
The setPrice(int) method – sets the cost of an item in cents, as an integer
The getName() methods – returns the name of the item
The Item Class : Represents a basic menu item with no options, such as a side dish or a drink like a cup of coffee
Properties
price – an int to hold the cost of the item in cents
name – A String to hold the name of the item
Methods
Item(String, int) : overloaded constructor
Accessors and Mutators : Add logic to ensure name isn’t blank, and price is a number > 0
toString() : Return a String representation of the item
The SizeableItem Class (inherits from the Item class)
This is used to create customizable items like an 8 ounce steak, 12 ounce steak, or a 1 scoop or 2 scoop ice cream, or a drink (12 oz Soda, 16 oz Soda, or 24 oz Soda, etc.). In the case of the constructor, the price will be the price per item (price per ounce of steak, or price per scoop of ice cream). The parent setPrice(int) method will need to be called to update the price based on the quantity * pricePerQuantity. For instance, creating a sizeable item for an 8 oz. steak that costs 99 cents per ounce would be done like this:
SizeableItem eightOzSteak = new SizeableItem(“8 oz. Steak”, 99, 8);
In the constructor, the code would need to call the parent constructor with the name and the pricePerQuantity * the quantity, and set the quantity.
Properties
quantity : an integer to represent how many or what size of the item the customer wants
Methods
SizeableItem(String, int, int) : overloaded constructor
Accessors and Mutators : Add logic to ensure quantity is a number > 0
The SizeableToppingsItem Class (inherits from the SizeableItem class)
This is used to create customizable SizeableItem with extra toppings, such as a scoop of Ice Cream or a Sundae (or perhaps a Steak with peppers and onions). In the case of the constructor, the parameters are all the same as the parent class. After the object is created, toppings can be added through the addToppings method. Each topping added will update the price by adding
the TOPPING_COST to the price. Toppings can be removed through the removeToppings method. Any toppings removed will have the TOPPING_COST removed from the price.
Properties
TOPPING_COST : a constant integer to represent how much a single topping costs. Set to 50 cents
toppings : an ArrayList of toppings that have been added to this item. The ArrayList holds values from the Topping enumeration (see diagram).
Methods
SizeableToppingsItem(String, int, int)
getToppings() : return the ArrayList of toppings added to this item
addTopping(Topping) : Add a Topping from the Topping enum to the toppings ArrayList and update the price
removeTopping(Topping) : Remove a Topping from the toppings ArrayList and update the price
The Order class
This is the driver of your application. Your application should initialize all variables necessary, and loop at least once, asking the user to add items from the menu to their order, and customize any items as necessary (add toppings to a Sundae, for instance).
Once the customer has completed the order, the application should calculate the total, then calculate the taxes. The Tax amounts should be rounded to the nearest cent (For example, calculating the tax on
a food item with a cost of 199 cents at a rate of 3% should return 6 cents – use Math.round()). Calculate the total after taxes, print out the receipt for the order, the total due for the order before and after taxes, and collect “payment”. Once payment is collected, the change should be returned, the order cleared, and the next customer should be able to start a new order, unless the user chooses to exit.
All money should be displayed in the correct format of $##.## (example: $22.18). See the UML diagram for suggested static methods in addition to main.
For a small bonus, suggest a 15% tip, an 18% tip, or a custom tip, and give the user the option of tipping the server.
Explanation / Answer
package Grocerary;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.print.attribute.standard.PrinterLocation;
interface Restaurant{
int getPrice();
void setPrice(int val);
String getName();
}
class Item implements Restaurant{
int price;
final String itemName;
Item(int price,String name){
this.price=price;
this.itemName=name;
}
public String toString()
{
return "Item Name: "+this.itemName+" ";
}
@Override
public int getPrice() {
return this.price;
}
@Override
public void setPrice(int val) {
this.price=val;
}
@Override
public String getName() {
return itemName;
}
}
class SizeableItem extends Item
{
int quantity;
public SizeableItem(String name,int pricePerItem,int quantity)
{
super(pricePerItem*quantity,name);
}
public void setQuantity(int qt)
{
this.quantity=qt;
}
}
enum Toppings {
IceCreamByTheScoop(99), Sundae(99);
private int value;
private Toppings(int value) {
this.value = value;
}
public int getPrice() {
return value;
}
}
class SizeableToppingsItem extends SizeableItem
{
private final static int TOPPING_COST=50;
private List<Integer> toppings=new ArrayList<Integer>();
public SizeableToppingsItem(String name,int pricePerItem,int quantity)
{
super(name,pricePerItem,quantity);
/*for(Toppings t: Toppings.values()){
toppings.add(t.getPrice());
}*/
}
public void addToppings(Toppings t)
{
toppings.add(t.getPrice());
setPrice(getPrice()+t.getPrice()+TOPPING_COST);
}
public void removeToppings(Toppings t)
{
toppings.remove(t.getPrice());
setPrice(getPrice()-(t.getPrice()+TOPPING_COST));
}
}
public class Order {
public static void main(String[] args) {
List<Item> items=new ArrayList<Item>();
System.out.println("Below is Menu...Add items from it");
System.out.println("************-----------------****************");
System.out.println("##########Drinks########");
System.out.println("Item:1 - Coffee-99cents");
System.out.println("Item:2 - Small soda (12 oz)-99cents");
System.out.println("Item:3 - Large soda (18 oz)-$1.50");
System.out.println("##########Side dishes########");
System.out.println("Item:4 - Salad -$1.99");
System.out.println("Item:5 - Green Beans-$1.99");
System.out.println("Item:6 - Rice-$1.99");
System.out.println("##########Entrees########");
System.out.println("Item:7 - 8 oz Steak-99 cents an ounce");
System.out.println("Item:8 - 12 oz Steak-99 cents an ounce");
System.out.println("Item:9 - 16 oz Steak-$1.20 an ounce");
System.out.println("Item:10 - Chicken Breast-$3.99");
System.out.println("##########Desserts########");
System.out.println("Item:11 - Cake-$1.99 a piece");
System.out.println("Item:12 - Ice Cream by the scoop-99 cents per scoop");
System.out.println("Item:13 - Sundae-99 cents per scoop");
char ch;
Scanner scan =new Scanner(System.in);
do{
System.out.println("Select an item ");
int no=scan.nextInt();
switch(no)
{
case 1: Item coffee=new Item(99,"Coffee");
items.add(coffee);
break;
case 2: SizeableItem smallSoda = new SizeableItem("Small soda (12 oz)", 99, 12);
items.add(smallSoda);
break;
case 3: SizeableItem largeSoda = new SizeableItem("Large soda (18 oz)", 150, 18);
items.add(largeSoda);
break;
case 4: Item itm4=new Item(199,"Salad");
items.add(itm4);
break;
case 5: Item itm5=new Item(199,"Green Beans ");
items.add(itm5);
break;
case 6: Item itm6=new Item(199,"Rice");
items.add(itm6);
break;
case 7: SizeableItem eightOzSteak = new SizeableItem("8 oz Steak", 99, 8);
items.add(eightOzSteak);
break;
case 8: SizeableItem twelveOzSteak = new SizeableItem("12 oz Steak", 99, 12);
items.add(twelveOzSteak);
break;
case 9: SizeableItem OzSteak = new SizeableItem("16 oz Steak", 99, 12);
items.add(OzSteak);
break;
case 10:Item itm10=new Item(399,"Chicken Breast");
items.add(itm10);
break;
case 11:SizeableItem cake = new SizeableItem("Cake", 199, 1);
items.add(cake);
break;
case 12:SizeableItem icecream = new SizeableItem("Ice Cream by the scoop", 99, 1);
items.add(icecream);
break;
case 13:SizeableItem sd = new SizeableItem("Sundae", 99, 1);
items.add(sd);
break;
default:System.out.println("Invalid item");
}
System.out.println("You want to add more item...(y/n)");
ch = scan.next().charAt(0);
}while(ch == 'y');
int total=0,noOfItems=0;
for (Item item : items) {
total+=item.getPrice();
noOfItems++;
}
System.out.println("No- "+"Item Name"+"-------"+"Price");
int count=1;
for (Item item : items) {
System.out.println(count+" "+item.getName()+" "+item.getPrice());
}
System.out.println(noOfItems+" "+"Total"+"-------"+total+"cents");
}
}