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
100 changes: 87 additions & 13 deletions starter_code/elevator.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,93 @@
class Elevator {
constructor(){
this.floor = 0;
this.MAXFLOOR = 10;
this.requests = [];
constructor() {
this.floor = 0;
this.MAXFLOOR = 10;
this.waitingList = [];
this.passengers = [];
this.requests = [];
this.direction = "up";
this.setInterval;
}
start() {
this.setInterval = setInterval(() => {
this.update();
if (this.direction == "up") {
this.floorUp();
}else {this.floorDown()}
}, 1000);
}
stop() {
clearInterval(this.setInterval);
this.checkWaitingList();
this.checkPassengersList();
if(this.requests.length > 0){
this.start()}
}
update() {
if (this.requests.length == 0){
this.stop()
console.log ("no one left in the elevator");
}
return this.log();
}
_passengersEnter(ind) {
this.passengers.push(this.waitingList[ind]);
console.log(this.waitingList[ind].name + " has enter the elevator");
this.requests.push(this.waitingList[ind].destinationFloor);
this.waitingList.splice(ind, 1);
}
_passengersLeave(ind) {
console.log(this.passengers[ind].name + " has left the elevator");
this.passengers.splice(ind, 1);
}
floorUp() {
this.checkRequestsList();
if (this.floor < this.MAXFLOOR && this.direction == "up") {
this.floor++;
} else {
this.direction = "down";
this.floorDown();
}
}

start() { }
stop() { }
update() { }
_passengersEnter() { }
_passengersLeave() { }
floorUp() { }
floorDown() { }
call() { }
log() { }
floorDown() {
this.checkRequestsList();
if (this.floor > 0 && this.direction == "down") {
this.floor--;
} else {
this.direction = "up";
this.floorUp();
}
}
call(person) {
this.waitingList.push(person);
this.requests.push(person.originFloor);
}
log() {
console.log(this.floor);
}
checkWaitingList() {
for (let i = 0; i < this.waitingList.length; i++) {
if (this.waitingList[i].originFloor == this.floor) {
this._passengersEnter(i);
}
}
}
checkPassengersList() {
for (let i = 0; i < this.passengers.length; i++) {
if (this.passengers[i].destinationFloor == this.floor) {
this._passengersLeave(i);
}
}
}
checkRequestsList() {
for (let i = 0; i < this.requests.length; i++) {
if (this.floor == this.requests[i]) {
this.requests.splice(i,1);
this.stop();
}
}
}
}

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

let elevator1 = new Elevator();
let person1 = new Person("alice",2,3)
let person2 = new Person("juan",2,1)
let person3 = new Person("pepe",2,7)
let person4 = new Person("jose",8,1)
let person5 = new Person("pedro",6,2)
elevator1.call(person1)
elevator1.call(person2)
elevator1.call(person3)
elevator1.call(person4)
elevator1.call(person5)


elevator1.start();
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