diff --git a/starter_code/elevator.js b/starter_code/elevator.js index 5339f35..18728e3 100644 --- a/starter_code/elevator.js +++ b/starter_code/elevator.js @@ -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; diff --git a/starter_code/index.js b/starter_code/index.js index 5e480eb..2b361e2 100644 --- a/starter_code/index.js +++ b/starter_code/index.js @@ -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(); diff --git a/starter_code/person.js b/starter_code/person.js index fddcc22..90f1dcc 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; } }