elevator-design part 1
How to design an elevator system
this is a famous interview questions. Design a elevator system would never be an easy task. How to ship people efficiently and quickly without over the load restriction, how to avoid the vacancy, how to endure the peak time. if there are multiple cars, how to allocate the floors and persons, all those questions are hard to solve. A lot of paper discussed about those problem, and a lot of patents got pulished during the investigation. some used AI, or fuzz theory. In interview, we won’t have time to talk about those advanced solutions, we just need to give a feasible way, if it looks reasonable, then it is fine.
we can start from the easiest one - only one elevator
Elevator system with only one elevator
this is an easy case. we can design the algorithm like this way:
- if the car is going up, the we loop all the floors which higher than the current level.if there are such floors, we would add them to our destination list.At the same time, if the lower level has requirement, we just simply don’t respond to it.
- if the car is going up, if no higher floor needs a ride, but there are lower level requirement, we can stop at the current level and change the direction and moving downward instead
- if the car is stop for more then 30 secs, we can move the car to the first floor and waiting
- going down could follow the same logic.
code
the link is here : code
Data Structure
1 | public int[][] controlTable; //用二维数组controlTable与电梯口的上下键对应,其中如果值1表示button应显示绿色,值0表示button应显示灰色。 |
controlTable
is correspoinding to buttons besides the door for each floor
1 | public int[]table; //用二维数组table与电梯内的数字键对应,其中如果值1表示button应显示桔黄色,值0表示button应显示灰色。 |
table
stands for the buttons inside car of elevator
1 | public class LiftThread extends Thread { |
LiftThread
would depicts the elevator running status. number
is the current floor number, state
is the elevator’s direction, 0-stop, 1-up, 2-down
, destination
is the elevator’s target floor.
Algorithm
control logic
1 | if (state == 1) { //向上电梯经过的向上任务完成,从任务数组中删除并重新显示上下键的颜色。 |
algorithm to move
1 | public class TimeListener implements ActionListener { |
move up is the same as the previous code logic,
1 | public void actionUp(){ |
above discussion is about the single elevator, next part we would discuss about multiple elevators in the same buiding.