模拟电梯第一版
没有考虑多线程的问题,只是简单的实现了功能
-
import java.util.Comparator;
-
import java.util.PriorityQueue;
-
-
/**
-
* 模擬电梯
-
*/
-
public class T implements Runnable {
-
private int floorSize = 10;
-
private PriorityQueue<Integer> upCommand = new PriorityQueue<Integer>(floorSize, new Comparator<Integer>() {
-
@Override
-
public int compare(Integer o1, Integer o2) {
-
return o1 - o2;
-
}
-
});
-
private PriorityQueue<Integer> downCommand = new PriorityQueue<Integer>(floorSize, new Comparator<Integer>() {
-
@Override
-
public int compare(Integer o1, Integer o2) {
-
return o2 - o1;
-
}
-
});
-
-
// 电梯的状态.分别为停止,上升,下降
-
private State direction = new Stop(this, upCommand, downCommand);
-
// 电梯的当前层
-
private int currentFloor;
-
-
public void addCommand(Integer floor) {
-
if (floor > getCurrentFloor() && !upCommand.contains(floor)) {
-
upCommand.add(floor);
-
} else if (floor < getCurrentFloor() && !downCommand.contains(floor)) {
-
downCommand.add(floor);
-
}
-
}
-
-
public T() {
-
this.currentFloor = 1;
-
new Thread(this).start();
-
}
-
-
public void setDirection(State direction) {
-
this.direction = direction;
-
}
-
-
public State getDirection() {
-
return direction;
-
}
-
-
public int getCurrentFloor() {
-
return currentFloor;
-
}
-
-
// 电梯上升
-
void up() {
-
this.currentFloor = this.currentFloor + 1;
-
System.out.println(" " + getCurrentFloor() + "层..");
-
}
-
-
// 电梯下降
-
void Down() {
-
this.currentFloor = this.currentFloor - 1;
-
System.out.println(" " + getCurrentFloor() + "层..");
-
-
}
-
-
public static void main(String[] args) throws InterruptedException {
-
T t = new T();
-
t.addCommand(3);
-
-
t.addCommand(6);
-
Thread.sleep(4000);
-
t.addCommand(2);
-
}
-
-
@Override
-
public void run() {
-
while (true) {
-
this.direction.action();
-
try {
-
Thread.sleep(1000);
-
} catch (InterruptedException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
}
-
}
-
-
/**
-
* 电梯的状态
-
*/
-
interface State {
-
void action();
-
-
void display();
-
}
-
-
abstract class AbstractState {
-
protected PriorityQueue<Integer> upCommandList;
-
protected PriorityQueue<Integer> downCommandList;
-
protected T t;
-
-
public AbstractState(T t, PriorityQueue<Integer> upCommandList, PriorityQueue<Integer> downCommandList) {
-
this.t = t;
-
this.upCommandList = upCommandList;
-
this.downCommandList = downCommandList;
-
}
-
-
}
-
-
/**
-
* 电梯停止状态
-
*/
-
class Stop extends AbstractState implements State {
-
-
public Stop(T t, PriorityQueue<Integer> upCommandList, PriorityQueue<Integer> downCommandList) {
-
super(t, upCommandList, downCommandList);
-
}
-
-
@Override
-
public void action() {
-
if (upCommandList.size() > downCommandList.size()) {
-
t.setDirection(new Up(t, upCommandList, downCommandList));
-
} else if (upCommandList.size() < downCommandList.size()) {
-
t.setDirection(new Down(t, upCommandList, downCommandList));
-
}
-
t.getDirection().display();
-
}
-
-
@Override
-
public void display() {
-
}
-
-
}
-
-
/**
-
* 电梯上升状态
-
*/
-
class Up extends AbstractState implements State {
-
-
public Up(T t, PriorityQueue<Integer> upCommandList, PriorityQueue<Integer> downCommandList) {
-
super(t, upCommandList, downCommandList);
-
}
-
-
@Override
-
public void action() {
-
if (upCommandList.peek() != null) {
-
if (upCommandList.peek() > t.getCurrentFloor()) {
-
t.up();
-
if (t.getCurrentFloor() == upCommandList.peek()) {
-
upCommandList.poll();
-
System.out.println(" " + t.getCurrentFloor() + "层到了");
-
}
-
}
-
} else if (downCommandList.peek() != null) {
-
t.setDirection(new Down(t, upCommandList, downCommandList));
-
t.getDirection().display();
-
-
} else {
-
t.setDirection(new Stop(t, upCommandList, downCommandList));
-
}
-
}
-
-
@Override
-
public void display() {
-
System.out.println("电梯上行");
-
}
-
-
}
-
-
/**
-
* 电梯下降状态
-
*/
-
class Down extends AbstractState implements State {
-
public Down(T t, PriorityQueue<Integer> upCommandList, PriorityQueue<Integer> downCommandList) {
-
super(t, upCommandList, downCommandList);
-
}
-
-
@Override
-
public void action() {
-
if (downCommandList.peek() != null) {
-
if (downCommandList.peek() < t.getCurrentFloor()) {
-
t.Down();
-
if (t.getCurrentFloor() == downCommandList.peek()) {
-
downCommandList.poll();
-
System.out.println(" " + t.getCurrentFloor() + "层到了");
-
}
-
}
-
} else if (downCommandList.peek() != null) {
-
t.setDirection(new Up(t, upCommandList, downCommandList));
-
t.getDirection().display();
-
-
} else {
-
t.setDirection(new Stop(t, upCommandList, downCommandList));
-
}
-
}
-
-
@Override
-
public void display() {
-
System.out.println("电梯下行");
-
}
-
-
}
结果如下所示:

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