ITPub博客

首页 > 应用开发 > Java > 模拟电梯

模拟电梯

原创 Java 作者:壹頁書 时间:2014-05-08 14:08:34 0 删除 编辑
模拟电梯第一版
没有考虑多线程的问题,只是简单的实现了功能

  1. import java.util.Comparator;
  2. import java.util.PriorityQueue;

  3. /**
  4.  * 模擬电梯
  5.  */
  6. public class T implements Runnable {
  7.     private int floorSize = 10;
  8.     private PriorityQueue<Integer> upCommand = new PriorityQueue<Integer>(floorSize, new Comparator<Integer>() {
  9.         @Override
  10.         public int compare(Integer o1, Integer o2) {
  11.             return o1 - o2;
  12.         }
  13.     });
  14.     private PriorityQueue<Integer> downCommand = new PriorityQueue<Integer>(floorSize, new Comparator<Integer>() {
  15.         @Override
  16.         public int compare(Integer o1, Integer o2) {
  17.             return o2 - o1;
  18.         }
  19.     });

  20.     // 电梯的状态.分别为停止,上升,下降
  21.     private State direction = new Stop(this, upCommand, downCommand);
  22.     // 电梯的当前层
  23.     private int currentFloor;

  24.     public void addCommand(Integer floor) {
  25.         if (floor > getCurrentFloor() && !upCommand.contains(floor)) {
  26.             upCommand.add(floor);
  27.         } else if (floor < getCurrentFloor() && !downCommand.contains(floor)) {
  28.             downCommand.add(floor);
  29.         }
  30.     }

  31.     public T() {
  32.         this.currentFloor = 1;
  33.         new Thread(this).start();
  34.     }

  35.     public void setDirection(State direction) {
  36.         this.direction = direction;
  37.     }

  38.     public State getDirection() {
  39.         return direction;
  40.     }

  41.     public int getCurrentFloor() {
  42.         return currentFloor;
  43.     }

  44.     // 电梯上升
  45.     void up() {
  46.         this.currentFloor = this.currentFloor + 1;
  47.         System.out.println(" " + getCurrentFloor() + "层..");
  48.     }

  49.     // 电梯下降
  50.     void Down() {
  51.         this.currentFloor = this.currentFloor - 1;
  52.         System.out.println(" " + getCurrentFloor() + "层..");

  53.     }

  54.     public static void main(String[] args) throws InterruptedException {
  55.         T t = new T();
  56.         t.addCommand(3);

  57.         t.addCommand(6);
  58.         Thread.sleep(4000);
  59.         t.addCommand(2);
  60.     }

  61.     @Override
  62.     public void run() {
  63.         while (true) {
  64.             this.direction.action();
  65.             try {
  66.                 Thread.sleep(1000);
  67.             } catch (InterruptedException e) {
  68.                 // TODO Auto-generated catch block
  69.                 e.printStackTrace();
  70.             }
  71.         }
  72.     }
  73. }

  74. /**
  75.  * 电梯的状态
  76.  */
  77. interface State {
  78.     void action();

  79.     void display();
  80. }

  81. abstract class AbstractState {
  82.     protected PriorityQueue<Integer> upCommandList;
  83.     protected PriorityQueue<Integer> downCommandList;
  84.     protected T t;

  85.     public AbstractState(T t, PriorityQueue<Integer> upCommandList, PriorityQueue<Integer> downCommandList) {
  86.         this.t = t;
  87.         this.upCommandList = upCommandList;
  88.         this.downCommandList = downCommandList;
  89.     }

  90. }

  91. /**
  92.  * 电梯停止状态
  93.  */
  94. class Stop extends AbstractState implements State {

  95.     public Stop(T t, PriorityQueue<Integer> upCommandList, PriorityQueue<Integer> downCommandList) {
  96.         super(t, upCommandList, downCommandList);
  97.     }

  98.     @Override
  99.     public void action() {
  100.         if (upCommandList.size() > downCommandList.size()) {
  101.             t.setDirection(new Up(t, upCommandList, downCommandList));
  102.         } else if (upCommandList.size() < downCommandList.size()) {
  103.             t.setDirection(new Down(t, upCommandList, downCommandList));
  104.         }
  105.         t.getDirection().display();
  106.     }

  107.     @Override
  108.     public void display() {
  109.     }

  110. }

  111. /**
  112.  * 电梯上升状态
  113.  */
  114. class Up extends AbstractState implements State {

  115.     public Up(T t, PriorityQueue<Integer> upCommandList, PriorityQueue<Integer> downCommandList) {
  116.         super(t, upCommandList, downCommandList);
  117.     }

  118.     @Override
  119.     public void action() {
  120.         if (upCommandList.peek() != null) {
  121.             if (upCommandList.peek() > t.getCurrentFloor()) {
  122.                 t.up();
  123.                 if (t.getCurrentFloor() == upCommandList.peek()) {
  124.                     upCommandList.poll();
  125.                     System.out.println(" " + t.getCurrentFloor() + "层到了");
  126.                 }
  127.             }
  128.         } else if (downCommandList.peek() != null) {
  129.             t.setDirection(new Down(t, upCommandList, downCommandList));
  130.             t.getDirection().display();

  131.         } else {
  132.             t.setDirection(new Stop(t, upCommandList, downCommandList));
  133.         }
  134.     }

  135.     @Override
  136.     public void display() {
  137.         System.out.println("电梯上行");
  138.     }

  139. }

  140. /**
  141.  * 电梯下降状态
  142.  */
  143. class Down extends AbstractState implements State {
  144.     public Down(T t, PriorityQueue<Integer> upCommandList, PriorityQueue<Integer> downCommandList) {
  145.         super(t, upCommandList, downCommandList);
  146.     }

  147.     @Override
  148.     public void action() {
  149.         if (downCommandList.peek() != null) {
  150.             if (downCommandList.peek() < t.getCurrentFloor()) {
  151.                 t.Down();
  152.                 if (t.getCurrentFloor() == downCommandList.peek()) {
  153.                     downCommandList.poll();
  154.                     System.out.println(" " + t.getCurrentFloor() + "层到了");
  155.                 }
  156.             }
  157.         } else if (downCommandList.peek() != null) {
  158.             t.setDirection(new Up(t, upCommandList, downCommandList));
  159.             t.getDirection().display();

  160.         } else {
  161.             t.setDirection(new Stop(t, upCommandList, downCommandList));
  162.         }
  163.     }

  164.     @Override
  165.     public void display() {
  166.         System.out.println("电梯下行");
  167.     }

  168. }
结果如下所示:

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-1156674/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-10-19

  • 博文量
    620
  • 访问量
    6339927