-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelevator.js
91 lines (87 loc) · 2.95 KB
/
elevator.js
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
class Elevator {
constructor(id, numberOfFloors, initialFloor, elevatorStatus) {
this.id = id;
this.numberOfFloors = numberOfFloors;
this.currentFloor = initialFloor;
this.status = elevatorStatus;
this.queueUp = [];
this.queueDown = [];
}
addToQueue(orderFloor, destinationFloor) {
if (this.currentFloor < orderFloor) {
this.queueUp.push(orderFloor);
this.queueUp.sort((a, b) => a - b);
} else {
this.queueDown.push(orderFloor);
this.queueDown.sort((a, b) => b - a);
}
if (orderFloor > destinationFloor) {
this.queueDown.push(destinationFloor);
this.queueDown.sort((a, b) => b - a);
}
if (orderFloor < destinationFloor) {
this.queueUp.push(destinationFloor);
this.queueUp.sort((a, b) => a - b);
}
this.moveElevator(orderFloor);
}
removeFromQueueUp(requestedFloor) {
this.queueUp = this.queueUp.filter((floor) => floor !== requestedFloor);
console.log(
`${this.id}-р цахилгаан шат 🛗 ${requestedFloor} -р давхарт ирлээ`
);
}
removeFromQueueDown(requestedFloor) {
this.queueDown = this.queueDown.filter((floor) => floor !== requestedFloor);
console.log(
`${this.id}-р цахилгаан шат 🛗 ${requestedFloor}-р давхарт ирлээ`
);
}
moveElevator(requestedFloor) {
console.log(
`${this.id}-р цахилгаан шатыг 🛗 ${this.currentFloor}-р давхраас ${requestedFloor}-р давхар руу шилжүүлнэ`
);
if (this.currentFloor < requestedFloor) {
this.moveUp();
} else {
this.moveDown();
}
}
moveUp() {
this.status = "up";
console.log(`${this.id}-р цахилгаан шат 🛗 дээшээ гарч байна`);
while (this.currentFloor < this.queueUp[this.queueUp.length - 1]) {
this.currentFloor++;
if (this.queueUp.includes(this.currentFloor)) {
this.removeFromQueueUp(this.currentFloor);
}
}
if (this.queueUp.length === 0 && this.queueDown.length > 0) {
this.moveDown();
} else {
this.status = "idle";
console.log(
`${this.id}-р цахилгаан шат 🛗 ${this.currentFloor}-р давхарт дуудлага хүлээж байна`
);
}
}
moveDown() {
this.status = "down";
console.log(`${this.id}-р цахилгаан шат 🛗 доошоо бууж байна`);
while (this.currentFloor > this.queueDown[this.queueDown.length - 1]) {
this.currentFloor--;
if (this.queueDown.includes(this.currentFloor)) {
this.removeFromQueueDown(this.currentFloor);
}
}
if (this.queueDown.length === 0 && this.queueUp.length > 0) {
this.moveUp();
} else {
this.status = "idle";
console.log(
`${this.id}-р цахилгаан шат 🛗 ${this.currentFloor}-р давхарт дуудлага хүлээж байна`
);
}
}
}
module.exports = Elevator;