题目集5-7blog

这次电梯程序我没有写出来。以下是我的反思和收获。

题目分析
题目一分析:

该题目要求设计一个电梯模拟系统,主要包含以下核心功能:

电梯状态管理:包括当前楼层、运行方向(上行/下行/空闲)、运行状态(停止、移动、开门、关门)。

请求队列管理:

内部请求(电梯内乘客按下的目标楼层)

外部请求(各楼层乘客的上下行请求,需区分方向)

调度算法:

默认停在1层,空闲状态。

同方向优先:电梯运行时,优先响应同方向的请求。

顺路停靠:移动过程中检查是否有顺路请求,有则停靠。

反向请求处理:当前方向无请求时,才处理反向请求。

运行模拟:

每次移动一个楼层,检查是否有停靠需求。

无请求时保持空闲。

关键点:

方向优先级(同方向优先)

请求队列的动态管理(内部+外部)

状态转换(移动→停止→开门→关门→继续移动)

无效请求处理(如超出楼层范围)
题目二分析:

新增 Passenger 类
职责:封装乘客的起点楼层和目标楼层,计算请求方向。

变化:

取代了之前的 ExternalRequest 和内部楼层请求的简单 Integer 类型。

通过 getDirection() 判断请求方向(UP/DOWN)。
同方向优先:

电梯运行时,优先响应与当前方向相同的 Passenger 请求。

空闲调度:

若电梯空闲,选择距离最近的请求(getClosest())。

停靠条件变化
shouldStop() 判断:

内部请求:Passenger.destinationFloor == currentFloor。

外部请求:Passenger.sourceFloor == currentFloor 且方向匹配。

无效请求处理
楼层校验:
Elevator.isValidFloor() 检查请求是否在 [minFloor, maxFloor] 范围内。

无效请求直接被丢弃。
题目三分析:
题目三对核心类职责更加明确细化,如| Passenger类完全取代原始设计中的ExternalRequest,统一用sourceFloor和destinationFloor区分内外请求

在开发电梯调度系统时,遇到了多个问题。这些问题不仅影响了代码的质量,还降低了系统的可维护性和扩展性。以下是详细的反思和具体的解决方案:
问题:

一:将请求处理、电梯移动、队列管理等逻辑全部塞入Elevator类,导致代码臃肿。

二:混淆Controller和RequestQueue的职责,例如在队列类中实现调度算法。

三:请求方向和移动方向之间的关系用代码实现模糊,例如同方向时可能先响应反方向导致结果错误或超时。

四:测试用例比较简单自己调试找不出错误。

五:代码冗杂,修改费力费时,不方便检查,可读性差。

六:对类图的理解不够。

反思与解决方案:

一:遇到难题更要多钻研多研究,加强对类图的理解与思考

二:划分个类的职责,避免单类职责过重。

三:减少代码的多层嵌套,易于后期修改和加强,清晰界定职责:确保Controller专注于决策和调度,而RequestQueue仅用于管理和存储请求。这样可以避免逻辑上的混淆。
`import javax.print.DocFlavor;
import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;
enum Direction {
UP, DOWN, IDLE
}
enum State{
STOPPED,
MOVE,
}
class Elevator
{
private int currentFloor;
private Direction direction;
private State state;
private int maxFloor;
private int minFloor;
public Elevator(){
this.minFloor=1;
this.maxFloor=0;
}
public Elevator(int maxFloor,int minFloor){
this.minFloor=minFloor;
this.maxFloor=maxFloor;
}
public int getMinFloor(){
return minFloor;
}
public void setMinFloor(int minFloor){
this.minFloor=minFloor;
}
public int getMaxFloor(){
return maxFloor;
}
public void setMaxFloor(int maxFloor){
this.maxFloor=maxFloor;
}
public Direction getDirection(){
return direction;
}
public void setDirection(Direction direction){
this.direction=direction;
}
public State getState(){
return state;
}
public void setState(State state){
this.state=state;
}
public int getCurrentFloor(){
return currentFloor;
}
public void setCurrentFloor(int currentFloor){
this.currentFloor=currentFloor;
}
public boolean isValidFloor(int floor){
if(floor>maxFloor||floor<minFloor) return false;
else return true;
}
}
class ExternalRequest {

private Integer floor;
private Direction direction;

public ExternalRequest(Integer floor, Direction direction) {
    this.floor = floor;
    this.direction = direction;
}

public Integer getFloor() {
    return floor;
}

public Direction getDirection() {
    return direction;
}

}
class RequestQueue {
private LinkedList internalRequests = new LinkedList<>();
private LinkedList externalRequests = new LinkedList<>();
private RequestQueue request;
public RequestQueue(){

}
public RequestQueue getRequestInstance(){
    return request;
}

public LinkedList<Integer> getInternalRequests() {
    return internalRequests;
}

public void setInternalRequests(LinkedList<Integer> internalRequests) {
    this.internalRequests = internalRequests;
}

public LinkedList<ExternalRequest> getExternalRequests() {
    return externalRequests;
}

public void setExternalRequests(LinkedList<ExternalRequest> externalRequest) {
    this.externalRequests = externalRequest;
}
public void addInternalRequests(int floor){
    internalRequests.add(floor);
}
public void addExternalRequests(int floor, Direction direction){
    ExternalRequest FAndD=new ExternalRequest(floor,direction);
    externalRequests.add(FAndD);
}

}
class Passenger {
private Integer sourceFloor;
private Integer destinationFloor;

public Passenger() {
}

public Passenger(Integer sourceFloor, Integer destinationFloor) {
    this.sourceFloor = sourceFloor;
    this.destinationFloor = destinationFloor;
}

public Passenger(Integer destinationFloor) {
    this.destinationFloor = destinationFloor;
}

public Integer getSourceFloor() {
    return sourceFloor;
}

public void setSourceFloor(Integer sourceFloor) {
    this.sourceFloor = sourceFloor;
}

public Integer getDestinationFloor() {
    return destinationFloor;
}

public void setDestinationFloor(Integer destinationFloor) {
    this.destinationFloor = destinationFloor;
}

public Direction getDirection() {
    if (destinationFloor == null || sourceFloor == null) {
        return Direction.IDLE;
    }
    return destinationFloor > sourceFloor ? Direction.UP : Direction.DOWN;
}

}
class Controller{
private Elevator elevator;
private RequestQueue queue;
public Controller(){

}
public Controller(Elevator elevator,RequestQueue queue){
    this.elevator=elevator;
    this.queue=queue;
}

public Elevator getElevator() {
    return elevator;
}

public void setElevator(Elevator elevator) {
    this.elevator = elevator;
}

public RequestQueue getQueue() {
    return queue;
}

public void setQueue(RequestQueue queue) {
    this.queue = queue;
}
public void processRequest(){
    while(queue.getExternalRequests().isEmpty()||queue.getInternalRequests().isEmpty()){
        determineDirection();//处理方向
        move();//处理移动
    }
    elevator.setDirection(Direction.IDLE);//处理完请求后电梯设置为静止
    System.out.println("Current Floor: " + elevator.getCurrentFloor() + " Direction: " + elevator.getDirection());
}
public void determineDirection(){
    if(elevator.getDirection()== Direction.IDLE){
        if(queue.getExternalRequests().isEmpty()){//
            ExternalRequest extReq = queue.getExternalRequests().peek();
            Passenger nextRequest = new Passenger(extReq.getFloor());
            if(nextRequest.getSourceFloor()>elevator.getCurrentFloor()){
                elevator.setDirection(Direction.UP);
            }
            else if(nextRequest.getSourceFloor()<elevator.getCurrentFloor()){
                elevator.setDirection(Direction.DOWN);
            }
            else {
                if (nextRequest.getDestinationFloor() > nextRequest.getSourceFloor()) {
                    elevator.setDirection(Direction.UP);
                } else {
                    elevator.setDirection(Direction.DOWN);
                }
            }
        }
        else if (!queue.getInternalRequests().isEmpty()) {
            ExternalRequest extReq = queue.getExternalRequests().peek();
            Passenger nextRequest = new Passenger(extReq.getFloor());
            if (nextRequest.getDestinationFloor() > elevator.getCurrentFloor()) {
                elevator.setDirection(Direction.UP);
            } else {
                elevator.setDirection(Direction.DOWN);
            }
        }
    }
}
public boolean move(){

}

}
总结:

以上代码移动方法仍未实现,但是本人会努力坚持写完该程序题目。提高对Java编程的理解和掌握.

posted @ 2025-04-20 20:53  yukinoacb  阅读(55)  评论(0)    收藏  举报