Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
142 lines (120 sloc) 3.46 KB
import edu.ucdavis.jr.*;
import java.util.*;
import java.util.ArrayList;
public class Landlord extends Mixologist {
private op void doorsReplyChannel();
private op void alarm();
private int numDoorCalls = 0;
public Landlord()
{
super("Landlord");
Global.clock.setAlarmEndTime(alarm);
Global.clock.setAlarm(alarm, new MyTime(Global.simulationTimeInSeconds-600));
}
protected boolean shouldGoHome()
{
return numDoorCalls >= 2;
}
protected void oneIteration()
{
inni void alarm()
{
if(numCalls++ == 0)
{
say("Last call!");
}
else
{
say("Closing time!");
}
List<Person> entrants = Global.door.lock(doorsReplyChannel); // first call actually isnt lock
for(Person p : entrants)
{
send p.tellLastCallOrClosing();
}
}
[] void doorsReplyChannel()
{
if(numDoorCalls++ == 0)
{
// tell assistant to do last round
say("Telling assistant to do last round");
send Global.assistant.onlyTwoLeft();
}
else
{
gatherStatistics();
/*
* We have some problems here:
*
* For some reason the landlord will stop execution after shutdown()
* of the clock, even though the call is ASYNCHRONUS!
*
* We assume this is a JR bug and ignores this for now because
* we have no clue for the cause of it, and it's not so relevant
* for message passing etc.
*
*/
say("About to shutdown clock");
send Global.clock.shutdown();
say("Shutdowned clock");
}
}
[] boolean Global.orderingChannel(Beverage beverage)
{
return mixDrink(beverage);
}
}
private void gatherStatistics()
{
//Gather statistics here!
say("=>\tGathering statistics\n");
List<Integer> l = Customer.getWaitTimes();
// int maxx = 0;
// int minn = Integer.MAX_VALUE ;
int sum = 0;
for(Integer a : l)
{
sum += a;
}
int f = 1;
if(l.size() > 0)
{
say("Number of served customers: " + (numServings+Global.barmaid.numServings) + "\n");
say("Maximum waiting time: " + Collections.max(l)*f + " seconds.");
say("Minimum waiting time: " + Collections.min(l)*f + " seconds.");
say("Average waiting time: " + (sum*f/l.size()) + " seconds.");
}
else
{
say("Nobody ordered anything tonight :(");
}
boolean ok = true;
for(Table t: Global.tables)
{
if(!t.clearTable().isEmpty())
{
say("Table " + t + " isnt empty!");
ok = false;
}
}
if(ok)
{
say("All tables are empty, wohoo!");
}
for(Item it : Global.allItemsList)
{
int count = 0;
for(Item it2 : Global.cupboard.getItems())
{
if(it == it2) count++;
}
int diff = count;
for(Item it2 : Global.cupboard.getOriginalItems())
{
if(it == it2) diff--;
}
say("Item " + it + " was found " + count + " times in the cupboard. The difference is " + diff);
}
}
}