-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAssistant.java
104 lines (62 loc) · 2.57 KB
/
Assistant.java
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
import java.util.concurrent.atomic.AtomicInteger;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;
public class Assistant implements Runnable {
ThriftStore tf;
AtomicInteger tick;
Dictionary<String, Integer> inventory= new Hashtable<>();
int daysToRun;
public Assistant(ThriftStore tf, AtomicInteger tick, int daysToRun){
this.tf = tf;
this.tick = tick;
this.daysToRun = daysToRun;
//Initialize Inventory to 0 for every section
for(int i = 0; i<6; i++){
inventory.put(tf.sections[i],0);
}
}
public void unload() {
//Only allow one assistant to access the delivery...This is necessary for multiple assistants.
synchronized (tf.delivery) {
if(tf.isDelivered == false){
return;
}
System.out.printf("<%d> <%s> Assistant Grabbed Delivery\n",tick.get(), Thread.currentThread().getId());
// Iterate through delivery and collect items to add to the assistant's inventory
List<String> itemsToAdd = new ArrayList<>();
for (String item : tf.delivery) {
if (itemsToAdd.size() < 10) {
itemsToAdd.add(item);
} else {
// Break if the inventory is full
break;
}
}
// Copy items from the list to the assistant's inventory
for (String item : itemsToAdd) {
inventory.put(item, inventory.get(item) + 1);
}
// Remove items from delivery
tf.delivery = new String[10];
tf.isDelivered = false;
}
}
@Override
public void run(){
while(true){
int tickToPerformAction = (1000 * daysToRun);
//This works, however it is definitly not optimal as it wastes CPU cycles?
while (tick.get() < tickToPerformAction) {
//check if there is a delivery
if((tf.isDelivered == true)){
//if there is a delivery, we want to unload
unload();
//After adding those items to our inventory, we can begin to stock
tf.stock(inventory, tick);
}
}
}
}
}