diff --git a/starter_code/elevator.js b/starter_code/elevator.js index 5339f35..c158324 100644 --- a/starter_code/elevator.js +++ b/starter_code/elevator.js @@ -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; diff --git a/starter_code/index.js b/starter_code/index.js index 5e480eb..ccb9f1c 100644 --- a/starter_code/index.js +++ b/starter_code/index.js @@ -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(); diff --git a/starter_code/person.js b/starter_code/person.js index fddcc22..3ccf0cb 100644 --- a/starter_code/person.js +++ b/starter_code/person.js @@ -1,5 +1,8 @@ class Person { constructor(name, originFloor, destinationFloor){ + this.name = name; + this.originFloor = originFloor; + this.destinationFloor = destinationFloor; } }