第一次博客作业

电梯三次作业迭代总结

前言

1.知识点总结:第一次电梯作业只有一个类,侧重于理解电梯的运行方式,了解其中的算法和楼层的输入和到达目标楼层的输出方式,以及如何处理内部楼层和外部楼层之间的优先关系。第二次电梯作业则是在第一次电梯作业的基础上进行选代优化,首先由原来的一个类按照功能与职责分成了多个类,类之间的关系则要符合迪米特法则,并在第一次的基础上在输入方面提出来更全面的要求,要处理乘客输入请求有误和输入请求重复的情况,第三次电梯作业的情况则是修改了乘客外部请求的输入方式,与第二次电梯相比没有大多变化
2.题量与难度分析:题量适宜,时间充分,难度情况主要是写第一次电梯时题目理解出现了障碍,下意识把其与生活上的电梯进行联系导致一开始的算法出现问题,在经过错误的尝试后和老师的指点与同学的沟通下成功解决了问题,第一次电梯与第三次电梯主要是在第一次电梯的基础上进行选代,难度不大

设计与分析

第一次电梯源代码分析

点击查看代码
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main{
    public static void main(String[]args){
        int minFloor;
        int maxFloor;
        String []a = new String[100];
        Scanner input = new Scanner(System.in);
        minFloor = input.nextInt();
        maxFloor = input.nextInt();
        input.nextLine();
        a[0] = input.nextLine();
        int i = 0;
        while(a[i].charAt(0)!='e'&&a[i].charAt(0)!='E'){
            a[++i] = input.nextLine();
        }
        Lift lift = new Lift();
        String []b = lift.changeNumber(a);
        String []upDown = lift.upDown(a);
        int []number = lift.change(b);
        lift.out(number,upDown);
        lift.control();
    }
}
class Lift{
    private int q = 0;
    private int w = 0;
    private int []num = new int [100];
    private int []out = new int [100];
    private int []uout = new int [100];
    private int []c = new int [100];
    private int floor1 = 0;
    public String[] changeNumber(String []a){
        int k = 0;
        String regstr = "(\\d+)";
        Pattern pattern = Pattern.compile(regstr);
        String []b = new String[100];
        for(String str:a){
            if(a[k].charAt(0)=='e'||a[k].charAt(0)=='E'){
                break;
            }
            Matcher matcher = pattern.matcher(str);
            while(matcher.find()){
                b[k] = matcher.group();
                k++;
            }
        }
        return b;
    }
    public String[] upDown(String[]a){
        int j = 0;
        String regstr1 = "([A-Z]+)";
        Pattern pattern1 = Pattern.compile(regstr1);
        String []upDown = new String[100];
        for(String str:a){
            if(a[j].charAt(0)=='e'||a[j].charAt(0)=='E'){
                upDown[j] = "end";
                break;
            }
            Matcher matcher1 = pattern1.matcher(str);
            if(matcher1.find()){
                upDown[j] = matcher1.group();
                j++;
            }
            else{
                upDown[j] = "Symbol";
                j++;
            }
        }
        return upDown;
    }
    public int[]change(String[]a){
        int []b = new int[100];
        int length = 0;
        int i = 0;
        int j = 0;
        for(String str:a){
            if(str==null){
                break;
            }
            length = str.length();
            while(j<length){
                b[i]+=(int)((str.charAt(j)-'0')*Math.pow(10,length-j-1));
                j++;
            }
            i++;
            j = 0;
        }
        return b;
    }
    public void out(int []a,String []b){
        int i = 0;
        int j = 0;
        int k = 0;
        for(int d:a){
            if(d==0){
                break;
            }
            if(!b[k].equals("UP") && !b[k].equals("DOWN")){
                out[i] = d;
                i++;
                q++;
            }
            else {
                uout[j] = d;
                if(b[k].equals("UP")){
                    c[j] = 1;
                }
                else{
                    c[j] = 2;
                }
                j++;
                w++;
            }
            k++;
        }
    }
    public void control(){
        int k = 1;//当前状态(1为向上,2为向下)
        int m =1;//当前外部请求方向
        int floor = 1;//当前所处楼层
        int i = 0;//要处理的外部楼层所在数组位置
        int j = 0;//要处理的内部楼层所在数组位置
        int temp = 0;//(外部指令与当前楼层之差)
        int temp1 = 0;//(内部指令与当前楼层之差)
        while(q!=0||w!=0){//(q为外部指令数目,w为内部指令数目)
            if(out[i]>floor){
            m = 1;
        }
        else {
            m = 2;
        }
            if(floor>out[i]&&floor>uout[j]){
                k = 2;
            }
            temp = Math.abs(out[i]-floor);
            temp1 = Math.abs(uout[j]-floor);
            if(q!=0&&w!=0){
            if(temp1<temp){
                if(k==c[j]){
                print1(floor,uout[j],k);
                floor = uout[j];
                j++;
                w--;
                }
                else if(k==m){
                    print1(floor,out[i],k);
                    floor = out[i];
                    i++;
                    q--;
                }
                else{
                    print1(floor,uout[j],k);
                    floor = uout[j];
                    k = c[j];
                    j++;
                    w--;
                }
            }
             if(temp1>temp){
                if(k==m){
                    print1(floor,out[i],k);
                    floor = out[i];
                    i++;
                    q--;
                }
                else if(k==c[j]){
                    print1(floor,uout[j],k);
                    floor = uout[j];
                    j++;
                    w--;
                }
                else{
                    print1(floor,out[i],k);
                    i++;
                    q--;
                }
            }
            }
            else if(q==0){
                print1(floor,uout[j],k);
                floor = uout[j];
                k = c[j];
                j++;
                w--;
            }
            else if(w==0){
                print1(floor,out[i],k);
                floor = out[i];
                k = m;
                i++;
                q--;
            }
            
    }
}
    public void print1(int floor,int sfloor,int k){
        while(floor!=sfloor){
            if(floor!=floor1){
            if(k==1){
                System.out.printf("Current Floor: %d Direction: UP\n",floor);
            }
            else{
                System.out.printf("Current Floor: %d Direction: DOWN\n",floor);
            }
            }
            if(floor<sfloor){
                floor++;
                k=1;
            }
            else{
                floor--;
                k=2;
            }
        }
           if(k==1){
               System.out.printf("Current Floor: %d Direction: UP\n",floor);
           }
        else{
            System.out.printf("Current Floor: %d Direction: DOWN\n",floor);
        }
            System.out.printf("Open Door # Floor %d\n",floor);
            System.out.println("Close Door");
        floor1 =floor;
    }
}




电梯调度方法解析

1确认电梯此时的状态
2计算外部请求和当前楼层的差值与内部请求和当前楼层的差值
3结合外部请求的方向进行判断执行哪个请求
4前往目标楼层
(要考虑只有内部请求和只有外部请求的情况)

类图

类设计分析
1 第一次电梯作业只设计了一个类,类中功能繁多
2 通过control方法来调动其他方法来实现电梯功能

代码分析

分析与心得
分析
1分支语句占比23.7% if-else语句占比太多
2含注释占比2.2%,注释比例较低
3最大复杂度19 代码复杂度偏高
4平均代码块深度3.24
5平均复杂度8.33
心得
1分支语句占比太多,要优化算法逻辑和修改代码
2注释比例太低,不易于代码的阅读和维护
3代码嵌套程度偏高,复杂度大,不易于理解与维护,要尽量避免嵌套

第二次源代码分析

点击查看代码
import java.util.Scanner;
public class Main{
    public static void main(String[]args) {
        Scanner input = new Scanner(System.in);
        String data;
        int floor = 0;
        Direction direction = null;
        int minFloor;
        int maxFloor;
        String request = "";
        LinkedList<String> list = new LinkedList<>();
        data = input.nextLine();
        while (!data.equalsIgnoreCase("End")) {
            list.add(data);
            data = input.nextLine();
        }
        minFloor = Integer.parseInt(list.get(0));
        maxFloor = Integer.parseInt(list.get(1));
        Elevator elevator = new Elevator(minFloor,maxFloor);
        RequestQueue requestQueue = new RequestQueue();
        Controller controller = new Controller(elevator,requestQueue);
        for (int i = 2; i < list.size(); i++) {
            request = list.get(i);
            if(request.contains(",")){
            if (!request.matches("<\\d+,\\s*(UP|DOWN)>")) {
                System.out.println("Wrong Format");}
            String[] parts = request.replaceAll("[<>]", "").split(",");
            floor = Integer.parseInt(parts[0].trim());
            direction = Direction.valueOf(parts[1].trim().toUpperCase());
            if(controller.getElevator().isValidFloor(floor)) {
                controller.getQueue().addExternalRequest(floor, direction);
            }
             }
            else{
                if(!request.matches("<\\d+>")){
                    System.out.println("Wrong Format");
                }
                floor = Integer.parseInt(request.replaceAll("[<>]",""));
                if(controller.getElevator().isValidFloor(floor)) {
                    controller.getQueue().addInternalRequest(floor);
                }
            }
        }
        controller.processRequest();
        System.out.println("Current Floor: 1 Direction: UP");
        controller.process1Requests();
    }
}
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;
    }
}//存储外部请求,因为链表添加只能加一个

enum Direction {
    UP,
    DOWN,
    IDLE,//无方向的
}
enum State{
    MOVING,
    STOPPED,
}
class RequestQueue{
    LinkedList<Integer> internalRequests = new LinkedList<>();
    LinkedList<ExternalRequest> externalRequests = new LinkedList<>();
    public RequestQueue(){

    }
    public RequestQueue getQueueInstance(){
        return new RequestQueue();
    }
    public LinkedList<Integer> getInternalRequests(){
        return internalRequests;
    }
    public void setInternalRequest(LinkedList<Integer>internalRequests){
        this.internalRequests =internalRequests;
    }
    public LinkedList<ExternalRequest> getExternalRequests(){
        return externalRequests;
    }
    public void setExternalRequests(LinkedList<ExternalRequest> externalRequests){
        this.externalRequests = externalRequests;
    }
    public void addInternalRequest(int floor){
       internalRequests.add(floor);
    }
    public void addExternalRequest(int floor,Direction direction){
         ExternalRequest externalRequest = new ExternalRequest(floor,direction);
         externalRequests.add(externalRequest);
    }
}
class Elevator{
    private int currentFloor = 1;
    private Direction direction = Direction.UP;
    private State state;
    private int maxFloor;
    private int minFloor;
    public Elevator(){

    }
    public Elevator (int minFloor,int maxFloor){
        this.minFloor = minFloor;
        this.maxFloor = maxFloor;
    }
    public Elevator getElevatorInstance(int minFloor,int maxFloor){
        return new Elevator(minFloor,maxFloor);
    }
    public int getCurrentFloor(){
        return currentFloor;
    }
    public void setCurrentFloor(int currentFloor){
        this.currentFloor = currentFloor;
    }
    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 getMaxFloor() {
        return maxFloor;
    }
    public int getMinFloor(){
        return minFloor;
    }
    public boolean isValidFloor(int floor){//判断输入楼层是否合法
        if(floor>=minFloor&&floor<=maxFloor){
            return true;
        }
        else {
            return false;
        }
    }
}
class Controller {
    Elevator elevator = new Elevator();
    RequestQueue queue = new RequestQueue();

    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() {//加工需求
        LinkedList<Integer> internalRequests = queue.getInternalRequests();
        int i = 0;
        while (i < internalRequests.size() - 1) {
            if (internalRequests.get(i).equals(internalRequests.get(i + 1))) {
                internalRequests.remove(i + 1);
            } else {
                i++;
            }
        }
        LinkedList<ExternalRequest> externalRequests = queue.getExternalRequests();
        i = 0;
        while (i < externalRequests.size() - 1) {
            if (externalRequests.get(i).getFloor().equals(externalRequests.get(i + 1).getFloor()) && externalRequests.get(i).getDirection().equals(externalRequests.get(i + 1).getDirection())) {
                externalRequests.remove(i + 1);
            } else {
                i++;
            }
        }

    }//删除重复元素

    public void determinDirection() {//确定方向
        if (elevator.getDirection() == Direction.UP) {
            elevator.setDirection(Direction.DOWN);
        } else {
            elevator.setDirection(Direction.UP);
        }
    }

    public void move() {//移动
        int currentFloor = elevator.getCurrentFloor();
        Direction direction = elevator.getDirection();
        if (direction == Direction.UP) {
            currentFloor++;
            System.out.printf("Current Floor: %d Direction: UP\n", currentFloor);
            elevator.setCurrentFloor(currentFloor);
        }
        if (direction == Direction.DOWN) {
            currentFloor--;
            System.out.printf("Current Floor: %d Direction: DOWN\n", currentFloor);
            elevator.setCurrentFloor(currentFloor);
        }
    }

    public boolean shouldStop(int floor) {//应该停下来的情况
        int currentfloor = elevator.getCurrentFloor();
        if (currentfloor == floor) {
            elevator.setState(State.STOPPED);
            return true;
        } else {
            return false;
        }
    }

    public Integer getClosest() {//对比得到最近的楼层
        int infloor = queue.getInternalRequests().peek() - elevator.getCurrentFloor();
        int exfloor = queue.getExternalRequests().peek().getFloor() - elevator.getCurrentFloor();
        int floor = 0;
        if (infloor > 0 && exfloor > 0) {
            if (elevator.getDirection() == Direction.UP) {
                if (exfloor >= infloor) {
                    floor = queue.getInternalRequests().peek();
                } else if (exfloor < infloor) {
                    if (queue.getExternalRequests().peek().getDirection() == Direction.UP) {
                        floor = queue.getExternalRequests().peek().getFloor();
                    } else {
                        floor = queue.getInternalRequests().peek();
                    }
                }
            } else if (elevator.getDirection() == Direction.DOWN) {
                floor = 0;
            }
        } else if (infloor > 0 && exfloor < 0) {
            if (elevator.getDirection() == Direction.UP) {
                floor = queue.getInternalRequests().peek();
            } else if (elevator.getDirection() == Direction.DOWN) {
                if (queue.getExternalRequests().peek().getDirection() == Direction.DOWN) {
                    floor = queue.getExternalRequests().peek().getFloor();
                } else {
                    floor = -1;
                }
            }
        } else if (infloor < 0 && exfloor > 0) {
            if (elevator.getDirection() == Direction.UP) {
                if (queue.getExternalRequests().peek().getDirection() == Direction.UP) {
                    floor = queue.getExternalRequests().peek().getFloor();
                } else {
                    floor = -1;
                }
            }
            if (elevator.getDirection() == Direction.DOWN) {
                floor = queue.getInternalRequests().peek();
            }
        } else if (infloor < 0 && exfloor < 0) {
            if (elevator.getDirection() == Direction.UP) {
                floor = 0;
            } else if (elevator.getDirection() == Direction.DOWN) {
                if (exfloor < infloor) {
                    floor = queue.getInternalRequests().peek();
                }
                if (exfloor >= infloor) {
                    if (queue.getExternalRequests().peek().getDirection() == Direction.DOWN) {
                        floor = queue.getExternalRequests().peek().getFloor();
                    } else {
                        floor = queue.getInternalRequests().peek();
                    }
                }

            }
        }
        return floor;
    }

    public void openDoors() {//应该开门的情况
        int currentfloor = elevator.getCurrentFloor();
        System.out.printf("Open Door # Floor %d\n", currentfloor);
        System.out.println("Close Door");
    }

    public void removeRequests(int currentFloor) {
        if (!queue.getInternalRequests().isEmpty()) {
            if (queue.getInternalRequests().peek() == currentFloor) {
                queue.getInternalRequests().poll();
            }
        }
        if (!queue.getExternalRequests().isEmpty()) {
            if (queue.getExternalRequests().peek().getFloor() == currentFloor) {
                queue.getExternalRequests().poll();
            }
        }
    }

public void process1Requests() {
        while (!queue.getExternalRequests().isEmpty() || !queue.getInternalRequests().isEmpty()) {
            if (!queue.getExternalRequests().isEmpty() && !queue.getInternalRequests().isEmpty()) {
                int floor = getClosest();
                if (floor == 0) {
                    determinDirection();
                } else if (floor == -1) {
                    while (true) {
                        if (shouldStop(queue.getExternalRequests().peek().getFloor())) {
                            removeRequests(queue.getExternalRequests().peek().getFloor());
                            openDoors();
                            break;
                        }
                        move();
                    }
                    determinDirection();
                } else {
                    while (true) {
                        if (shouldStop(floor)) {
                            removeRequests(floor);
                            openDoors();
                            break;
                        }
                        move();
                    }
                }
            } else if (!queue.getExternalRequests().isEmpty() && queue.getInternalRequests().isEmpty()) {
                int a = queue.getExternalRequests().peek().getFloor() - elevator.getCurrentFloor();
                if (a >= 0) {
                    if (elevator.getDirection() == Direction.UP) {
                        while (true) {
                            if (shouldStop((queue.getExternalRequests().peek().getFloor()))) {
                                if (!queue.getExternalRequests().isEmpty()) {
                                    if (queue.getExternalRequests().peek().getDirection() == Direction.DOWN) {
                                        determinDirection();
                                    }
                                }
                                removeRequests(queue.getExternalRequests().peek().getFloor());
                                openDoors();
                                break;
                            }
                            move();
                        }
                    } else {
                        determinDirection();
                    }
                }
                if (a < 0) {
                    if (elevator.getDirection() == Direction.DOWN) {
                        while (true) {
                            if (shouldStop((queue.getExternalRequests().peek().getFloor()))) {
                                if (!queue.getExternalRequests().isEmpty()) {
                                    if (queue.getExternalRequests().peek().getDirection() == Direction.UP) {
                                        determinDirection();
                                    }
                                }
                                removeRequests(queue.getExternalRequests().peek().getFloor());
                                openDoors();
                                break;
                            }
                            move();
                        }
                    } else {
                        determinDirection();
                    }
                } else if (!queue.getInternalRequests().isEmpty() && queue.getExternalRequests().isEmpty()) {
                    int b = queue.getInternalRequests().peek() - elevator.getCurrentFloor();
                    if (b >= 0) {
                        if (elevator.getDirection() == Direction.UP) {
                            while (true) {
                                if (shouldStop((queue.getInternalRequests().peek()))) {
                                    removeRequests(queue.getInternalRequests().peek());
                                    openDoors();
                                    break;
                                }
                                move();
                            }
                        }
                        if (elevator.getDirection() == Direction.DOWN && b != 0) {
                            determinDirection();
                        }
                    }
                    if (b < 0) {
                        if (elevator.getDirection() == Direction.DOWN) {
                            while (true) {
                                if (shouldStop((queue.getInternalRequests().peek()))) {
                                    removeRequests(queue.getInternalRequests().peek());
                                    openDoors();
                                    break;
                                }
                                move();
                            }
                        }
                        if (elevator.getDirection() == Direction.UP) {
                            determinDirection();
                        }
                    }
                }
            }
        }
    }
}

电梯调度分析

第二次电梯算法在第一次电梯算法的基础上进行了优化和完善
1要考虑开两次门的情况
2从考虑请求与当前楼层的绝对值变成考虑差值的情况,优化了方向的判断

类图

类图设计分析
1相比于第一次电梯设计,将其按功能分成了多个类
2control从方法变成了一个类,通过这个类来调其他类的方法来实现电梯功能,符合迪米特法则

代码分析

分析与心得
分析
1分支语句占比30.9%,相比上次增加
2含注释语句数占比2.4%
3最大复杂度高达41,太过复杂
4平均代码复杂度3.27
5平均复杂度3.53
心得
1相比与上一次,对算法的逻辑进行了完善,导致分支语句数增加
2最大复杂度太高,要进行优化改进
平均代码复杂度相比上次下降明显

第三次电梯源代码分析

点击查看代码
import java.util.Scanner;
public class Main{
    public static void main(String[]args) {
        Scanner input = new Scanner(System.in);
        String data;
        Direction direction = null;
        String request = "";
        LinkedList<String> list = new LinkedList<>();
        data = input.nextLine();
        while (!data.equalsIgnoreCase("End")) {
            list.add(data);
            data = input.nextLine();
        }
        int beforefloor;
        int minFloor = Integer.parseInt(list.get(0));
        int maxFloor = Integer.parseInt(list.get(1));
        Elevator elevator = new Elevator(minFloor,maxFloor);
        RequestQueue requestQueue = new RequestQueue();
        Controller controller = new Controller(elevator,requestQueue);
        for (int i = 2; i < list.size(); i++) {
            request = list.get(i);
            if(request.contains(",")){
                if (!request.matches("<\\d+,\\s*\\d+>")) {
                    System.out.println("Wrong Format");}
                String[] parts = request.replaceAll("[<>]", "").split(",");
                 beforefloor = Integer.parseInt(parts[0].trim());
                int underfloor = Integer.parseInt(parts[1].trim());
                if(beforefloor>underfloor){
                    direction = Direction.DOWN;
                }
                else{
                    direction = Direction.UP;
                }
                if(controller.getElevator().isValidFloor(beforefloor)&&controller.getElevator().isValidFloor(underfloor)) {
                    controller.getQueue().addExternalRequest(beforefloor, underfloor,direction);
                }
            }
            else{
                if(!request.matches("<\\d+>")){
                    System.out.println("Wrong Format");
                }
                beforefloor = Integer.parseInt(request.replaceAll("[<>]",""));
                if(controller.getElevator().isValidFloor(beforefloor)) {
                    controller.getQueue().addInternalRequest(beforefloor);
                }
            }
        }
        controller.processRequest();
        System.out.println("Current Floor: 1 Direction: UP");
        controller.process1Requests();
    }
}
enum Direction {
    UP,
    DOWN,
}
enum State{
    STOPPED,
}
class Passenger {
    private Integer floor;
    private Integer underfloor;
    private Direction direction;
    public Passenger(Integer floor,Integer underfloor,Direction direction){
        this.floor = floor;
        this.underfloor = underfloor;
        this.direction = direction;
    }
    public Integer getFloor(){
        return floor;
    }
    public Integer getUnderfloor(){
        return underfloor;
    }
    public Direction getDirection(){
        return direction;
    }
}//存储外部请求,因为链表添加只能加一个 ·
class RequestQueue{
    LinkedList<Integer> internalRequests = new LinkedList<>();
    LinkedList<Passenger> passengers = new LinkedList<>();
    public RequestQueue(){

    }
    public LinkedList<Integer> getInternalRequests(){
        return internalRequests;
    }
    public LinkedList<Passenger> getExternalRequests(){
        return passengers;
    }
    public void addInternalRequest(int floor){
        internalRequests.add(floor);
    }
    public void addExternalRequest(int floor,int underfloor,Direction direction){
        Passenger passenger = new Passenger(floor,underfloor,direction);
        passengers.add(passenger);
    }
}
class Elevator{
    private int currentFloor = 1;
    private Direction direction = Direction.UP;
    private State state;
    private int maxFloor;
    private int minFloor;
    public Elevator(){

    }
    public Elevator (int minFloor,int maxFloor){
        this.minFloor = minFloor;
        this.maxFloor = maxFloor;
    }
    public int getCurrentFloor(){
        return currentFloor;
    }
    public void setCurrentFloor(int currentFloor){
        this.currentFloor = currentFloor;
    }
    public Direction getDirection(){
        return direction;
    }
    public void setDirection(Direction direction){
        this.direction = direction;
    }
    public void setState(State state){
        this.state = state;
    }
    public boolean isValidFloor(int floor){//判断输入楼层是否合法
        if(floor>=minFloor&&floor<=maxFloor){
            return true;
        }
        else {
            return false;
        }
    }
}
class Controller {
    Elevator elevator = new Elevator();
    RequestQueue queue = new RequestQueue();
    public Controller(Elevator elevator, RequestQueue queue) {
        this.elevator = elevator;
        this.queue = queue;
    }

    public Elevator getElevator() {
        return elevator;
    }
    public RequestQueue getQueue() {
        return queue;
    }
    public void processRequest() {//加工需求
        LinkedList<Integer> internalRequests = queue.getInternalRequests();
        int i = 0;
        while (i < internalRequests.size() - 1) {
            if (internalRequests.get(i).equals(internalRequests.get(i + 1))) {
                internalRequests.remove(i + 1);
            } else {
                i++;
            }
        }
        LinkedList<Passenger> passengers = queue.getExternalRequests();
        i = 0;
        while (i < passengers.size() - 1) {
            if (passengers.get(i).getFloor().equals(passengers.get(i + 1).getFloor()) && passengers.get(i).getUnderfloor().equals(passengers.get(i + 1).getUnderfloor())) {
                passengers.remove(i + 1);
            } else {
                i++;
            }
        }

    }//删除重复元素

    public void determinDirection() {//确定方向
        if (elevator.getDirection() == Direction.UP) {
            elevator.setDirection(Direction.DOWN);
        } else {
            elevator.setDirection(Direction.UP);
        }
    }

    public void move() {//移动
        int currentFloor = elevator.getCurrentFloor();
        Direction direction = elevator.getDirection();
        if (direction == Direction.UP) {
            currentFloor++;
            System.out.printf("Current Floor: %d Direction: UP\n", currentFloor);
            elevator.setCurrentFloor(currentFloor);
        }
        if (direction == Direction.DOWN) {
            currentFloor--;
            System.out.printf("Current Floor: %d Direction: DOWN\n", currentFloor);
            elevator.setCurrentFloor(currentFloor);
        }
    }

    public boolean shouldStop(int floor) {//应该停下来的情况
        int currentfloor = elevator.getCurrentFloor();
        if (currentfloor == floor) {
            elevator.setState(State.STOPPED);
            return true;
        } else {
            return false;
        }
    }

    public Integer getClosest() {//对比得到最近的楼层
        int infloor = queue.getInternalRequests().peek() - elevator.getCurrentFloor();
        int exfloor = queue.getExternalRequests().peek().getFloor() - elevator.getCurrentFloor();
        int floor = 0;
        if (infloor > 0 && exfloor >= 0) {
            if (elevator.getDirection() == Direction.UP) {
                if (exfloor >infloor) {
                    floor = queue.getInternalRequests().peek();
                }
                else if (exfloor <= infloor) {
                    if (queue.getExternalRequests().peek().getDirection() == Direction.UP) {
                        floor = -2;
                        queue.addInternalRequest(queue.getExternalRequests().peek().getUnderfloor());
                    } else {
                        floor = queue.getInternalRequests().peek();
                    }
                }
            } else if (elevator.getDirection() == Direction.DOWN) {
            }
        } else if (infloor > 0 && exfloor <= 0) {
            if (elevator.getDirection() == Direction.UP) {
                floor = queue.getInternalRequests().peek();
            } else if (elevator.getDirection() == Direction.DOWN) {
                if (queue.getExternalRequests().peek().getDirection() == Direction.DOWN) {
                    floor = -2;
                    queue.addInternalRequest(queue.getExternalRequests().peek().getUnderfloor());
                } else {
                    floor = -1;
                    queue.addInternalRequest(queue.getExternalRequests().peek().getUnderfloor());
                }
            }
        } else if (infloor < 0 && exfloor >= 0) {
            if (elevator.getDirection() == Direction.UP) {
                if (queue.getExternalRequests().peek().getDirection() == Direction.UP) {
                    floor = -2;
                    queue.addInternalRequest(queue.getExternalRequests().peek().getUnderfloor());
                } else {
                    floor = -1;
                    queue.addInternalRequest(queue.getExternalRequests().peek().getUnderfloor());
                }
            }
            if (elevator.getDirection() == Direction.DOWN) {
                floor = queue.getInternalRequests().peek();
            }
        } else if (infloor < 0 && exfloor <= 0) {
            if (elevator.getDirection() == Direction.UP) {
            } else if (elevator.getDirection() == Direction.DOWN) {
                if (exfloor < infloor) {
                    floor = queue.getInternalRequests().peek();
                }
                if (exfloor >= infloor) {
                    if (queue.getExternalRequests().peek().getDirection() == Direction.DOWN) {
                        floor = -2;
                        queue.addInternalRequest(queue.getExternalRequests().peek().getUnderfloor());
                    } else {
                        floor = queue.getInternalRequests().peek();
                    }
                }

            }
        }
        return floor;
    }

    public void openDoors() {//应该开门的情况
        int currentfloor = elevator.getCurrentFloor();
        System.out.printf("Open Door # Floor %d\n", currentfloor);
        System.out.println("Close Door");
    }

    public void removeRequests(int currentFloor) {
        if (!queue.getInternalRequests().isEmpty()) {
            if (queue.getInternalRequests().peek() == currentFloor) {
                queue.getInternalRequests().poll();
            }
        }
        if (!queue.getExternalRequests().isEmpty()) {
            if (!queue.getInternalRequests().isEmpty()&&queue.getExternalRequests().peek().getUnderfloor()!=queue.getInternalRequests().peek()) {
                if (elevator.getDirection() == Direction.UP) {
                    if (queue.getExternalRequests().peek().getFloor() == currentFloor && queue.getExternalRequests().peek().getUnderfloor() > currentFloor) {
                        queue.getExternalRequests().poll();
                    }
                } else {
                    if (queue.getExternalRequests().peek().getFloor() == currentFloor && queue.getExternalRequests().peek().getUnderfloor() < currentFloor) {
                        queue.getExternalRequests().poll();
                    }
                }
            } else {
                if (queue.getExternalRequests().peek().getFloor() == currentFloor) {
                    queue.getExternalRequests().poll();
                }

            }
        }
    }

    public void process1Requests() {
        while (!queue.getExternalRequests().isEmpty() || !queue.getInternalRequests().isEmpty()) {
            if (!queue.getExternalRequests().isEmpty() && !queue.getInternalRequests().isEmpty()) {
                int floor = getClosest();
                if(floor == -2) {
                    while (true) {
                        if (shouldStop(queue.getExternalRequests().peek().getFloor())) {
                            removeRequests(queue.getExternalRequests().peek().getFloor());
                            openDoors();
                            break;
                        }
                        move();
                    }
                }
                else if (floor == 0) {
                    determinDirection();
                } else if (floor == -1) {
                    while (true) {
                        if (shouldStop(queue.getExternalRequests().peek().getFloor())) {
                            openDoors();
                            break;
                        }
                        move();
                    }
                    determinDirection();
                    removeRequests(queue.getExternalRequests().peek().getFloor());
                } else {
                    while (true) {
                        if (shouldStop(floor)) {
                            removeRequests(floor);
                            openDoors();
                            break;
                        }
                        move();
                    }
                }
            } else if (!queue.getExternalRequests().isEmpty() && queue.getInternalRequests().isEmpty()) {
                int a = queue.getExternalRequests().peek().getFloor() - elevator.getCurrentFloor();
                if (a >= 0) {
                    elevator.setDirection(Direction.UP);
                }
                else {
                    elevator.setDirection(Direction.DOWN);
                }
                queue.addInternalRequest(queue.getExternalRequests().peek().getUnderfloor());
                while (true) {
                    if (shouldStop((queue.getExternalRequests().peek().getFloor()))) {
                        removeRequests(queue.getExternalRequests().peek().getFloor());
                        openDoors();
                        break;
                    }
                    move();
                }

            }else if (!queue.getInternalRequests().isEmpty() && queue.getExternalRequests().isEmpty()) {
                int b = queue.getInternalRequests().peek() - elevator.getCurrentFloor();
                if (b >= 0) {
                    elevator.setDirection(Direction.UP);
                }
                else {
                    elevator.setDirection(Direction.DOWN);
                }
                while (true) {
                    if (shouldStop((queue.getInternalRequests().peek()))) {
                        removeRequests(queue.getInternalRequests().peek());
                        openDoors();
                        break;
                    }
                    move();
                }
            }
        }
    }
}

电梯调度方法解析

1相比与前两次,输入方是进行了改变,外部请求从输入当前楼层和方向改变为输入当前楼层和目标楼层,但只要通过其的输入来得到外部请求的方向,则与前两次的算法相同,无需进行较大改进
2增加了执行外部请求时将其目标楼层放在内部请求末尾

类图

类图分析
1相比与第二次电梯作业,去除了外部请求类,增加了乘客类(其实二者并无太大差异)

代码分析

分析与心得
分析
1分支语句占比31.1%
2含注释行百分比 2.4%
3最大复杂度29
4平均代码块深度3.05
5平均复杂度4.24
心得
1最大复杂度下降
2平均复杂度较上次上升

踩坑心得

  • 不要一来就傻傻乎的写代码
    只有一开始把算法都想好再开始写才能事半功倍,否者只会事倍功半甚至代码全删从头来过(别问我怎么知道的)
  • 算法一定尽量设计的好一点
    一个好的算法关乎你写代码的进度和你代码的结构以及你后续代码的迭代和优化,一个好的算法会帮你避免好多别人可能会遇到的问题
  • 多和同学沟通,别一个人独自冲锋
    别一昧的自己死钻牛角尖,要多和同学间交流看法,可以解决一些你认为棘手的问题
  • 学会自己设置测试用例和进行单步调试
    代码逻辑上的问题别人帮不了你,只能自己解决,而单步调试就是一个很好的方法,它能帮你理清你的代码逻辑来避免你的代码陷入死循环和解决你的代码逻辑问题(一定要会哦)

理论上说了那么多看一些具体的例子

例子一

单步调试的灵活运用
这里的部分正确就是因为我算法的问题(捂脸)导致第一个测试点一直过不去当时我还想通过增加一些特殊情况来通过这个测试点,后面发现怎么加都过不了。
后面最后决定大刀阔斧换了一个算法,然后再不断地完善中就遇到了运行超时问题,而运行超时问题就是通过单步调试解决最后完成这次作业的,单步调试的过程
需要耐心和恒心来先确定大概错的位置,后确定具体的位置存在的逻辑问题进行改正(天知道我在这里花了多久)

例子二

自己设置测试用例的必要性
这里的部分正确就不是算法逻辑的问题,而是我考虑不周到的问题(我为什么知道,请看下文),首先通过提示我们只能看到答案错误和运行超时两个提醒,而题目给的
测试用例我都输出的没用任何问题,所以这时让人摸不到任何思绪,后面在我灵光一闪下怀疑是特殊输入的问题,最后发现是只输入外部请求会发生问题,后面对代码进行
改进最后解决了问题,但我这种碰一碰的法子并不可取,实际应该统筹全部情况来进行排除分析,不过这也能体现出自己设置测试用例的重要性

改进建议

算法设计改进(重要的事情说多遍)
1在经过这次电梯作业后我越发的体会到了一个好的算法的优越之处(所以设计的时候一定要三思啊),不然就会像我一样太多if-else改都不好改,等到后面迭代
的时候你就知道快乐了,反正我是不想在体会了
2多增加一点注释,方便后面的改进和迭代,不然到后面自己都要好好思考一下自己写的什么东西
3方法的功能要更明确一点,不要因为单纯的完成题目而增加或减少方法的功能,使其符合单一职责原则

总结

通过这次电梯作业,让我对JAVA有了进一步认识,认识到JAVA和C语言的不同,不能带着学习C语言的思维来学习JAVA
这次作业我学习到了正则表达式的使用和队列的使用,以及如何设计类和处理类与类之间的关系,使其符合单一职责原则,迪米特法则和·如何正确全面的设置测试用例和单步调试
的至关作用

通过这次的作业,我发现自己在如何处理方法之间的关系还是不是很到位,还有在降低代码复杂度方面还要进一步努力,尽量避免if-else代码的嵌套影响代码的阅读性
还有自己实际解决问题的能力还是偏弱,在第一次电梯作业和第二次电梯作业耗时太长,以及一些JAVA中自带的方法了解不够深,要增强这方面自己的能力来方便和加快
自己的解题速度

改进与建议
希望线上课能讲的再细致一点,听完线上课后,线上线下课的联动能早点来加深印象

posted @ 2025-04-20 14:12  张海斌  阅读(39)  评论(0)    收藏  举报