@@ -7,7 +7,7 @@ export class ElevatorController {
77 private _currentFloor : number = 0 ;
88
99 constructor (
10- readonly door : ElevatorDoor ,
10+ public readonly door : ElevatorDoor ,
1111 private readonly onChange ?: ( status : ElevatorStatus ) => void ,
1212 ) {
1313 // todo: I don't like it, but works for now
@@ -42,25 +42,7 @@ export class ElevatorController {
4242
4343 private _activeQ : ElevatorQueue | null = null ;
4444
45- useDownQ ( ) : void {
46- this . _activeQ = this . downQ ;
47-
48- this . callOnChange ( ) ;
49- }
50-
51- useUpQ ( ) : void {
52- this . _activeQ = this . upQ ;
53-
54- this . callOnChange ( ) ;
55- }
56-
5745 get direction ( ) : Direction {
58- if ( this . _activeQ === this . upQ ) {
59- return "up" ;
60- } else if ( this . _activeQ === this . downQ ) {
61- return "down" ;
62- }
63-
6446 switch ( this . _activeQ ) {
6547 case this . upQ : {
6648 return "up" ;
@@ -95,10 +77,18 @@ export class ElevatorController {
9577 }
9678
9779 queueFloor ( floor : number ) : void {
80+ let updatedQ : ElevatorQueue ;
81+
9882 if ( floor > this . currentFloor ) {
9983 this . upQ . add ( floor ) ;
84+ updatedQ = this . upQ ;
10085 } else {
10186 this . downQ . add ( floor ) ;
87+ updatedQ = this . downQ ;
88+ }
89+
90+ if ( this . direction === "none" ) {
91+ this . _activeQ = updatedQ ;
10292 }
10393
10494 this . callOnChange ( ) ;
@@ -124,8 +114,21 @@ export class ElevatorController {
124114 }
125115 break ;
126116 }
127- default :
117+ case "none" : {
118+ if ( floor > this . currentFloor ) {
119+ this . upQ . add ( floor ) ;
120+ this . _activeQ = this . upQ ;
121+ } else {
122+ this . downQ . add ( floor ) ;
123+ this . _activeQ = this . downQ ;
124+ }
128125 break ;
126+ }
127+ default : {
128+ break ;
129+ }
129130 }
131+
132+ this . callOnChange ( ) ;
130133 }
131134}
0 commit comments