From 628d528229c4a0ca59e9a199c05234012935e605 Mon Sep 17 00:00:00 2001 From: Bart Smet Date: Tue, 9 Jan 2018 20:46:22 +0100 Subject: [PATCH 1/2] feat: Added methods in Elevator and creating attributes for Person --- starter_code/elevator.js | 50 +++++++++++++++++++++++++++++++++------- starter_code/index.js | 12 ++++++++++ starter_code/person.js | 5 ++++ 3 files changed, 59 insertions(+), 8 deletions(-) diff --git a/starter_code/elevator.js b/starter_code/elevator.js index 5339f35..8a015f4 100644 --- a/starter_code/elevator.js +++ b/starter_code/elevator.js @@ -3,17 +3,51 @@ class Elevator { this.floor = 0; this.MAXFLOOR = 10; this.requests = []; + this.waitingList = []; + this.passengers = []; + this.direction = "up"; + this.startMoving; + } + + start() { + this.startMoving = setInterval(()=> {this.update()}, 1000); + } + + stop() { + clearInterval(this.startMoving); + } + + update() { + this.log(); } - start() { } - stop() { } - update() { } _passengersEnter() { } + _passengersLeave() { } - floorUp() { } - floorDown() { } - call() { } - log() { } -} + + floorUp() { + if(this.floor < this.MAXFLOOR) { + this.direction = "up"; + this.floor++; + } + } + + floorDown() { + if(this.floor > 0) { + this.direction = "down"; + this.floor--; + } + } + + call(name, originFloor, destinationFloor) { + let person = new Person(name, originFloor, destinationFloor); + this.requests.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..7eb7a63 100644 --- a/starter_code/person.js +++ b/starter_code/person.js @@ -1,6 +1,11 @@ class Person { constructor(name, originFloor, destinationFloor){ + this.name = name; + this.originFloor = originFloor; + this.destinationFloor = destinationFloor; } + + } module.exports = Person; From f4c802fd8707f5692dd6c1e1ecbb37dda534f249 Mon Sep 17 00:00:00 2001 From: Bart Smet Date: Wed, 10 Jan 2018 21:30:20 +0100 Subject: [PATCH 2/2] feat: Last iteration, final commit --- starter_code/elevator.js | 34 +++++++++++++++++++++++++++++----- starter_code/person.js | 2 -- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/starter_code/elevator.js b/starter_code/elevator.js index 8a015f4..18728e3 100644 --- a/starter_code/elevator.js +++ b/starter_code/elevator.js @@ -19,11 +19,35 @@ class Elevator { 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() { } + _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() { } + _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) { @@ -39,9 +63,9 @@ class Elevator { } } - call(name, originFloor, destinationFloor) { - let person = new Person(name, originFloor, destinationFloor); - this.requests.push(person); + call(person) { + this.requests.push(person.originFloor); + this.waitingList.push(person); } log() { diff --git a/starter_code/person.js b/starter_code/person.js index 7eb7a63..90f1dcc 100644 --- a/starter_code/person.js +++ b/starter_code/person.js @@ -4,8 +4,6 @@ class Person { this.originFloor = originFloor; this.destinationFloor = destinationFloor; } - - } module.exports = Person;