Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 67 additions & 9 deletions starter_code/elevator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,75 @@ class Elevator {
this.floor = 0;
this.MAXFLOOR = 10;
this.requests = [];
this.waitingList = [];
this.passengers = [];
this.direction = "up";
this.startMoving;
}

start() { }
stop() { }
update() { }
_passengersEnter() { }
_passengersLeave() { }
floorUp() { }
floorDown() { }
call() { }
log() { }
start() {
this.startMoving = setInterval(()=> {this.update()}, 1000);
}

stop() {
clearInterval(this.startMoving);
}

update() {
this.log();
this._passengersEnter();
this._passengersLeave();
this.floor < this.requests[0] ? this.floorUp() : this.floorDown();
if (this.floor === this.requests[0]) {
this.requests.shift();
}
if (this.requests.length === 0) {
this.stop();
}
}

_passengersEnter() {
this.waitingList.forEach((element, index) => {
if(element.originFloor === this.floor) {
this.passengers.push(element);
this.waitingList.splice(index, 1);
this.requests.push(element.destinationFloor);
}
});
}

_passengersLeave() {
this.passengers.forEach((element, index)=> {
if (element.destinationFloor === this.floor) {
this.passengers.splice(index, 1);
console.log(`${element.name} has left the elevator`);
}
});
}

floorUp() {
if(this.floor < this.MAXFLOOR) {
this.direction = "up";
this.floor++;
}
}

floorDown() {
if(this.floor > 0) {
this.direction = "down";
this.floor--;
}
}

call(person) {
this.requests.push(person.originFloor);
this.waitingList.push(person);
}

log() {
console.log(`Direction: ${this.direction}`);
console.log(`Floor: ${this.floor}`);
};
}

module.exports = Elevator;
12 changes: 12 additions & 0 deletions starter_code/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
const Elevator = require('./elevator.js');
const Person = require('./person.js');

let elevator = new Elevator();


//test zone//
elevator.floorUp();
elevator.update();
elevator.floorUp();
elevator.update();
elevator.floorDown();
elevator.update();
3 changes: 3 additions & 0 deletions starter_code/person.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class Person {
constructor(name, originFloor, destinationFloor){
this.name = name;
this.originFloor = originFloor;
this.destinationFloor = destinationFloor;
}
}

Expand Down