Repository navigation
Expand file tree
/
Copy pathAgentsQueue.java
More file actions
123 lines (93 loc) · 2.91 KB
/
Copy pathAgentsQueue.java
File metadata and controls
123 lines (93 loc) · 2.91 KB
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
import helpers.RandomNumberGenerator;
import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.Queue;
public class AgentsQueue {
private int arrivalRate;
private int timeToNextEvent;
private int maxLength;
private int allTimeLength;
private Queue<Agent> agents;
private int resignedAgents;
private AgentsQueue() {
this.maxLength = -1;
this.allTimeLength = 0;
this.agents = new ArrayDeque<>();
this.resignedAgents = 0;
}
public AgentsQueue(int arrivalRate) {
this();
this.arrivalRate = arrivalRate;
}
public AgentsQueue(int arrivalRate, int maxLength) {
this();
this.arrivalRate = arrivalRate;
this.maxLength = maxLength;
}
public int getAllTimeLength() {
return allTimeLength;
}
public int getResignedAgents() {
return resignedAgents;
}
public Queue<Agent> getAgents() {
return agents;
}
public void handleAgents() {
Iterator<Agent> iter = this.agents.iterator();
while (iter.hasNext()) {
Agent agent = iter.next();
agent.waitInLine();
if (agent.getCurrWaitTime() > agent.getMaxWaitTime()) {
iter.remove();
this.resignedAgents++;
}
}
}
public void scheduleNextEvent() {
RandomNumberGenerator randomNumberGenerator = new RandomNumberGenerator();
this.timeToNextEvent = randomNumberGenerator.gerRandomNumberBasedOnAverage(this.arrivalRate);
// System.out.println("new event in: " + this.timeToNextEvent);
}
public void handleEvents() {
this.timeToNextEvent--;
if (this.timeToNextEvent == 0) {
this.scheduleNextEvent();
if (this.hasFreeSpace()) {
Agent agent = new Agent();
this.addAgent(agent);
}
}
}
private void addAgent(Agent agent) throws UnsupportedOperationException {
if (this.agents.size() < this.maxLength || this.maxLength == -1) {
this.agents.add(agent);
} else {
throw new UnsupportedOperationException();
}
}
public Agent removeAgent() {
this.allTimeLength++;
return this.agents.poll();
}
private void resignAgent(Agent agent) {
this.agents.remove(agent);
// System.out.println("resigned");
this.resignedAgents++;
}
public boolean hasFreeSpace() {
return this.maxLength == -1 || this.maxLength > this.agents.size();
}
public boolean isEmpty() {
return this.agents.size() == 0;
}
@Override
public String toString() {
return "AgentsQueue{" +
"arrivalRate=" + arrivalRate +
", maxLength=" + maxLength +
", allTimeLength=" + allTimeLength +
", agents=" + agents +
'}';
}
}