Java implementation of two elevator operation (semi-finished products)

Purpose and results:

The living floor is a two family elevator on the first floor. Every time I get on and off the elevator, I am thinking about the specific operation logic of the elevator and how the two elevators operate interactively. When I was free, I wrote a similar one. Before the project went online recently, I mainly tested and modified bug s. I started to design it. As expected, it was an independent villa. The finished product was a thatched earth house. At first, I was full of energy. Later, I stopped for a week and looked at the code I wrote. Even if there were comments, it was tasteless, Without the energy to start, the final simulation of elevator operation is also a hasty end.

This example is a small demo of the console. Although it is completed roughly with the attitude of starting and ending, the designated elevator is selected. The elevator ascending to the designated floor and the elevator descending still need to be optimized. For example, at present, the 4th floor, the 6th floor and the 9th floor have to go downstairs. How to carry out the elevator is something I have considered and involved in the design of entity classes, but I am too lazy to operate. It needs to be improved by interested partners.

Demand explanation

  1. Two step elevator, sharing up and down buttons.
  2. The basic principle of elevator operation is to give priority to the right elevator under the same conditions.
  3. The possible combination of the specific operation of the two-step elevator is also lost because it is casually written on A4 paper. You can observe the floors of the two-step elevator combination in your life and analyze it yourself.
  4. It can be obtained through analysis                                                                                                                     Each elevator shall have: operation direction, operation status, internal switch, target floor and current floor, which are shared globally; Users on each floor have: Up button, down button and the number of floors. (ps: I'm lazy and easy to define into an entity)
  5. Each time the elevator reaches the target floor, there will be a short "rest" time. One second after the door is closed, it will enter the "operation" state. At this time, click the up button, and the down button will not open the door.
  6. Each elevator has the principle of the highest floor and the lowest floor, that is, when the running direction of one elevator is the same, and there is demand at the higher floor or the lower floor, it is preferred to reach the highest floor or the lowest floor. (ps: I've been in the elevator and observed it. Do you understand it
  7. Residents in the elevator choose the designated floor to open the door. Users outside the elevator click the up or down button. If it is consistent with the direction of the running elevator, it should also open the door. (question: have you ever had the experience of going up when you got into the elevator and got to the negative floor first
  8. Think again if you are interested.

  With their own semi-finished products

I only wrote three classes:
RunLift.java is an entity class that defines the parameter fields of the above analysis;

RunLiftMethod.java is a method class that encapsulates all written methods;

Running.java is a running class, which is responsible for calling methods to run.

Interested partners to improve this poor semi-finished product QAQ.

RunLift.class

package com.soft.TwoOfLift;

import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

public class RunLift {

    private int floorMy; //Own floor
    private static HashMap<Integer,Integer> downOrUp = new HashMap<>(); //Up or down parameters: number of floors on the first floor, parameter 2, up and down stairs button 1 up and 2 down
    private static List<Integer> floorTargetLift; //What floor are you going to
    private static List<Integer> floorTargetRight; // What floor, right
    private static int floorNumRight; //Current right elevator floor
    private static int floorNumLeft; //Current left elevator floor
    private static int switchLeft;  //Elevator internal switch left 1 on 2 off
    private static int switchRight;  //Elevator internal switch right 1 on 2 off
    private static int runFlagRight; // 0 is not running 1 is running
    private static int runFlagLeft;  // 0 is not running 1 is running
    private static int runLeftDirection ; //Current left elevator running direction 1 up and 2 down
    private static int runRightDirection; //Currently, the elevator on the right side runs in the direction of 1 up and 2 down

    //Assign initial value to each floor button, and the default is 20 floors
    {
        for (int i = 0; i < 20; i++) {
            downOrUp.put(i+1,0);
        }
        floorNumLeft = 1;
        floorNumRight = 1;
    }

    public int getFloorMy() {
        return floorMy;
    }

    public void setFloorMy(int floorMy) {
        this.floorMy = floorMy;
    }

    public static HashMap<Integer, Integer> getDownOrUp() {
        return downOrUp;
    }

    public static void setDownOrUp(HashMap<Integer, Integer> downOrUp) {
        RunLift.downOrUp = downOrUp;
    }

    public static List<Integer> getFloorTargetLift() {
        return floorTargetLift;
    }

    public static void setFloorTargetLift(List<Integer> floorTargetLift) {
        RunLift.floorTargetLift = floorTargetLift;
    }

    public static List<Integer> getFloorTargetRight() {
        return floorTargetRight;
    }

    public static void setFloorTargetRight(List<Integer> floorTargetRight) {
        RunLift.floorTargetRight = floorTargetRight;
    }

    public static int getFloorNumRight() {
        return floorNumRight;
    }

    public static void setFloorNumRight(int floorNumRight) {
        RunLift.floorNumRight = floorNumRight;
    }

    public static int getFloorNumLeft() {
        return floorNumLeft;
    }

    public static void setFloorNumLeft(int floorNumLeft) {
        RunLift.floorNumLeft = floorNumLeft;
    }

    public static int getSwitchLeft() {
        return switchLeft;
    }

    public static void setSwitchLeft(int switchLeft) {
        RunLift.switchLeft = switchLeft;
    }

    public static int getSwitchRight() {
        return switchRight;
    }

    public static void setSwitchRight(int switchRight) {
        RunLift.switchRight = switchRight;
    }

    public static int getRunFlagRight() {
        return runFlagRight;
    }

    public static void setRunFlagRight(int runFlagRight) {
        RunLift.runFlagRight = runFlagRight;
    }

    public static int getRunFlagLeft() {
        return runFlagLeft;
    }

    public static void setRunFlagLeft(int runFlagLeft) {
        RunLift.runFlagLeft = runFlagLeft;
    }

    public static int getRunLeftDirection() {
        return runLeftDirection;
    }

    public static void setRunLeftDirection(int runLeftDirection) {
        RunLift.runLeftDirection = runLeftDirection;
    }

    public static int getRunRightDirection() {
        return runRightDirection;
    }

    public static void setRunRightDirection(int runRightDirection) {
        RunLift.runRightDirection = runRightDirection;
    }
}

RunLiftMethod.java

package com.soft.TwoOfLift;

import com.sun.org.apache.xerces.internal.impl.xs.SchemaNamespaceSupport;
import org.junit.runner.notification.RunListener;

import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

public class RunLiftMethod {
    private Scanner scanner = new Scanner(System.in) ;

    /**
     * Upper and lower floor selection
     */
    public void InputNum(RunLift runLift){
        int num = 0;
        boolean flag = true;
        while (flag){
            try{
                System.out.println("Please select:1 Up, 2 down");
                num = scanner.nextInt();
                if (num == 1 || num == 2){
                    flag = false;
                    //Set the number of floors to arrive
                    RunLift.getDownOrUp().put(runLift.getFloorMy(),num);
                    System.out.println("Current elevator floors:"+"left:"+RunLift.getFloorNumLeft() + "Layer right:"+RunLift.getFloorNumRight()+"layer");
                    System.out.println("The current floor is:"+ runLift.getFloorMy()+"layer");
                }else {
                    System.out.println("Please enter 1 or 2 ");
                    continue;
                }
            }catch (Exception e){
                System.out.println("Please enter an integer!");
                scanner.next();
            }
        }
    }

    //Enter the arrival floor
    public int scannerTargetFloor(RunLift runLift,int target) {
        int num = 0;
        boolean flag = true;
        while (flag){
            try{
                System.out.println("Please select the floor number to arrive:");
                num = scanner.nextInt();
                if (num >= 1 || num <= 20){
                    flag = false;
                    if (target == 1){ //1 is left, otherwise it is right
                        RunLift.getFloorTargetLift().add(num);
                        if (num > runLift.getFloorMy()){
                           RunLift.setRunLeftDirection(1);
                        }else {
                            RunLift.setRunLeftDirection(2);
                        }
                    }else {
                        RunLift.getFloorTargetRight().add(num);
                        if (num > runLift.getFloorMy()){
                            RunLift.setRunRightDirection(1);
                        }else {
                            RunLift.setRunRightDirection(2);
                        }
                    }
                    System.out.println("Current elevator floors:"+"left:"+RunLift.getFloorNumLeft() + "Layer right:"+RunLift.getFloorNumRight()+"layer");
                    System.out.println("The current floor is:"+ runLift.getFloorMy()+"layer");
                    return num;
                }else {
                    System.out.println("Please enter 1--20 Integer! ");
                    continue;
                }
            }catch (Exception e){
                System.out.println("Please enter an integer!");
                scanner.next();
            }
        }
        return 0;
    }

    /**
     * Arrive at the designated floor
     * @param runLift
     */
    public void toTargetFloor(RunLift runLift,int target) throws InterruptedException {
        //Target floor
        int targetLift = 0;
        //Obtain the height of each elevator from the target floor,
        int[] arr = this.toTargetNum(runLift.getFloorMy());
        int num = 0;
        //Current floor
        int floorMy = runLift.getFloorMy();
        if (target == 1 ){//Call left elevator
            int targetNum = RunLift.getFloorTargetLift().get(0);
            if (RunLift.getRunLeftDirection()==2){//If downstairs
                num = arr[0];
                for (int i = floorMy; i >= targetNum; i--) {
                    RunLift.setFloorNumLeft(i);
                    if (targetNum == i) {
                        System.out.println("Left elevator arrived:" + targetNum + "Layer.");
                        RunLift.setSwitchLeft(1);
                        System.out.println(targetNum + "The first floor has arrived and the elevator door is open.");
                        Thread.sleep(2000);
                        RunLift.setSwitchLeft(2);
                        System.out.println("Elevator door closed");
                        RunLift.setRunFlagLeft(0);//Declare the elevator as the end of operation
                        RunLift.getDownOrUp().put(targetNum, 0); //Declare that the floor up and down parameter is 0,

                    } else {
                        System.out.println("The left elevator is running, and the current position is:" + RunLift.getFloorNumLeft() + "Layer.");
                        RunLift.setSwitchLeft(2);
                    }
                    //Someone else was in the elevator and had to go down to their own floor
                    innerUerCheck(num, runLift,1);
                    //People on other floors also click up or down to judge the operation.
                    outUserCheck(num, runLift,1);
                }
            }else{
                num = arr[0];
                for (int i = floorMy; i <= targetNum; i++) {
                    RunLift.setFloorNumLeft(i);
                    if (targetNum == i) {
                        System.out.println("Left elevator arrived:" + targetNum + "Layer.");
                        RunLift.setSwitchLeft(1);
                        System.out.println(num + "The first floor has arrived and the elevator door is open.");
                        Thread.sleep(2000);
                        RunLift.setSwitchLeft(2);
                        System.out.println("Elevator door closed");
                        RunLift.setRunFlagLeft(0);//Declare the elevator as the end of operation
                        RunLift.getDownOrUp().put(targetNum, 0); //Declare that the floor up and down parameter is 0,

                    } else {
                        System.out.println("The right elevator is running, and the current position is:" + RunLift.getFloorNumRight() + "Layer.");
                        RunLift.setSwitchRight(2);
                    }
                    //Someone else was in the elevator and had to go down to their own floor
                    innerUerCheck(num, runLift,1);
                    //People on other floors also click up or down to judge the operation.
                    outUserCheck(num, runLift,1);
                }
            }
        }else { //Call right elevator
            int targetNum = RunLift.getFloorTargetRight().get(0);
            if (RunLift.getRunRightDirection()==2){//If downstairs
                num = arr[1];
                for (int i = floorMy; i >= targetNum; i--) {
                    RunLift.setFloorNumRight(i);
                    if (targetNum == i) {
                        System.out.println("The right elevator has arrived:" + targetNum + "Layer.");
                        RunLift.setSwitchRight(1);
                        System.out.println(targetNum + "The first floor has arrived and the elevator door is open.");
                        Thread.sleep(2000);
                        RunLift.setSwitchRight(2);
                        System.out.println("Elevator door closed");
                        RunLift.setRunFlagRight(0);//Declare the elevator as the end of operation
                        RunLift.getDownOrUp().put(targetNum, 0); //Declare that the floor up and down parameter is 0,

                    } else {
                        System.out.println("The right elevator is running, and the current position is:" + RunLift.getFloorNumRight() + "Layer.");
                        RunLift.setSwitchRight(2);
                    }
                    //Someone else was in the elevator and had to go down to their own floor
                    innerUerCheck(num, runLift,1);
                    //People on other floors also click up or down to judge the operation.
                    outUserCheck(num, runLift,1);
                }
            }else{
                num = arr[1];
                for (int i = floorMy; i <= targetNum; i++) {
                    RunLift.setFloorNumRight(i);
                    if (targetNum == i) {
                        System.out.println("The right elevator has arrived:" + targetNum + "Layer.");
                        RunLift.setSwitchRight(1);
                        System.out.println(num + "The first floor has arrived and the elevator door is open.");
                        Thread.sleep(2000);
                        RunLift.setSwitchRight(2);
                        System.out.println("Elevator door closed");
                        RunLift.setRunFlagRight(0);//Declare the elevator as the end of operation
                        RunLift.getDownOrUp().put(num, 0); //Declare that the floor up and down parameter is 0,

                    } else {
                        System.out.println("The right elevator is running, and the current position is:" + RunLift.getFloorNumRight() + "Layer.");
                        RunLift.setSwitchRight(2);
                    }
                    //Someone else was in the elevator and had to go down to their own floor
                    innerUerCheck(num, runLift,1);
                    //People on other floors also click up or down to judge the operation.
                    outUserCheck(num, runLift,1);
                }
            }
        }
    }


    /**
     * Select the running elevator and reach the specified floor
     * @param runLift
     * @return
     */
    public int openAndWait(RunLift runLift) throws InterruptedException {
        //Judge the number of floors where the elevator is located and the location of the user's floor, and judge the left side and right side of elevator startup 1
        int checkfar = checkFar(runLift);
        int floorMy = runLift.getFloorMy();
        int floorNumLeft = RunLift.getFloorNumLeft();
        int floorNumRight = RunLift.getFloorNumRight();
        if (checkfar == 1 ) {
            //The user is higher than the left elevator floor
            if (floorMy > floorNumLeft) {
                //Set the elevator to up
                RunLift.setRunLeftDirection(1);
                //Declare elevator in operation
                RunLift.setRunFlagLeft(1);
                for (int num = floorNumLeft; num <= floorMy; num++) {
                    RunLift.setFloorNumLeft(num);
                    if (num == floorMy) {
                        System.out.println("The left elevator has reached your floor:" + floorMy + "Layer.");
                        RunLift.setSwitchLeft(1);
                        System.out.println(num + "The first floor has arrived and the elevator door is open.");
                        Thread.sleep(2000);
                        RunLift.setSwitchLeft(2);
                        System.out.println("Elevator door closed");
                        RunLift.setRunFlagLeft(0);//Declare the elevator as the end of operation
                        RunLift.getDownOrUp().put(num, 0); //Declare that the floor up and down parameter is 0,

                    } else {
                        System.out.println("The left elevator is running, and the current position is:" + RunLift.getFloorNumLeft() + "Layer.");
                        RunLift.setSwitchLeft(2);
                    }
                    //Someone else was in the elevator and had to go down to their own floor
                    innerUerCheck(num, runLift,checkfar);
                    //People on other floors also click up or down to judge the operation.
                    outUserCheck(num, runLift,checkfar);

                }
            } //The current user floor is smaller than the elevator floor
            else if (floorMy < floorNumLeft) {
                //Set the elevator down
                RunLift.setRunLeftDirection(2);
                //Declare elevator in operation
                RunLift.setRunFlagLeft(1);
                for (int num = floorNumLeft; num >= floorMy; num--) {
                    RunLift.setFloorNumLeft(num);
                    if (num == floorMy) {
                        System.out.println("The left elevator has reached your floor:" + floorMy + "Layer.");
                        RunLift.setSwitchLeft(1);
                        System.out.println(num + "The first floor has arrived and the elevator door is open.");
                        Thread.sleep(2000);
                        RunLift.setSwitchLeft(2);
                        System.out.println("Elevator door closed");
                        RunLift.setRunFlagLeft(0);//Declare the elevator as the end of operation
                        RunLift.getDownOrUp().put(num, 0); //Declare that the floor up and down parameter is 0,

                    } else {
                        System.out.println("The left elevator is running, and the current position is:" + RunLift.getFloorNumLeft() + "Layer.");
                        RunLift.setSwitchLeft(2);
                    }
                    //Someone else was in the elevator and had to go down to their own floor
                    innerUerCheck(num, runLift,checkfar);
                    //People on other floors also click up or down to judge the operation.
                    outUserCheck(num, runLift,checkfar);
                }
            }else{
                System.out.println("The left elevator has reached your floor:" + floorMy + "Layer.");
                RunLift.setSwitchLeft(1);
                System.out.println(floorMy + "The first floor has arrived and the elevator door is open.");
                Thread.sleep(2000);
                RunLift.setSwitchLeft(2);
                System.out.println("Elevator door closed");
                RunLift.setRunFlagLeft(0);//Declare the elevator as the end of operation
                RunLift.getDownOrUp().put(floorMy, 0); //Declare that the floor up and down parameter is 0,
            }
        }else if(checkfar == 2){
            //The user is higher than the right elevator floor
            if (floorMy > floorNumRight) {
                //Set the elevator to up
                RunLift.setRunRightDirection(1);
                //Declare elevator in operation
                RunLift.setRunFlagRight(1);
                for (int num = floorNumRight; num <= floorMy; num++) {
                    RunLift.setFloorNumRight(num);
                    if (num == floorMy) {
                        System.out.println("The right elevator has reached your floor:" + floorMy + "Layer.");
                        RunLift.setSwitchRight(1);
                        System.out.println(num + "The first floor has arrived and the elevator door is open.");
                        Thread.sleep(2000);
                        RunLift.setSwitchRight(2);
                        System.out.println("Elevator door closed");
                        RunLift.setRunFlagRight(0);//Declare the elevator as the end of operation
                        RunLift.getDownOrUp().put(num, 0); //Declare that the floor up and down parameter is 0,

                    } else {
                        System.out.println("The right elevator is running, and the current position is:" + RunLift.getFloorNumRight() + "Layer.");
                        RunLift.setSwitchRight(2);
                    }
                    //Someone else was in the elevator and had to go down to their own floor
                    innerUerCheck(num, runLift,checkfar);
                    //People on other floors also click up or down to judge the operation.
                    outUserCheck(num, runLift,checkfar);

                }
            } //The current user floor is smaller than the elevator floor
            else if (floorMy < floorNumRight) {
                //Set the elevator down
                RunLift.setRunRightDirection(2);
                //Declare elevator in operation
                RunLift.setRunFlagRight(1);
                for (int num = floorNumRight; num >= floorMy; num--) {
                    RunLift.setFloorNumRight(num);
                    if (num == floorMy) {
                        System.out.println("The right elevator has reached your floor:" + floorMy + "Layer.");
                        RunLift.setSwitchRight(1);
                        System.out.println(num + "The first floor has arrived and the elevator door is open.");
                        Thread.sleep(2000);
                        RunLift.setSwitchRight(2);
                        System.out.println("Elevator door closed");
                        RunLift.setRunFlagRight(0);//Declare the elevator as the end of operation
                        RunLift.getDownOrUp().put(num, 0); //Declare that the floor up and down parameter is 0,

                    } else {
                        System.out.println("The right elevator is running, and the current position is:" + RunLift.getFloorNumRight() + "Layer.");
                        RunLift.setSwitchRight(2);
                    }
                    //Someone else was in the elevator and had to go down to their own floor
                    innerUerCheck(num, runLift,checkfar);
                    //People on other floors also click up or down to judge the operation.
                    outUserCheck(num, runLift,checkfar);
                }
            }else{
                System.out.println("The right elevator has reached your floor:" + floorMy + "Layer.");
                RunLift.setSwitchRight(1);
                System.out.println(floorMy + "The first floor has arrived and the elevator door is open.");
                Thread.sleep(2000);
                RunLift.setSwitchRight(2);
                System.out.println("Elevator door closed");
                RunLift.setRunFlagRight(0);//Declare the elevator as the end of operation
                RunLift.getDownOrUp().put(floorMy, 0); //Declare that the floor up and down parameter is 0,
            }
        }

        return checkfar;
    }

    /**
     * Determine which elevator to assign first, 1 left and 2 right
     * @param runLift
     * @return
     */
    public int checkFar(RunLift runLift) {
        int liftNum = 0;
        int floorMy = runLift.getFloorMy();
        int downOrUp = runLift.getDownOrUp().get(runLift.getFloorMy());
        //Both elevators are idle
        if (RunLift.getRunFlagLeft() == 0 && RunLift.getRunFlagRight() == 0){
            liftNum = allFree(floorMy, downOrUp);
        }
        //Left side running, right side idle
        if (RunLift.getRunFlagLeft() == 1 && RunLift.getRunFlagRight() == 0){
            liftNum = LeftRun(floorMy, downOrUp);
        }
        //Right side running, left side idle
        if (RunLift.getRunFlagLeft() == 1 && RunLift.getRunFlagRight() == 0){
            liftNum = RightRun(floorMy, downOrUp);
        }
        return liftNum;
    }

    /**
     * Left idle, right running
     * @param floorMy
     * @param downOrUp
     * @return
     */
    public int RightRun(int floorMy, int downOrUp) {
        //Get floor difference
        int[] floorHigh = floorNum(floorMy);
        //The elevator direction is consistent with the user direction
        if (RunLift.getRunRightDirection() == 1 && downOrUp == 1 ){
            //The right elevator floor is higher than the user floor
            if (RunLift.getFloorNumRight() >floorMy){
                return 1;
            } //The left elevator floor is lower than the user floor
            else{
                //The floor difference on the right is higher than that on the left
                if (floorHigh[0] < floorHigh[1]){
                    return 1;
                }else {
                    return 2;
                }
            }
            //In the same direction
        }else if (RunLift.getRunRightDirection() == 2 && downOrUp == 2 ){
            //The right elevator floor is higher than the user floor
            if (RunLift.getFloorNumLeft() >floorMy){
                return 1;
            } //The left elevator floor is lower than the user floor
            else{
                //The floor difference on the right is higher than that on the left
                if (floorHigh[0] < floorHigh[1]){
                    return 1;
                }else {
                    return 2;
                }
            }
            //The direction is inconsistent. The user is up and down on the right
        }else if(RunLift.getRunRightDirection() == 1 && downOrUp == 2 ){
            return 1;
            //Left lower user upper
        }else if (RunLift.getRunRightDirection() == 2 && downOrUp == 1 ){
            return 1;
        }
        return 1;
    }

    /**
     * Left side running, right side idle
     * @param floorMy
     * @param downOrUp
     * @return
     */
    public int LeftRun(int floorMy, int downOrUp) {
        //Get floor difference
        int[] floorHigh = floorNum(floorMy);
        //The elevator direction is consistent with the user direction
        if (RunLift.getRunLeftDirection() == 1 && downOrUp == 1 ){
            //The left elevator floor is higher than the user floor
            if (RunLift.getFloorNumLeft() >floorMy){
                return 2;
            } //The left elevator floor is lower than the user floor
            else{
                //The floor difference on the left is higher than that on the right
                if (floorHigh[0] > floorHigh[1]){
                    return 2;
                }else {
                    return 1;
                }
            }
            //In the same direction
        }else if (RunLift.getRunLeftDirection() == 2 && downOrUp == 2 ){
            //The left elevator floor is higher than the user floor
            if (RunLift.getFloorNumLeft() >floorMy){
                return 2;
            } //The left elevator floor is lower than the user floor
            else{
                //The floor difference on the left is higher than that on the right
                if (floorHigh[0] > floorHigh[1]){
                    return 2;
                }else {
                    return 1;
                }
            }
            //Inconsistent direction left up and down
        }else if(RunLift.getRunLeftDirection() == 1 && downOrUp == 2 ){
            return 2;
            //Left lower user upper
        }else if (RunLift.getRunLeftDirection() == 2 && downOrUp == 1 ){
            return 2;
        }
        return 2;
    }

    /**
     * Full modification of two sets
     * @param floorMy
     * @param downOrUp
     * @return  2 Right 1 left 
     */
    public int allFree(int floorMy, int downOrUp){
        int[] floorHigh = floorNum(floorMy);
        if (RunLift.getRunFlagRight() == RunLift.getRunFlagLeft()){
            return 2;
        }
        if (floorHigh[1] >floorHigh[0]){
            return 1;
        }else{
            return 2;
        }
    }

    /**
     * Floor difference between two elevators and user floor
     * @param floorMy
     * @return
     */
    public int[] floorNum(int floorMy){
        int floorNumRight = RunLift.getFloorNumRight();
        int floorNumLeft = RunLift.getFloorNumLeft();
        //Number of layers from the right to the target
        int rightNum = Math.abs(floorNumRight - floorMy);
        //left...........
        int leftNum = Math.abs(floorNumLeft - floorMy);
        int[] arr = new int[2];
        arr[0] = leftNum;
        arr[1] = rightNum;
        return arr;
    }

    /**
     * Floor difference between two elevators and target floor
     * @param floorMy
     * @return
     */
    public int[] toTargetNum(int floorMy){
        Integer targetLift = 0;
        Integer targetRight = 0;
        if (RunLift.getFloorTargetLift().size()!=0){
           targetLift = RunLift.getFloorTargetLift().get(0);
        }
        if (RunLift.getFloorTargetRight().size()!=0){
            targetRight = RunLift.getFloorTargetRight().get(0);
        }
        //right
        int rightNum = Math.abs(targetRight - floorMy);
        //left...........
        int leftNum = Math.abs(targetLift - floorMy);
        int[] arr = new int[2];
        arr[0] = leftNum;
        arr[1] = rightNum;
        return arr;
    }

    /**
     * Users outside the elevator press up and down buttons to open and close the door
     * @param num
     */
    public void outUserCheck(int num, RunLift runLift,int checkFar) throws InterruptedException {
        if (checkFar == 1){
            //People on other floors also click up or down.
            Set<Integer> keys = RunLift.getDownOrUp().keySet();
            Iterator<Integer> iterator = keys.iterator();
            while (iterator.hasNext()){
                //The floor is within the range and the status is not 0
                Integer next = iterator.next();
                if (num ==next && RunLift.getDownOrUp().get(next)!=0){
                    //If you, other floors, users in the floor and the destination floor are the same, skip the operation
                    if (runLift.getFloorMy() == num){
                        continue;
                    }
                    //If other floors, users in the floor and destination floors are the same, skip the operation
                    for (int i = 0; i <RunLift.getFloorTargetLift().size(); i++) {
                        if (RunLift.getFloorTargetLift().get(i) == num){
                            continue;
                        }
                    }
                    //Open the door
                    RunLift.setSwitchLeft(1);
                    System.out.println(num +"The first floor has arrived and the elevator door is open.");
                    Thread.sleep(2000);
                    //Close the door and clear the upstairs and downstairs buttons
                    RunLift.setSwitchLeft(2);
                    System.out.println("The elevator door is closed.");
                    RunLift.getDownOrUp().put(iterator.next(),0);
                }
            }
        }else {
            //People on other floors also click up or down.
            Set<Integer> keys = RunLift.getDownOrUp().keySet();
            Iterator<Integer> iterator = keys.iterator();
            while (iterator.hasNext()){
                //The floor is within the range and the status is not 0
                Integer next = iterator.next();
                if (num ==next && RunLift.getDownOrUp().get(next)!=0){
                    //If you, other floors, users in the floor and the destination floor are the same, skip the operation
                    if (runLift.getFloorMy() == num){
                        continue;
                    }
                    //If other floors, users in the floor and destination floors are the same, skip the operation
                    for (int i = 0; i <RunLift.getFloorTargetRight().size(); i++) {
                        if (RunLift.getFloorTargetRight().get(i) == num){
                            continue;
                        }
                    }
                    //Open the door
                    RunLift.setSwitchRight(1);
                    System.out.println(num +"The first floor has arrived and the elevator door is open.");
                    Thread.sleep(2000);
                    //Close the door and clear the upstairs and downstairs buttons
                    RunLift.setSwitchRight(2);
                    System.out.println("The elevator door is closed.");
                    RunLift.getDownOrUp().put(iterator.next(),0);
                }
            }
        }
    }

    /**
     * Operation of users in the elevator to the designated floor
     * @param num
     * @throws InterruptedException
     */
    public void innerUerCheck(int num,RunLift runLift,int checkFar) throws InterruptedException {
        if (checkFar == 1){
            for (int i = 0; i <RunLift.getFloorTargetLift().size(); i++) {
                if (RunLift.getFloorTargetLift().get(i) ==num){
                    //If you, other floors, users in the floor and the destination floor are the same, skip the operation
                    if (runLift.getFloorMy() == num){
                        continue;
                    }
                    //Open the door
                    RunLift.setSwitchLeft(1);
                    System.out.println(num +"The first floor has arrived and the elevator door is open.");
                    Thread.sleep(2000);
                    //Close the door and remove the floor from the list
                    RunLift.setSwitchLeft(2);
                    RunLift.getFloorTargetLift().remove(i);
                    System.out.println("Elevator door closed");
                }
            }
        }else {
            for (int i = 0; i <RunLift.getFloorTargetRight().size(); i++) {
                if (RunLift.getFloorTargetRight().get(i) == num){
                    //If you, other floors, users in the floor and the destination floor are the same, skip the operation
                    if (runLift.getFloorMy() == num){
                        continue;
                    }
                    //Open the door
                    RunLift.setSwitchRight(1);
                    System.out.println(num +"The first floor has arrived and the elevator door is open.");
                    Thread.sleep(2000);
                    //Close the door and remove the floor from the list
                    RunLift.setSwitchRight(2);
                    RunLift.getFloorTargetRight().remove(i);
                    System.out.println("Elevator door closed");
                }
            }
        }
    }


    /**
     * Judge whether to start over
     * @return
     */
    public int checkNew() {
        int num = 0;
        boolean flag = true;
        while (flag){
            try{
                System.out.println("Do you want to continue driving? Please select:1 Yes, 2 no");
                num = scanner.nextInt();
                if (num == 1 || num == 2){
                    flag = false;
                    return num;
                }else {
                    System.out.println("Please enter 1 or 2 ");
                    continue;
                }
            }catch (Exception e){
                System.out.println("Please enter an integer!");
                scanner.next();
            }
        }
        return 1;
    }
}

Running.java

package com.soft.TwoOfLift;

import java.util.ArrayList;
import java.util.List;

public class Running {
    public static void main(String[] args) throws InterruptedException {
        while (true){
       //Data initialization
        RunLiftMethod runLiftMethod = new RunLiftMethod();
        RunLift runLift = new RunLift();
        //Left target layer
        List<Integer> listLeft =new ArrayList<Integer>();
        //Right target layer
        List<Integer> listRight =new ArrayList<Integer>();

        //Select your floor
        runLift.setFloorMy(5);
        //initialization
        RunLift.setFloorTargetLift(listLeft);
        RunLift.setFloorTargetRight(listRight);
        //Choose to go upstairs or downstairs
        runLiftMethod.InputNum(runLift);
        //Wait for the elevator to arrive and return to which side of the elevator to arrive
        int target = runLiftMethod.openAndWait(runLift);
        //Enter the arrival floor
        runLiftMethod.scannerTargetFloor(runLift,target);
        //Arrive at the specified floor, to be optimized: the current 4th floor will go downstairs, and the 6th floor will also go downstairs. It is optimized in the measurement method, and floorTargetLift or floorTargetRight are not judged
        //getDownOrUp field optimization
        runLiftMethod.toTargetFloor(runLift,target);
        //Circular judgment
        if (runLiftMethod.checkNew() == 1){
            continue;
        }else {
            break;
        }
        }
    }

}

Tags: Java Back-end

Posted on Fri, 12 Nov 2021 04:39:35 -0500 by tlavelle