明确主要需求
首先需要设计电梯系统的基本工作流程,一个简单电梯系统主要就是两个主要功能:
- 乘客在电梯外按下按钮时,电梯系统会驱动一个电梯来接人
 - 乘客在电梯内部按下楼层按钮时,电梯系统会驱动该电梯到达指定楼层
 
根据需求来创建必要对象及属性
电梯系统类 - ElevatorSystem
用来管理系统中所有电梯类,并保存电梯调度算法 - (当乘客在外部摁键时,通过算法分配一个电梯去接人)
class ElevatorSystem {
    List<Elevator> elevators;
    Date datetime;
    private static final ElevatorSystem instance = new ElevatorSystem();
    private ElevatorSystem() {
        elevators = new ArrayList<>();
    }
    public static ElevatorSystem getInstance() {
        return instance;
    }
    public void addElevator(Elevator e) {
        elevators.add(e);
    }
    public Elevator assignElevator(Request req) {
        // 选一个IDLE状态的电梯,如果没有则找一个最不忙的电梯
        Elevator res = elevators.get(0);
        for (Elevator tmp: elevators) {
            if (tmp.status == Direction.IDLE) return tmp;
            else if (tmp.checkWorkLoad() < res.checkWorkLoad()) res = tmp;
        }
        return res;
    }
}
 
电梯类 - Elevator
用于保存电梯的各种属性(当前层,当前状态,最大承重,能到达的最高楼层),以及电梯接送人的顺序逻辑
- 我们建立两个HashSet 来分别保存乘客向上和向下的请求,使用HashSet的优点是可以过滤掉重复请求,没到达一层时直接查找HashSet来决定停还是不停。
 
class Elevator {
    int currFloor; // 当前楼层
    Direction status; // 电梯当前方向
    int maxFloor;
    int maxWeight;
    HashSet<Integer> upQueue = new HashSet<>();
    HashSet<Integer> downQueue = new HashSet<>();
    public Elevator(int maxFloor, int maxWeight) {
        this.maxFloor = maxFloor;
        this.maxWeight = maxWeight;
        currFloor = 1;
        status = Direction.IDLE;
    }
    public void handleRequest(Request req) {
        if (req.dir == Direction.DOWN) {
            downQueue.add(req.floor);
        } else {
            upQueue.add(req.floor);
        }
    }
    public int checkWorkLoad() {
        // 查看当前电梯繁忙程度
        return downQueue.size() + upQueue.size();
    }
    public boolean run() {
        try {
            // status refresh
            // when reach 1st floor then switch status to up
            // when reach top floor than switch status to down
            // if status is down but not req in downQ then switch status to UP
            // if status is up but not req in upQ then swith status to Down
            // if no req then switch to IDLE
            if (currFloor==1 && status==Direction.DOWN) {
                status = Direction.UP;
            } else if (currFloor==maxFloor && status==Direction.UP) {
                status = Direction.DOWN;
            } else if (checkWorkLoad()==0) {
                status = Direction.IDLE;
            } else if (upQueue.size()==0) {
                status = Direction.DOWN;
            } else if (downQueue.size()==0) {
                status = Direction.UP;
            }
            // move to next stop
            // if status is down then find next stop and move
            // if status is up then find next stop and move
            if (status == Direction.DOWN) {
                for (int i = currFloor; i>=1; i--) {
                    if (downQueue.contains(i)) {
                        downQueue.remove(i);
                        currFloor = i;
                        return true;
                    }
                }
            } else if (status == Direction.UP) {
                for(int i = currFloor; i<=maxFloor; i++) {
                    if (upQueue.contains(i)) {
                        upQueue.remove(i);
                        currFloor = i;
                        return true;
                    }
                }
            }
        } catch (Exception ex) {
            System.out.println("Alert!!! call the emergency office");
        }
        return false;
    }
} 
按键类 (或者请求类) - Request (InternalRequest/ExternalRequest)
这是一个抽象类,并被内部摁键类和外部摁键类继承,用于保存每一个乘客请求的具体信息
enum Direction  {
    UP, DOWN, IDLE
}
abstract class Request {
    int floor;
    Direction dir;
}
class ExternalRequest extends Request {
    int floor;
    Direction dir;
    public ExternalRequest(int f, Direction d) {
        floor = f;
        dir = d;
    }
}
class InternalRequest extends Request {
    int floor;
    Direction dir;
    public InternalRequest(int f, Direction d) {
        floor = f;
        dir = d;
    }
} 
(非必要) 乘客类 - Passenger
保存乘客摁键行为的类
- 按外部按键
 - 按内部按键
 
class Passenger {
    ElevatorSystem instance = ElevatorSystem.getInstance();
    Elevator assignedElv = null;
    public boolean pressExtButton(int floor, Direction dir) {
        assignedElv = instance.assignElevator(new ExternalRequest(floor, dir));
        return true;
    }
    public boolean pressIntButton(int floor) {
        Direction dir = Direction.IDLE;
        if (floor > assignedElv.currFloor) assignedElv.handleRequest(
                new InternalRequest(floor, Direction.UP));
        else if (floor > assignedElv.currFloor) assignedElv.handleRequest(
                new InternalRequest(floor, Direction.DOWN));
        else return false;
        return true;
    }
} 
 
根据需求来创建相应函数
电梯系统类 - ElevatorSystem
- 将外部请求调度给系统中的一架电梯 - assignElevator(Request)
 
电梯类 - Elevator
- 处理按键请求 - handleRequest(Request)
 - 根据所有请求运行电梯 - runElevator()
 
(非必要) 乘客类 - Passenger
- 按外部按键 - pressExtButton()
 - 按内部按键 - pressIntButton()
 

加分要求
- 引入Enum类来定义常量状态
 - 引入单例设计模式来定义ElevatorSystem类
 - 增加电梯种类(货运电梯,载人电梯,VIP电梯) 不同电梯有不同策略
 - 优化电梯调度 
  
- 闲置电梯优先
 - 如果没有闲置,选取请求数最少的电梯
 
 - 优化电梯请求处理业务 
  
- 如果非高峰期,可以采用FIFO方式处理请求
 - 如果达到高峰期,可以根据当前电梯状态按照楼层升序处理相同状态的所有请求
 
 - 根据电梯工作高峰期来使用不同的请求处理算法,使用Strategy设计模式
 



















