Skip to content

Instantly share code, notes, and snippets.

@kchodorow
Created March 10, 2015 15:33
Show Gist options
  • Save kchodorow/37d06b90313bb2320ae2 to your computer and use it in GitHub Desktop.
Save kchodorow/37d06b90313bb2320ae2 to your computer and use it in GitHub Desktop.
OOP practice
public class Restaurant {
public static void main(String args[]) throws InterruptedException {
Kitchen kitchen = new Kitchen();
while (true) {
Order meal = kitchen.checkForFinished();
System.out.println("Finished: " + meal);
// Maybe send a new order to the kitchen.
double rand = Math.random();
if (rand < .1) {
kitchen.placeOrder(new Pasta());
} else if (rand < .2) {
kitchen.placeOrder(new Burger());
} else if (rand < .3) {
kitchen.placeOrder(new Pizza());
} else if (rand < .4) {
kitchen.placeOrder(new Sushi());
}
kitchen.timePasses();
}
}
}
/**
* ----------------------------------------------
* Only modify code between the line above and the matching line below.
*
* Step 1: Fill in the TODOs below. What happens when there are more orders than Frank can handle?
* Step 2: Create a SushiChef that is a subclasses of chef and can make sushi 2x faster than a Chef.
* Step 3: Make "chef" an array of chefs instead of a single one.
*/
class Kitchen {
Chef chef;
int time;
public Kitchen() {
chef = new Chef("Frank");
time = 0;
}
/**
* Returns a finished Order, or null.
*/
public Order checkForFinished() {
// TODO
System.out.println("Checking with " + chef + " if any orders are up.");
return null;
}
/**
* Sets the chef's current order, if they're not already busy.
*/
public void placeOrder(Order order) {
// TODO
System.out.println("Placing a new order [" + order + "] with " + chef);
}
/**
* Modifies timeLeft for any order(s) in progress.
*/
public void timePasses() throws InterruptedException {
// TODO
System.out.println("Current time: " + time++);
Thread.sleep(2000);
}
}
/**
* Do not modify anything below this line!
* ----------------------------------------
*/
class Chef {
String name;
Order currentOrder;
public Chef(String name) {
this.name = name;
}
public String toString() {
return name;
}
}
class Order {
String description;
int timeLeft;
public boolean isDone() {
return timeLeft == 0;
}
public String toString() {
return description + " (" + timeLeft + ")";
}
}
class Pasta extends Order {
public Pasta() {
description = "Pasta";
timeLeft = 2;
}
}
class Burger extends Order {
public Burger() {
description = "Burger";
timeLeft = 1;
}
}
class Pizza extends Order {
public Pizza() {
description = "Pizza";
timeLeft = 3;
}
}
class Sushi extends Order {
public Sushi() {
description = "Sushi";
timeLeft = 6;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment