-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuilding.java
More file actions
139 lines (106 loc) · 4.81 KB
/
Building.java
File metadata and controls
139 lines (106 loc) · 4.81 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
import java.util.Random;
import java.util.Scanner;
public class Building {
private int N; // num of floors
private int L; // num of elevators
private int U; // population
private int algorithm;
private Elevator elevatorGroup[]; // An array of L elevators
private Floor floors[]; // An array of N floors
private GroupElevatorController controller;
private Random rand;
public Building(int N, int L, int U) {
this.N = N;
this.L = L;
this.U = U;
this.elevatorGroup = new Elevator[this.L];
this.floors = new Floor[this.N];
this.controller = new GroupElevatorController(this.elevatorGroup, this.floors);
this.controller.setN(this.N);
this.controller.setL(this.L);
this.controller.setU(this.U);
this.rand = new Random();
}
public int getN() {
return N;
}
public void setAlgorithm(int algorithm) {
this.algorithm = algorithm;
}
//creating L elevator objects in the elevatorGroup array.
private void createElevators() {
for (int i = 0; i < this.L; ++i) {
this.elevatorGroup[i] = new Elevator(i, this.algorithm, 1,
1, 1, this.U / 4, 3);
this.elevatorGroup[i].setCurrentFloor(N / 2);
this.elevatorGroup[i].setDirection(1);
this.elevatorGroup[i].setN(N);
this.elevatorGroup[i].setL(L);
}
// creating elevator threads
for (int i = 0; i < this.L; ++i) {
this.elevatorGroup[i].elevatorControllerThread();
this.elevatorGroup[i].performJobThread();
// Start this thread only if user chose Up-peak
if (this.algorithm == 2) {
this.elevatorGroup[i].upPeakThread();
}
// Start this thread only if user chose Zoning
if (this.algorithm == 3) {
this.elevatorGroup[i].zoningThread();
}
}
}
//createing N rloor objects in the floors array.
private void createFloors() {
for (int i = 0; i < this.N; ++i) {
this.floors[i] = new Floor(i);
}
}
public GroupElevatorController getController() {
return controller;
}
//randomly selects a floor from the floors array and calls the generatePassenger method on the Floor(randFloor) object.
private void generatePassenger(int N) throws InterruptedException {
int randFloor = this.rand.nextInt(N);
floors[randFloor].generatePassenger(N);
}
public static void main(String[] args) throws InterruptedException {
Scanner reader = new Scanner(System.in);
if ((args.length == 3) && (Integer.parseInt(args[0]) > 1) &&
(Integer.parseInt(args[1]) > 0) && (Integer.parseInt(args[2]) > 0)) {
if (Integer.parseInt(args[0]) > Integer.parseInt(args[1])) {
Building building = new Building(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]));
// Chose algorithm
System.out.println("\nChoose algorithm:\n");
System.out.println("\t1 - Round-Robin");
System.out.println("\t2 - Up-Peak");
System.out.println("\t3 - Zoning");
System.out.println("\t4 - Three Passage");
int tempAlgorithm = reader.nextInt();
while (tempAlgorithm < 1 || tempAlgorithm > 4) {
tempAlgorithm = reader.nextInt();
}
building.setAlgorithm(tempAlgorithm); // Sets algorithm in Building class
building.getController().setAlgorithm(tempAlgorithm); // Sets algorithm in GroupElevatorController class
System.out.println("\nAll elevators start on the central floor of the building with direction up.\n");
// Create N number of Floor objects
building.createFloors();
// Create L number of Elevator objects
building.createElevators();
// Start the GroupElevatorController thread
new Thread(building.getController()).start(); // Activates the GroupElevatorController to scan the floors array
// Keep generating passengers on different floors of the building
while (true) {
// Generate a passenger on one of the floors
building.generatePassenger(building.getN());
Thread.sleep(20000);
}
} else {
System.out.println("The number of floors cannot be less than number of elevators.");
}
} else {
System.out.println("Usage: java Building <number of floors, at least 2>\n\t\t<number of elevators, at least 1>\n\t\t<building population, at least 1>");
}
}
}