Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. roman_elevator.js
  2. var DIRECTION_DOWN = -1
  3. var DIRECTION_NONE = 0
  4. var DIRECTION_UP = 1
  5.  
  6. function HardwareElevator(){};
  7. HardwareElevator.prototype = {
  8.     moveUp:function(){console.log('moving up');},
  9.     moveDown:function(){console.log('moving down');},
  10.     stopAndOpenDoors:function(){console.log('stopping and opening doors');},
  11.     getCurrentFloor:function(){console.log('getting current floor');},
  12.     getCurrentDirection:function(){console.log('getting current drection');}
  13. }
  14.  
  15. function Elevator() {
  16.     this.hw = new HardwareElevator();
  17.     this.hw.addEventListener("doorsClosed", _.bind(this.onDoorsClosed, this));
  18.     this.hw.addEventListener("beforeFloor", _.bind(this.onBeforeFloor, this));
  19.    
  20.     this.destinationFloor = 0;
  21. }
  22. Elevator.prototype = {
  23.     onDoorsClosed: function(floor) {
  24.         if (this.destinationFloor > floor) {
  25.             this.hw.moveUp();
  26.         } else if (this.destinationFloor < floor) {
  27.             this.hw.moveDown();
  28.         }
  29.     },
  30.     onBeforeFloor: function(floor, direction) {
  31.         if (this.destinationFloor === floor) {
  32.             this.hw.stopAndOpenDoors();
  33.         }
  34.     },
  35.     floorButtonPressed: function(floor, direction) {
  36.         if (this.hw.getCurrentDirection === DIRECTION_NONE) {
  37.             this.destinationFloor = floor;
  38.            
  39.             if (this.hw.getCurrentFloor > floor) {
  40.                 this.hw.moveDown();
  41.             } else if (this.hw.getCurrentFloor < floor) {
  42.                 this.hw.moveUp();
  43.             } else {
  44.                 this.stopAndOpenDoors();
  45.             }
  46.         }
  47.     },
  48.     cabinButtonPressed: function(floor) {
  49.         if (this.hw.getCurrentDirection === DIRECTION_NONE) {
  50.             this.destinationFloor = floor;
  51.            
  52.             if (this.hw.getCurrentFloor > floor) {
  53.                 this.hw.moveDown();
  54.             } else if (this.hw.getCurrentFloor < floor) {
  55.                 this.hw.moveUp();
  56.             } else {
  57.                 this.hw.stopAndOpenDoors();
  58.             }
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement