-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildingManager.java
More file actions
154 lines (130 loc) · 3.94 KB
/
BuildingManager.java
File metadata and controls
154 lines (130 loc) · 3.94 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Yiqiao Zhao: 38332452
* Chen Lu: 51398516
*/
import java.util.ArrayList;
//import java.util.concurrent.locks.*;
public class BuildingManager
{
private BuildingFloor[] floors;
private int currentHeight=0;
//private Lock missionCheckLock;
BuildingManager(){
floors=new BuildingFloor[5];
}
public void addFloor(BuildingFloor f){
floors[currentHeight]=f;
currentHeight++;
}
public int getHeight(){
return floors.length;
}
public synchronized int[] returnNewMission(int currentFloor){
//The return array is an array of SIX int, array[0] is
//the floor that has new passengers, the last 5 int are the number
// of passengers to each floor
//going up and going down are separated
//remove the request, mark as handled
int[] answer=new int[]{-1,0,0,0,0,0};
//generate a list of floor number sorted by the distance to
// the currentFloor
ArrayList<Integer> index = new ArrayList<Integer>();
index.add(currentFloor);
for (int delta=1; delta < floors.length; delta++){
int floorNum = delta + currentFloor;
if (floorNum >= 0 && floorNum < floors.length){
index.add(floorNum);
}
floorNum = currentFloor - delta;
if (floorNum >= 0 && floorNum < floors.length){
index.add(floorNum);
}
}
//for every floor number in index, find the requests if exist
for (int i: index){
if (floors[i].isBuildingFree()==false){
answer[0]=i;
boolean hasAnswer = false;
//find all going-up requests
for (int j=i; j<floors.length; j++){
answer[j+1]=floors[i].getPassengerRequests()[j];
removeRequest(i, j);
if (answer[j+1] != 0)
hasAnswer = true;
}
if (hasAnswer){
return answer;
}
//find all going-down requests
for (int j =i; j > -1; j--){
answer[j+1]=floors[i].getPassengerRequests()[j];
removeRequest(i, j);
}
return answer;
}
}
return answer;
}
public synchronized void setAppoarchingElevator(int eid, int floor){
floors[floor].setApproachingElevator(eid);
}
public synchronized void setRequest(int from, int to, int amount){
// add new request to records
floors[from].setNewRequest(to, amount);
}
public synchronized void setNewArrival(int from, int to, int amount){
floors[to].setNewArrival(from, amount);
}
public synchronized void removeRequest(int from, int to){
floors[from].removeRequest(to);
}
public String toString(){
String result= toStringCurrent();
result += "----------------------------\n";
result += toStringTotalD();
result += "----------------------------\n";
// result += toStringApproaching();
result += toStringTotalA();
result += "----------------------------\n";
return result;
}
public String toStringApproaching(){
String result = "Approaching elevators\n";
for (int i = 0; i < 5; i++){
result += "\t Floor " + i + ", " +floors[i].toStringApproching();
}
return result;
}
public String toStringCurrent(){
// return a string represent the current requests
String result = "Current Passenger Request\n";
result += " To | 0 | 1 | 2 | 3 | 4 |\n";
for (int i = 0; i < 5; i++){
result += "From " + i + " |";
result += floors[i].toStringCurrent();
result += "\n";
}
return result;
}
public String toStringTotalD(){
// return a string represent the total departure
String result = "Total Passenger Request\n";
result += " To | 0 | 1 | 2 | 3 | 4 |\n";
for (int i = 0; i < 5; i++){
result += "From " + i + " |";
result += floors[i].toStringTotalD();
result += "\n";
}
return result;
}
public String toStringTotalA(){
String result = "Total Passenger Arrival\n";
result += " From | 0 | 1 | 2 | 3 | 4 |\n";
for (int i = 0; i < 5; i++){
result += " To " + i + " |";
result += floors[i].toStringTotalA();
result += "\n";
}
return result;
}
}