-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssistant.jr
84 lines (72 loc) · 2.72 KB
/
Assistant.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
import edu.ucdavis.jr.*;
import java.util.*;
/**
* The assistant sweeps the tables and does the dishes.
*/
public class Assistant {
/** Process running or not */
private boolean running;
/** Knows about the serving area */
private ServingArea servingArea;
/** Knows about the tables */
private List<Table> tables;
/** Has a tray to put dirty dishes on */
private List<Container> tray;
/** Channel for listening for final round message from landlord */
public op void finalRound();
/**
* Initialize assistant process!
*/
public Assistant(ServingArea servingArea, List<Table> tables) {
this.running = true;
this.servingArea = servingArea;
this.tables = tables;
this.tray = new ArrayList<Container>();
}
/**
* Assistant does table sweeps with breaks in between until final round is
* signaled from the landlord. Then performs final sweep and goes home.
*/
public process run {
while (this.running) {
inni void finalRound() {
System.out.println("Assistant: All is quiet. Making my final sweep!");
this.sweep();
this.running = false;
} else {
this.sweep();
System.out.println("Assistant: Time for my break.");
JR.nap(Pub.ASSISTANT_BREAK);
}
}
System.out.println("Assistant: Done for tonight. Time to head home.");
}
/**
* Goes between all tables and picks up dirty dishes. Assistant then
* cleans dirty dishes and puts it pack in the cupboard.
*/
private void sweep() {
System.out.println("Assistant: Time for my sweep.");
// put dirty dish on tray
List<Container> tmp;
for (Table table : this.tables) {
tmp = table.clean();
JR.nap(Pub.ASSISTANT_HANDLE_CONTAINER * tmp.size());
this.tray.addAll(tmp);
JR.nap(Pub.ASSISTANT_WALK_BETWEEN_TABLES);
}
// clean dirty dishes
JR.nap(Pub.ASSISTANT_CLEAN_CONTAINER * this.tray.size());
// put back clean dishes
for (Container container : this.tray) {
if (container instanceof Glass) {
this.servingArea.getCupboard().getGlassAllocator().release((Glass) container);
} else if (container instanceof Cup) {
this.servingArea.getCupboard().getCupAllocator().release((Cup) container);
}
JR.nap(Pub.ASSISTANT_HANDLE_CONTAINER);
}
System.out.println("Assistant: Collected and washed " + this.tray.size() + " containers");
this.tray = new ArrayList<Container>();
}
}