-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomer.jr
135 lines (122 loc) · 5.05 KB
/
Customer.jr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import edu.ucdavis.jr.*;
import java.util.*;
/**
* A customer is a person that will continously order beverages
* and drink them.
*/
public class Customer {
/* Allows each customer a unique ID */
private static int counter = 0;
/* Customer instance variables */
private Pub.Drink preferredDrink;
private Table preferredTable;
private boolean running;
private int id;
private int numDrinks;
private Container currentDrink;
private MyTime orderTime;
private boolean lastCallInterrupt = false;
/* Private operation references used to interact with the pub */
private cap void(cap void(Container), Pub.Drink) order;
private cap void(Customer) goodbye;
private cap void(MyTime) stats;
/* Operations allowing other parts of the pub to interact with the customer */
public op void lastCall();
public op void finishedDrink();
public op void leftDrink();
private op void getOrder(Container);
/**
* Constructor requires three channels (the first for ordering drinks, the second for saying goodbye
* to the landlord and the last to send statistics on waiting time) and a reference to their favorite table.
*/
public Customer(cap void(cap void(Container), Pub.Drink) order,
cap void(Customer) goodbye,
cap void(MyTime) stats,
Table table) {
Random random = new Random();
this.order = order;
this.goodbye = goodbye;
this.stats = stats;
int rand = random.nextInt(Pub.Drink.values().length);
this.preferredDrink = Pub.Drink.values()[rand];
this.running = true;
this.numDrinks = 1 + random.nextInt(3);
this.preferredTable = table;
this.id = ++Customer.counter;
}
/**
* Customer process that allows a customer to continously drink and order drinks until
* the pub closes or they are done for tonight.
*/
public process run {
// enter pub and say greeting phrase!
List<String> greetings = new ArrayList<String>();
greetings.add("Good to see you, old chap!");
greetings.add("Greetings, friends!");
greetings.add("Hello Pub!");
System.out.println("Customer " + this.id + ": " + greetings.get(new Random().nextInt(greetings.size())));
this.getDrink();
while (this.running) {
inni void finishedDrink() {
// If last call interrupt, skip one finishedDrink message
if (this.lastCallInterrupt) {
this.lastCallInterrupt = false;
continue;
}
// Put down glass/cup and either get another one or go home.
this.leaveContainer();
if(this.numDrinks == 0) {
this.running = false;
} else {
this.getDrink();
}
}
[] void lastCall() {
// When it's time for last call, beer drinkers will drink up and order a new one.
if(this.numDrinks > 0 && this.preferredDrink == Pub.Drink.BEER) {
this.lastCallInterrupt = true;
this.leaveContainer();
this.getDrink();
}
}
[] void getOrder(Container c) st this.running {
if (c == null) {
System.out.println("Customer " + this.id + ": Oh, that's to bad. Guess I'll head home.");
this.running = false;
continue;
}
// Receive a container with their beverage of choice.
this.currentDrink = c;
this.numDrinks--;
// Send order time to statistics.
MyTime time = new MyTime(Pub.getClock().getTime().compareTo(this.orderTime));
System.out.println("Customer " + this.id + ": Thank you! I waited " + time + ".");
send this.stats(time);
// Set timer for drinking
send Pub.getClock().setAlarm(this.finishedDrink, new MyTime(0, 10, 0));
}
}
// Say goodbye to landlord and everyone else. Time to go home.
send this.goodbye(this);
System.out.println("Customer " + this.id + ": I bid you all adiue!");
}
/**
* Put down the container on table immediately if possible, otherwise do it once
* the assistant says the table has been cleared.
*/
private void leaveContainer() {
// put down container
if(this.preferredTable.putDown(this.leftDrink, this.currentDrink) == false) {
receive this.leftDrink();
}
this.currentDrink = null;
}
/**
* Order another dri-hic!-nk.
*/
private void getDrink() {
System.out.println("Customer " + this.id + ": I would like a " + this.preferredDrink + ", please.");
send this.order(getOrder, this.preferredDrink);
this.orderTime = Pub.getClock().getTime();
}
}