Restaurant - Parts 1,2,3
public class Food {
public String food;
static int orders = 0;
protected double startTime;
private int id;
//protected so it can be accessed through the extended classes
public Food(String food) {
this.food = food;
orders++; //increases the orders since its static
this.startTime = System.nanoTime(); //starts the time when the food is prepared
this.id = orders; //unique id is the order
}
public int getId() {
return id;
}
public String toString() { //uses toString method to get the order
return "Restaurant order: " + food + "\n Order Number: " + id;
}
public int getOrders() {
return orders;
}
public double waitingTime() {
return (System.nanoTime() - startTime);
} //returns the waiting time after food is prepared to when its picked up
public boolean goneBad() {
return (this.waitingTime() >= 4 * 100000000);
} //food has gone bad once time is equal to ~ 1 hr
public static void main(String[] args){
System.out.println("Part 1:");
System.out.println("~~~~~~~~~~~");
Food[] foods = {
new Food("Pasta"),
new Food("Tacos"),
new Food("Bagels")
}; //puts all the foods into an array for simplicity
for(Food food1 : foods){
System.out.println(food1);
} //enhanced for loop to print out all foods
System.out.println("\nNumber of Total Orders: " + orders);
}
}
Food.main(null);
Part 2 & 3
- Ensure Novel and Textbook run the Constructor from Book.
- Create instance variables unique to Novel has Author, Textbook has publishing company. New items are not required by Constructor.
- Make Getters and Setters for all new items. For instance, be able to set items not required by constructor.
- Add a time when book entered the library. This should be same for Parent and Subclasses.
- Define tester method to test all items
Build a Tester Method that does a Simulation. Define a default method in Book that returns "current shelfLife" from date/time of construction. (Hint, think about capturing time in sorts) Define shelfLife expiration methods as needed in TextBook and Novel. A TextBook has a fixed shelf life based on the date/time of constructor. (Hint, simulation 3 yrs as 3000 milliseconds) A Novel has a computed shelf life of expiration, the simulation should extend shelf life if a certain number of return stamps where placed in the book. (Hint, 3 return stamps renews for an extra year) Use a sleep in Java to assist with simulation Make a method that determines if book has shelf life remaining, try to have title and on/off status in output.
class Takeout extends Food {
private String orderName;
private int pickups;
Takeout(String food) {
super(food); //calls super from the Food class
this.pickups = 0; //sets the pickups amount to 0
}
@Override
public String toString() {
return "Restaurant order: " + food + "\n Pickup Order Name: " + orderName;
//overrides to return the order name
}
public String getOrderName() {
return orderName;
}
//setter and getter for order name
public void setOrderName(String orderName) {
this.orderName = orderName;
}
//number of orders picked up
public void pickups() {
pickups++;
this.startTime = System.nanoTime();
//starts the time at the pickup
}
public int getPickups() {
return this.pickups; //returns pickups
}
}
class Dine extends Food {
private String dineIn;
Dine(String food) {
super(food);
} //constructor for dine in people
public String getDineIn() {
return dineIn;
} //gets the name of the person
public void setDineIn(String dineIn) {
this.dineIn = dineIn;
} //setter for dine in
@Override
public String toString() {
return "Restaurant order: " + food + "\n Dine in Order Name: " + dineIn;
//overrides to return the order name
}
}
public class Owner{
public static void main(String[] args){
System.out.println("Part 2:");
System.out.println("~~~~~~~~~~~");
Takeout[] takeouts = {
new Takeout("Burrito"),
new Takeout("Chicken Curry"),
new Takeout("Vegetarian Noodles")
}; //puts all the foods into an array for simplicity
Dine[] diners = {
new Dine("Salad"),
new Dine("Pizza"),
new Dine("Gobi Manchurian")
}; //puts all the foods into an array for simplicity
takeouts[0].setOrderName("Riya");
takeouts[1].setOrderName("Vidhi");
takeouts[2].setOrderName("Vika");
diners[0].setDineIn("Riya");
diners[1].setDineIn("Vidhi");
diners[2].setDineIn("Vika");
//sets all the takeout order names and diner names
for(Takeout take : takeouts){
System.out.println(take);
System.out.println("Waiting Time: " + take.waitingTime());
System.out.println();
} //enhanced for loop to print out all takeout orders
for(Dine dine : diners){
System.out.println(dine);
System.out.println("Waiting Time: " + dine.waitingTime());
System.out.println();
} //enhanced for loop to print out all diners
System.out.println("Part 3:");
System.out.println("~~~~~~~~~~~");
Takeout t = new Takeout("Burgers");
Dine d = new Dine("Soup");
//checks if gone bad
System.out.println("Has it gone bad? " + t.goneBad());
System.out.println("Has it gone bad? " + d.goneBad());
try{
Thread.sleep(1000); //sleep in java to forward time and check
} catch (Exception e) {
Thread.currentThread().interrupt();
}
System.out.println();
System.out.println("After some time...");
System.out.println();
//checks if gone bad after time skip
System.out.println("Has it gone bad? " + t.goneBad());
System.out.println("Has it gone bad? " + d.goneBad());
}
}
Owner.main(null);