diff --git a/starter_code/elevator.js b/starter_code/elevator.js index 5339f35..ac2b23a 100644 --- a/starter_code/elevator.js +++ b/starter_code/elevator.js @@ -1,19 +1,107 @@ +const Person = require('./person.js'); class Elevator { - constructor(){ - this.floor = 0; - this.MAXFLOOR = 10; - this.requests = []; - } - - start() { } - stop() { } - update() { } - _passengersEnter() { } - _passengersLeave() { } - floorUp() { } - floorDown() { } - call() { } - log() { } + constructor() { + this.floor = 0; + this.MAXFLOOR = 10; + this.waitingList = []; + this.passengers = []; + this.requests = []; + this.direction = "Up"; + this.stringLog = ""; + } + + management() { + if (!this.requests.length) { + console.log("\n|| END ||\n"); + this.stop(); + } else { + //Enter passengers from waiting list + for (let i = this.waitingList.length - 1; i >= 0; i--) { + if (this.floor === this.waitingList[i].originFloor) { + this._passengersEnter(this.waitingList[i]); + this.requests.push(this.waitingList[i].destinationFloor); + this.waitingList.splice(i, 1); + } + } + + //Exit passengers + if (!this.passengers.length) { + console.log("Nobody is in the elevator right now"); + } else { + for (let i = this.passengers.length - 1; i >= 0; i--) { + if (this.floor === this.passengers[i].destinationFloor) { + this._passengersLeave(i); + } + } + } + + //Delete all requests + for (let i = this.requests.length - 1; i >= 0; i--) { + if (this.floor === this.requests[i]) { + this.requests.splice(i, 1); + } + } + + let destination = this.requests[0]; + if (destination >= this.floor) this.direction = "Up"; + else this.direction = "Down"; + this.log(); + switch (this.direction) { + case "Up": + this.floorUp(); + break; + case "Down": + this.floorDown(); + break; + default: + } + } + } + + start() { + this.elevatorLoop = setInterval(() => this.update(), 1000); + } + stop() { + clearInterval(this.elevatorLoop); + } + update() { + console.log("\n*****************************************************************\n\n"); + this.management(); + } + + _passengersEnter(person) { + this.passengers.push(person); + console.log("--> PASSANGER ENTER:", person); + } + _passengersLeave(index) { + console.log("<-- PASSANGER EXIT:", this.passengers[index]); + this.passengers.splice(index, 1); + } + + floorUp() { + if (this.floor < 10) { + this.floor += 1; + } else { + this.floor = 10; + // this.stop(); + } + } + floorDown() { + if (this.floor > 0) { + this.floor -= 1; + } else { + this.floor = 0; + } + } + call(person) { + this.waitingList.push(person); + this.requests.push(person.originFloor); + console.log(`${person.name} called elevator at ${person.originFloor} going to ${person.destinationFloor} `); + } + + log() { + console.log(`\nWAITING LIST: ${JSON.stringify(this.waitingList)} \nPASSENGERS: ${JSON.stringify(this.passengers)} \nDirection: ${this.direction} | Floor: ${this.floor} | Requests:[${this.requests}]`); + } } module.exports = Elevator; diff --git a/starter_code/index.js b/starter_code/index.js index 5e480eb..2b831a2 100644 --- a/starter_code/index.js +++ b/starter_code/index.js @@ -1 +1,17 @@ +const Person = require('./person.js'); const Elevator = require('./elevator.js'); + +let isa = new Person("Isa", 1, 8); +let isa2 = new Person("Isa", 1, 8); +let raul = new Person("Raul", 2, 5); +let fer = new Person("Fer", 5, 1); +let gonzu = new Person("Gonzu", 0, 4); + +let elevator = new Elevator(); + +elevator.start(); +elevator.call(isa); +elevator.call(isa2); +elevator.call(raul); +elevator.call(fer); +setTimeout(() => {elevator.call(gonzu)}, 10000); diff --git a/starter_code/package-lock.json b/starter_code/package-lock.json new file mode 100644 index 0000000..7d3d765 --- /dev/null +++ b/starter_code/package-lock.json @@ -0,0 +1,5 @@ +{ + "name": "elevator", + "version": "1.0.0", + "lockfileVersion": 1 +} diff --git a/starter_code/package.json b/starter_code/package.json new file mode 100644 index 0000000..9a8afea --- /dev/null +++ b/starter_code/package.json @@ -0,0 +1,13 @@ +{ + "name": "starter_code", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start": "index.js", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" +} diff --git a/starter_code/person.js b/starter_code/person.js index fddcc22..885a3cc 100644 --- a/starter_code/person.js +++ b/starter_code/person.js @@ -1,6 +1,8 @@ class Person { - constructor(name, originFloor, destinationFloor){ + constructor(name, originFloor, destinationFloor) { + this.name = name; + this.originFloor = originFloor; + this.destinationFloor = destinationFloor; } } - module.exports = Person;