-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeManager.java
108 lines (83 loc) · 2.79 KB
/
TimeManager.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
105
106
107
108
/*this class implements Singleton design pattern.*/
package timemanager;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import elevatorentities.AvgMinMaxWaitTimeByFloor;
import elevatorentities.Building;
public class TimeManager{
private static TimeManager timeManagerInstance;
private long currentTimeMillis;
private TimeManager() {
}
public static TimeManager getInstance() {
if (timeManagerInstance == null) {
timeManagerInstance = new TimeManager();
}
return timeManagerInstance;
}
public long getCurrentTimeMillis() {
return currentTimeMillis;
}
/*this method runs the process for specified duration.*/
public void runtime() throws Exception {
/*
* Elevator functions for 15 minutes, below is the initialization of the
* duration
*/
long elevatorProcessDuration = 900000;
long startTimeOfProcess = 0;
while (currentTimeMillis - startTimeOfProcess <= elevatorProcessDuration) {
//calls method to create the person request
ProcessElevatorRequestesAccToPersonRequest();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
currentTimeMillis += 4000;
}
generateWaitTimeByFloor();
System.exit(0);
}
/*this method is used to generate the Avg/Max/Min wait time by floor.*/
private void generateWaitTimeByFloor() {
int max = 0;
int min = 0;
int avg = 0;
Map<Integer, AvgMinMaxWaitTimeByFloor> waitTimeByFloor = controller.ElevatorControllerSimulator.getInstance()
.getWaitTimeByFloor();
System.out.printf("%10s %30s %20s %5s", " Start Floor ", " Average Wait Time ", " Min Wait Time",
" Max Wait Time");
System.out.println();
for (Integer key : waitTimeByFloor.keySet()) {
AvgMinMaxWaitTimeByFloor avgMinMaxWaitTimeByFloor = waitTimeByFloor.get(key);
List<Integer> waitTime = avgMinMaxWaitTimeByFloor.getWaitTime();
if (waitTime.size() > 0) {
max = Collections.max(waitTime);
min = Collections.min(waitTime);
avg = findAvg(waitTime);
}
System.out.format("%10s %30s %20s %5s", "Floor " + key, avg + "seconds", min + "seconds", max + "seconds");
System.out.println();
}
}
/*this method is used to find the average of integers in the arraylist.*/
private int findAvg(List<Integer> waitTime) {
Integer sum = 0;
if (!waitTime.isEmpty()) {
for (Integer wait : waitTime) {
sum += wait;
}
return sum / waitTime.size();
}
return sum;
}
/*this method creates a person object, every 12 seconds.*/
private void ProcessElevatorRequestesAccToPersonRequest() throws Exception {
// Each minute 5 persons requests are raised
if (currentTimeMillis % 12000 == 0) {
Building.getInstance().createPersonRequest();
}
}
}