From f3b5b053563cac33fc5034f1f4844f3d502e3649 Mon Sep 17 00:00:00 2001 From: Alfio Date: Mon, 29 Jan 2018 19:08:12 +0100 Subject: [PATCH 1/2] el elevador sube y baja --- starter_code/elevator.js | 55 ++++++++++++++++++++++++++++++---------- starter_code/index.js | 4 +++ 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/starter_code/elevator.js b/starter_code/elevator.js index 5339f35..f3f1b14 100644 --- a/starter_code/elevator.js +++ b/starter_code/elevator.js @@ -1,19 +1,48 @@ class Elevator { - constructor(){ - this.floor = 0; - this.MAXFLOOR = 10; - this.requests = []; + constructor() { + this.floor = 0; + this.MAXFLOOR = 3; + this.requests = []; + this.direction = "up"; + this.setInterval; + } + start() { + this.setInterval = setInterval(() => { + this.update(); + this.floorUp(); + }, 1000); + + } + stop() { + clearInterval(this.setInterval); + } + update() { + return this.log(); + } + _passengersEnter() {} + _passengersLeave() {} + floorUp() { + 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() { + if (this.floor > 0 && this.direction == "down") { + this.floor--; + } else { + this.direction = "up"; + this.floorUp(); + } + } + call() {} + log() { + console.log(this.floor); + + } } module.exports = Elevator; diff --git a/starter_code/index.js b/starter_code/index.js index 5e480eb..4aea762 100644 --- a/starter_code/index.js +++ b/starter_code/index.js @@ -1 +1,5 @@ const Elevator = require('./elevator.js'); + +let elevator1 = new Elevator(); + +elevator1.start(); From 3c751c41079199a258c39b5eefa2003873d3025e Mon Sep 17 00:00:00 2001 From: Alfio Date: Mon, 29 Jan 2018 23:29:02 +0100 Subject: [PATCH 2/2] ElevaTHor --- starter_code/elevator.js | 75 ++++++++++++++++++++++++++++++++-------- starter_code/index.js | 12 +++++++ starter_code/person.js | 3 ++ 3 files changed, 75 insertions(+), 15 deletions(-) diff --git a/starter_code/elevator.js b/starter_code/elevator.js index f3f1b14..c158324 100644 --- a/starter_code/elevator.js +++ b/starter_code/elevator.js @@ -1,36 +1,57 @@ class Elevator { constructor() { this.floor = 0; - this.MAXFLOOR = 3; + this.MAXFLOOR = 10; + this.waitingList = []; + this.passengers = []; this.requests = []; this.direction = "up"; - this.setInterval; + this.setInterval; } start() { this.setInterval = setInterval(() => { this.update(); - this.floorUp(); + if (this.direction == "up") { + this.floorUp(); + }else {this.floorDown()} }, 1000); - } stop() { - clearInterval(this.setInterval); + 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() {} - _passengersLeave() {} + _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() { - if (this.floor < this.MAXFLOOR && this.direction == "up" ) { - this.floor++; - } else { - this.direction = "down"; - this.floorDown() - } + this.checkRequestsList(); + if (this.floor < this.MAXFLOOR && this.direction == "up") { + this.floor++; + } else { + this.direction = "down"; + this.floorDown(); + } } floorDown() { + this.checkRequestsList(); if (this.floor > 0 && this.direction == "down") { this.floor--; } else { @@ -38,10 +59,34 @@ class Elevator { this.floorUp(); } } - call() {} + 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(); + } + } } } diff --git a/starter_code/index.js b/starter_code/index.js index 4aea762..ccb9f1c 100644 --- a/starter_code/index.js +++ b/starter_code/index.js @@ -1,5 +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; } }