Skip to content

Instantly share code, notes, and snippets.

@Zolomon
Created January 7, 2015 15:53
Show Gist options
  • Select an option

  • Save Zolomon/2cae24e8f5c306826659 to your computer and use it in GitHub Desktop.

Select an option

Save Zolomon/2cae24e8f5c306826659 to your computer and use it in GitHub Desktop.
Temporary exam answer for the burger joint simulator
public class Simulation {
public static void Main(String[] args) {
Monitor mon = new Monitor();
ArrayList<Chef> chefs = new ArrayList<Chef>();
ArrayList<Customer> customers = new ArrayList<Customer>();
ArrayList<Seller> sellers = new ArrayList<Seller>();
Customers simulator = new Customers();
Statistics statistics = new Statistics();
for(int i = 0; i < 7; i++) {
chefs.add(new Chef());
chefs[i].start();
}
for (int i = 0; i < 3; i++) {
sellers.add(new Seller());
sellers[i].start();
}
simulator.start();
statistics.start();
}
}
public class Monitor {
private LinkedList<Integer> customers;
private LinkedList<Integer> orders;
private LinkedList<Integer>[] shelfs;
private int[] inProgress;
private int thrownBurgers;
private int missedDeadlines;
private int orderedBurgers;
public Monitor() {
shelfs = new LinkedList<Integer>[Util.shelfs()];
customers = new LinkedList<Integer>(); // requested orders?
orders = new LinkedList<Integer>(); // order
}
// Simulate a chef manufacturing a burger
public synchronized void manufactureBurger() {
}
// Simulate a seller handling an order
public synchronized void handleOrder() {
// 1. Wait for new customer
try {
while (customers.size() == orders.size()) {
wait();
}
} catch (InterruptedException e) {
return;
}
// 2. Place order in chefs
orders.addLast(customers.getLast());
orderedBurgers++;
// 3. While waiting for chefs to prepare burgers, throw old ones
int currentTime = Util.currentTime();
int waitLength = currentTime + Util.manufacturingTime(customers.getLast());
while (waitLength - currentTime >= 0) {
}
}
// Placing a new customer in queue
public synchronized void newCustomer(int burgerType) {
customers.add(burgerType);
notifyAll();
}
// Return a vector containing {thrownBurgers,missedDeadlines,orderedBurgers}
public synchronized int[] gatherStatistics() {
int[] stats = new int[3];
stats[0] = thrownBurgers;
stats[1] = missedDeadlines;
stats[2] = orderedBurgers;
thrownBurgers = 0;
missedDeadlines = 0;
orderedBurgers = 0;
return stats;
}
}
public class Customer extends Thread {
//LinkedList<Integer> customers;
Monitor mon;
public Customer(Monitor mon) {
this.mon = mon;
//customers = new LinkedList<Integer>();
}
public void run() {
while(!isInterrupted) {
int type = Util.newCustomer();
//customers.add(type);
mon.newCustomer(type);
}
}
}
public class Seller extends Thread {
Monitor mon;
public Seller(Monitor mon) {
this.mon = mon;
}
public void run() {
while(!isInterrupted) {
mon.handleOrder();
}
}
}
public class Chef extends Thread {
Monitor mon;
public Chef(Monitor mon) {
this.mon = mon;
}
public void run() {
while(!isInterrupted) {
mon.manufactureBurger();
}
}
}
public class Statistics extends Thread {
Monitor mon;
public Statistics(Monitor mon) {
this.mon = mon;
}
public void run() {
while(!isInterrupted) {
int[] stats = mon.gatherStatistics();
synchronized(stats) {
System.out.println("Thrown burgers: " + stats[0].toString());
System.out.println("Missed deadlines: " + stats[1].toString());
System.out.println("Ordered burgers: " + stats[2].toString());
System.out.println("");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment