-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElevator.java
More file actions
89 lines (80 loc) · 2.75 KB
/
Elevator.java
File metadata and controls
89 lines (80 loc) · 2.75 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
import java.util.ArrayList;
import java.util.Random;
import java.util.Deque;
import java.util.Queue;
import java.util.LinkedList;
import java.util.List;
class Elevator {
private int currentFloor;
public int elevatorCapacity;
private String direction;
List<Passenger> passengers;
public Elevator(int elevatorCap){
elevatorCapacity = elevatorCap;
currentFloor = 1;
direction = "NONE";
this.passengers = new ArrayList<>();
}
public int getCurrentFloor(){
return currentFloor;
}
public String getDirection(){
return direction;
}
public List<Passenger> getPassengers(){
return passengers;
}
//If the direction needs to be changed
public void setDirection(String type){
if (type.equals("UP")) {
direction = "UP";
} else if (type.equals("DOWN")) {
direction = "DOWN";
}
}
//Add passengers to the queue as they board
public void boardPassengers(Passenger passenger){
passengers.add(passenger);
System.out.println("Passenger boarded at " + currentFloor);
}
//Remove the passengers on the list who reached their destination
public void releasingPassengers(){
List<Passenger> released = new ArrayList<>();
for (Passenger passenger : passengers) {
if (passenger.getDestination() == currentFloor) {
released.add(passenger);
}
}
for (Passenger passenger : released) {
passengers.remove(passenger);
passenger.setDestinationTime(); //This saves their total wait times
System.out.println("Passenger released at floor " + currentFloor);
}
}
//If the elevator needs to move to a specific floor
public void moveToFloor(int destination){
if (destination > currentFloor) {
System.out.println("Moving up to floor " + destination);
} else if (destination < currentFloor) {
System.out.println("Moving down to floor " + destination);
} else {
System.out.println("Elevator is already on floor " + currentFloor);
}
currentFloor = destination;
System.out.println("Elevator is now on floor " + currentFloor);
}
//Move up one floor at a time
public void moveUp(){
System.out.println("Elevator is going up. It is currently on floor " + currentFloor);
currentFloor++;
}
//Move down one floor at a time
public void moveDown(){
if (currentFloor > 1) {
System.out.println("Elevator is going down. It is currently on floor " + currentFloor);
currentFloor--;
} else {
System.out.println("Elevator is already on first floor");
}
}
}