Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BCN-PT] Javier Armero - lab-node-elevator #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 103 additions & 15 deletions starter_code/elevator.js
Original file line number Diff line number Diff line change
@@ -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;
16 changes: 16 additions & 0 deletions starter_code/index.js
Original file line number Diff line number Diff line change
@@ -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);
5 changes: 5 additions & 0 deletions starter_code/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions starter_code/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
6 changes: 4 additions & 2 deletions starter_code/person.js
Original file line number Diff line number Diff line change
@@ -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;