The elevator system based on Java piggyback

First, the idea

Write a simple elevator system, first offered in accordance with the needs of teachers, write about basic ideas:

  1. Elevator has the highest level and the lowest level, select the correct digital input Number of floors
  2. Input number is greater than the current floor, compared with the uplink; less than the current floor, for the downlink
  3. Each entering a number, the need for the same number of uplink or downlink of the same number, sort
  4. The destination floor inputted by the set of storage, the lowest level to the top of the cycle, if a current layer is present in the set, a display door, if there is a destination floor, is closed, continue to the next destination floor.
  5. When selecting a destination floor, will generate a random weight is recorded at the target floor, the uplink with the original target weight plus the weight of the floor, the downlink destination floor by subtracting the weight of the original weight

Second, the realization

2.1 Elevator class

  1 package Ele;
  2 
  3 import java.util.ArrayList;
  4 import java.util.Collections;
  5 import java.util.Iterator;
  6 import java.util.List;
  7 import java.util.Random;
  8 
  9 public class Elevator {
 10     private List<Integer> upFloorList = new ArrayList<Integer>();       // 上升楼层
 11     private List<Integer> downFloorList = new ArrayList<Integer>();     // 下降楼层
12 is      Private  int [] storeyWeight; // target weight of the layer 
13 is      Private  int Capacity;        // elevators maximum weight 
14      Private  int topFloor;        // lift the top 
15      Private  int bottomFloor;     // lift the bottom 
16      Private  int nowFloor =. 1;    / / current layer 
. 17  
18 is      public Elevator ( int bottomFloor, int topFloor, int Capacity) { // there constructor parameter 
. 19          the this= .topFloor topFloor;
 20 is          the this .bottomFloor = bottomFloor;
 21 is          the this .capacity = Capacity;
 22 is  
23 is          // current floor minus the lowest layer is the current layer index if the weight of the current floor is the fifth floor, standard 5 is downstairs 5- 1 = 4
 24          // initialize the target floor by weight, the top array size = - 1 the lowest level of + 
25          storeyWeight = new new  int [(topFloor - bottomFloor + 1 )];
 26 is      }
 27  
28      // installed floor 
29      public  void SetFloor ( int floorNum) {
 30          // if selected the same floor where the floor, the tips 
31 is          IF(floorNum == nowFloor) {
 32              System.out.println ( "Please select a floor" );
 33 is              return ;
 34 is          }
 35  
36          // generates a random weight between 90-500 
37 [          the Random Random = new new the Random ();
 38 is          int thisFloorWeight random.nextInt = (500 - 90 +. 1) + 90 ;
 39  
40          int SUM = 0 ;
 41 is          // destination floor increased weight 
42 is          for ( int I = 0; I <storeyWeight.length; I ++ ) {
 43 is              SUM = + storeyWeight [I];
44          }
 45          // original weight + weight increase = Current weight 
46 is          System.out.println (+ floorNum "up layer weight:" + thisFloorWeight + ", the total weight at this time:" + (SUM + thisFloorWeight));
 47  
48          / / If the destination floor on the total weight> maximum weight, suggesting 
49          IF (SUM + thisFloorWeight> the this .capacity) {
 50              System.out.println ( "overweight yo" );
 51 is              return ;
 52 is          }
 53 is  
54 is          // current input floor by weight plus the weight of the new increase in the weight of the floor 
55          storeyWeight [floorNum - bottomFloor] + = thisFloorWeight;
 56 is  
57 is          //If the input number of floors up or down has been set in the floor, only to add weight without adding a floor 
58          IF (! UpFloorList.contains (floorNum) &&! DownFloorList.contains (floorNum)) {
 59              IF (floorNum> nowFloor) {
 60                  upFloorList.add (floorNum);
 61 is  
62 is                  // rise in ascending order of stories 
63 is                  the Collections.sort (upFloorList);
 64  
65              } the else {
 66                  downFloorList.add (floorNum);
 67  
68                  // decrease in descending order floor 
69                  downFloorList.sort (Collections.reverseOrder ());
 70              }
71          }
 72      }
 73 is  
74      @ rise: layer to the selected location from the top of the floor
 75      @ lowered: from the layer where the lowest level to the selected floor
 76      // get the last element in the set: List.get (list.size () -. 1);
 77  
78      // activate the elevator 
79      public  void StartElevator () throws InterruptedException {
 80          System.out.println ( "current <" + nowFloor + "> layer" );
 81          // uplink 
82          IF (upFloorList.size ()> 0 ) {
 83              System.out.println ( "elevator up --- ---" );
 84              for (int i = nowFloor + 1; i <= upFloorList.get(upFloorList.size() - 1); i++) {
 85                 Thread.sleep(500);
 86                 System.out.println("---第" + i + "层---");
 87                 if (upFloorList.contains(i)) {
 88                     System.out.println("  ☆开门☆");
 89                     nowFloor = i;
 90                     upFloorList.remove(upFloorList.indexOf(i));
 91                     storeyWeight[i - bottomFloor] = 0;
 92 
 93                     if (upFloorList.size() > 0) {
 94                         System.out.println("剩余所选层数为:");
 95                         Iterator it = upFloorList.iterator();
 96                         while (it.hasNext()) {
 97                             int floor = (int) it.next();
 98                             System.out.print(floor + "层 重量:" + storeyWeight[floor - bottomFloor] + "  ");
 99                         }
100                         System.out.println();
101                     }
102                     return;
103                 }
104             }
105         }
106 
107         // 下行
108         if (downFloorList.size() > 0) {
109             System.out.println("---电梯下行---");
110             for (int i = nowFloor - 1; i >= bottomFloor; i--) {
111                 Thread.sleep(500);
112                 System.out.println("---第" + i + "层---");
113                 if (downFloorList.contains(i)) {
114                     System.out.println("  ☆开门☆");
115                     nowFloor = i;
116                     downFloorList.remove(downFloorList.indexOf(i));
117                     storeyWeight[i - bottomFloor] = 0;
118                     if (downFloorList.size() > 0) {
119                         System.out.println("剩余所选层数为:");
120                         Iterator it = downFloorList.iterator();
121                         while (it.hasNext()) {
122                             int floor = (int) it.next();
123                             System.out.print(floor + "层 重量:" + storeyWeight[floor - bottomFloor] + " ");
124                         }
125                         System.out.println();
126                     }
127                     return;
128                 }
129             }
130         }
131         System.out.println("无客");
132 
133     }
134 }

 

2.2 program entry

. 1  Package com.company;
 2  
. 3  
. 4  Import Ele.Elevator;
 . 5  
. 6  Import java.util.Scanner;
 . 7  
. 8  public  class the Main {
 . 9  
10      public  static  void main (String [] args) throws InterruptedException {
 . 11          // create a lift 
12          int bottomFloor = 1;     // lowest first floor 
13          int topFloor = 12;       // the top 12th floor, 
14          int Capacity = 1000;     // maximum load 1000
15          Elevator elvator = new new Elevator (bottomFloor, topFloor, Capacity);
 16  
. 17          System.out.println ( "Current lift select" + bottomFloor + "-" + + " layer topFloor, select the number of floors (input means closed -1 elevator door): " );
 18 is  
. 19          // typing 
20 is          Scanner Scanner = new new Scanner (the System.in);
 21 is          the while (scanner.hasNextLine ()) {
 22 is              // If the digital input is not prompted, again input 
23 is              IF (! scanner.hasNextInt ()) {
 24                  System.out.println ( "Please enter the number!" );
 25                  Scanner.next ();
 26 is              }
27  
28              // input is digital, the following actions 
29              the else {
 30                  int NUM = scanner.nextInt ();
 31 is                  // if the input number is -1, which means the end of input, activate the elevator 
32                  IF (NUM == -1 ) {
 33 is                      System.out.println ( "------------------------" );
 34 is                      System.out.println ( "elevator door closed, started " );
 35                      elvator.StartElevator ();
 36                  } the else  IF (NUM> NUM topFloor || <|| bottomFloor NUM == 0 ) {
 37 [                      // If the input number does not conform to the number of floors, and is prompted to again enter the 
38                     System.out.println ( "Please choose 1-12 floor." );
 39                  } the else {
 40                      elvator.SetFloor (NUM);
 41 is                  }
 42 is              }
 43 is          }
 44 is      }
 45 }

 

 

Third, the summary

  This simple procedure elevator, the basic realization of the elevator upward and downward determination, when a plurality of floors, may be sequentially arrive at the destination floor is automatically sorted with the uplink or downlink, each of the destination floor the passenger weight is randomly generated and recorded.

  In writing this program, we met some problems:

1. Use the while statement to receive user input to determine whether the digital input, digital input will not prompt an endless loop. The increased scanner.next () statement, you can continue to enter when prompted.

  if (!scanner.hasNextInt()) {
                System.out.println ( "Please enter the number!" );
                Scanner.next (); 
            }

 

After repeated selection 2. If a floor, the arrival floor, the floor will still show the remaining floors. The increase judgment sentence, if the selected number of floors up or down is already present in the target set of floors, only increase the weight, the destination floor is not repeated addition.

 if (!upFloorList.contains(floorNum) && !downFloorList.contains(floorNum)) {
            if (floorNum > nowFloor) {
                upFloorList.add(floorNum);

                // increase in ascending order of stories 
                Collections.sort (upFloorList);

            } else {
                downFloorList.add(floorNum);

                // drop floor in descending order 
                downFloorList.sort (Collections.reverseOrder ());
            }
        }
    }

 

3. The weight of the destination floor stored in a random array, the current floor minus the lowest layer is the current layer index by weight, if the current floor to floor 5, 5 is the standard downstairs 5-1 = 4, storeyWeight [4 ] is the floor 5 by weight.

 

  Code also imperfections, such as if rising from 1-8 layers, 6 layers someone should take the elevator to stop and how to meet the Master in the 6th floor, continue upward. These have yet to be I continue to improve, but also hope to please criticism

Guess you like

Origin www.cnblogs.com/lzh7513/p/11728866.html