OOD - Design Elevator System
完整代码
Direction.java
package com.design.elevator;
public enum Direction {
UP, DOWN
}
Status.java
package com.design.elevator;
public enum Status {
UP, DOWN, IDLE
}
Request.java
package com.design.elevator;
public class Request {
private int level;
public Request(int l) {
level = l;
}
public int getLevel() {
return level;
}
}
ExternalRequest.java
package com.design.elevator;
public class ExternalRequest extends Request{
private Direction direction;
public ExternalRequest(int l, Direction d) {
super(l);
// TODO Auto-generated constructor stub
this.direction = d;
}
public Direction getDirection() {
return direction;
}
}
InternalRequest.java
package com.design.elevator;
public class InternalRequest extends Request{
public InternalRequest(int l) {
super(l);
// TODO Auto-generated constructor stub
}
}
ElevatorButton.java
package com.design.elevator;
public class ElevatorButton {
private int level;
private Elevator elevator;
public ElevatorButton(int l, Elevator e) {
this.level = l;
this.elevator = e;
}
public void pressButton() {
InternalRequest request = new InternalRequest(level);
elevator.handleInternalRequest(request);
}
}
Elevator.java
package com.design.elevator;
import java.util.ArrayList;
import java.util.List;
public class Elevator {
private List<ElevatorButton> buttons;
private List<Boolean> upStops;
private List<Boolean> downStops;
private int currLevel;
private Status status;
public Elevator(int n) {
buttons = new ArrayList<ElevatorButton>();
upStops = new ArrayList<Boolean>();
downStops = new ArrayList<Boolean>();
currLevel = 0;
status = Status.IDLE;
for (int i = 0; i < n; i++) {
upStops.add(false);
downStops.add(false);
}
}
public void insertButton(ElevatorButton eb) {
buttons.add(eb);
}
public void handleExternalRequest(ExternalRequest r) {
if (r.getDirection() == Direction.UP) {
upStops.set(r.getLevel() - 1, true);
if (noRequests(downStops)) {
status = Status.UP;
}
} else {
downStops.set(r.getLevel() - 1, true);
if (noRequests(upStops)) {
status = Status.DOWN;
}
}
}
public void handleInternalRequest(InternalRequest r) {
// check valid
if (status == Status.UP) {
if (r.getLevel() >= currLevel + 1) {
upStops.set(r.getLevel() - 1, true);
}
} else if (status == Status.DOWN) {
if (r.getLevel() <= currLevel + 1) {
downStops.set(r.getLevel() - 1, true);
}
}
}
public void openGate() throws Exception {
if (status == Status.UP) {
for (int i = 0; i < upStops.size(); i++) {
int checkLevel = (currLevel + i) % upStops.size();
if (upStops.get(checkLevel)) {
currLevel = checkLevel;
upStops.set(checkLevel, false);
break;
}
}
} else if (status == Status.DOWN) {
for (int i = 0; i < downStops.size(); i++) {
int checkLevel = (currLevel + downStops.size() - i) % downStops.size();
if (downStops.get(checkLevel)) {
currLevel = checkLevel;
downStops.set(checkLevel, false);
break;
}
}
}
}
public void closeGate() {
if (status == Status.IDLE) {
if (noRequests(downStops)) {
status = Status.UP;
return;
}
if (noRequests(upStops)) {
status = Status.DOWN;
return;
}
} else if (status == Status.UP) {
if (noRequests(upStops)) {
if (noRequests(downStops)) {
status = Status.IDLE;
} else {
status = Status.DOWN;
}
}
} else {
if (noRequests(downStops)) {
if (noRequests(upStops)) {
status = Status.IDLE;
} else {
status = Status.UP;
}
}
}
}
private boolean noRequests(List<Boolean> stops) {
for (int i = 0; i < stops.size(); i++) {
if (stops.get(i)) {
return false;
}
}
return true;
}
public String elevatorStatusDescription() {
String description = "Currently elevator status is : " + status
+ ".\nCurrent level is at: " + (currLevel + 1)
+ ".\nup stop list looks like: " + upStops
+ ".\ndown stop list looks like: " + downStops
+ ".\n*****************************************\n";
return description;
}
}
模拟流程
Simulation.java
package com.design.elevator;
public class Simulation {
public static void main(String[] args) throws Exception {
// 初始化电梯对象
Elevator elevator = new Elevator(5);
// 实例化 request对象
ExternalRequest e1 = new ExternalRequest(3, Direction.DOWN);
elevator.handleExternalRequest(e1);
System.out.println(elevator.elevatorStatusDescription());
ExternalRequest e2 = new ExternalRequest(2, Direction.UP);
elevator.handleExternalRequest(e2);
System.out.println(elevator.elevatorStatusDescription());
elevator.openGate();
elevator.closeGate();
elevator.openGate();
elevator.closeGate();
System.out.println(elevator.elevatorStatusDescription());
}
}