Java电梯模拟
文章目录
- Java电梯模拟
- 前言
- 一、UML类图
- 二、代码
- 三、测试
前言
此程序为单线程简单模拟电梯(初版),如果存在问题或者设计不合理的地方,请大家帮忙指出。
一、UML类图
二、代码
电梯调度器
package cn.xx.evevator;
import java.util.*;
public abstract class AbstarctDispatcher {
protected final Set<Integer> floorList = new LinkedHashSet<>();
private final HashMap<Integer,Floor> floorObjMap = new HashMap<>(11);
protected AbstractEvevator abstractEvevator;
public void setAbstractEvevator(AbstractEvevator abstractEvevator) {
this.abstractEvevator = abstractEvevator;
}
protected abstract void pressFloor(Integer[] floors);
public void newPressPanel(String name,int currentFloor, Status status){
System.out.println(name+"在"+currentFloor+"层按了"+status.NAME+"键");
// 暂时不考虑反方向
if (abstractEvevator.status != StatusCommon.stopStatus && abstractEvevator.status.NAME.equals(status.NAME)) {
abstractEvevator.status.judgeAdd(abstractEvevator.currentFloor,currentFloor,floorList);
}
if (abstractEvevator.status == StatusCommon.stopStatus) {
if (abstractEvevator.currentFloor==currentFloor){
abstractEvevator.openDoor(currentFloor);
}
if (abstractEvevator.currentFloor<currentFloor){
abstractEvevator.up(currentFloor);
}
if (abstractEvevator.currentFloor>currentFloor){
abstractEvevator.down(currentFloor);
}
}
abstractEvevator.run();
handler(currentFloor,status,this);
}
public void addFloor(int floor){
floorList.add(floor);
}
protected abstract void handler(int currentFloor, Status status,AbstarctDispatcher abstarctDispatcher);
public void removeFloorObj(Integer floor){
this.floorObjMap.remove(floor);
}
public abstract void upEve(Integer floor);
public abstract void downEve(Integer floor);
public Floor getFloorObj(Integer floor) {
return floorObjMap.get(floor);
}
public void putFloorObj(Integer currentFloor,Floor floor){
this.floorObjMap.put(currentFloor,floor);
}
}
调度器继承类
package cn.xx.evevator;
import java.util.Arrays;
public class Dispatcher extends AbstarctDispatcher{
@Override
protected void pressFloor(Integer[] floors) {
if (floors!=null && floors.length>0){
this.abstractEvevator.status.handler(this,floors);
}
}
@Override
protected void handler(int currentFloor, Status status, AbstarctDispatcher abstarctDispatcher) {
this.abstractEvevator.status.handler(currentFloor,status,abstarctDispatcher);
}
@Override
public void upEve(Integer floor) {
this.abstractEvevator.up(floor);
}
@Override
public void downEve(Integer floor) {
this.abstractEvevator.down(floor);
}
}
电梯类
package cn.xx.evevator;
public abstract class AbstractEvevator {
protected final AbstarctDispatcher abstarctDispatcher;
protected Status status = StatusCommon.stopStatus;
protected Integer currentFloor = 1;
protected AbstractEvevator(AbstarctDispatcher abstarctDispatcher) {
this.abstarctDispatcher = abstarctDispatcher;
}
abstract void up(int floor);
abstract void down(int floor);
abstract void openDoor(int floor);
abstract void show();
void run(){
show();
}
}
电梯继承类
package cn.xx.evevator;
public class Evevator extends AbstractEvevator{
protected Evevator(AbstarctDispatcher abstarctDispatcher ) {
super(abstarctDispatcher);
}
@Override
void up(int floor) {
if (floor==this.currentFloor){
openDoor(floor);
}else {
this.currentFloor = floor;
System.out.println("电梯正在上升至:" + floor);
openDoor(floor);
}
}
@Override
void down(int floor) {
if (floor==this.currentFloor){
openDoor(floor);
}else {
this.currentFloor = floor;
System.out.println("电梯正在下降至:"+floor);
openDoor(floor);
}
}
@Override
void openDoor(int floor) {
System.out.println(floor+"层开门中,"+status.NAME+"状态");
}
@Override
void show() {
System.out.println("电梯状态:"+this.status.NAME+",楼层:"+this.currentFloor);
}
}
楼层
package cn.xx.evevator;
import java.util.HashMap;
import java.util.LinkedHashSet;
public abstract class AbstractFloor {
protected final int currentFloor;
protected final Status status;
protected final Person person;
protected AbstarctDispatcher abstarctDispatcher;
protected HashMap<Integer, LinkedHashSet<Person>> personList = new HashMap<>();
protected AbstractFloor(int currentFloor, Status status, Person person) {
this.currentFloor = currentFloor;
this.status = status;
this.person = person;
this.abstarctDispatcher = person.abstarctDispatcher;
LinkedHashSet<Person> people = this.personList.get(currentFloor);
if (people!=null && people.size()>0){
people.add(person);
}else{
LinkedHashSet<Person> people1 = new LinkedHashSet<>();
people1.add(person);
personList.put(currentFloor,people1);
}
}
protected abstract void notifyPersonLiftHere();
}
楼层继承类
package cn.xx.evevator;
import java.util.LinkedHashSet;
public class Floor extends AbstractFloor{
protected Floor(int currentFloor, Status status, Person person) {
super(currentFloor, status, person);
}
@Override
protected void notifyPersonLiftHere() {
LinkedHashSet<Person> people = this.personList.get(currentFloor);
if (people!=null && people.size()>0){
for (Person person1 : people) {
if (person1.floor.status.NAME.equals(person1.abstarctDispatcher.abstractEvevator.status.NAME)) {
person1.elevatorIsHere(currentFloor);
}
}
}
}
}
状态类
package cn.xx.evevator;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;
public abstract class Status {
public final String NAME;
protected Status(String name) {
NAME = name;
}
abstract void judgeAdd(int eveFloor, int personFloor, Set<Integer> floorList);
/**
* 这个主要用于用户在楼层中按电梯
* @param currentFloor
* @param status
* @param abstarctDispatcher
*/
abstract void handler(int currentFloor, Status status,AbstarctDispatcher abstarctDispatcher);
/**
* 这个主要用于在电梯中按楼层
* @param abstarctDispatcher
*/
abstract void handler(AbstarctDispatcher abstarctDispatcher,Integer[] floors);
void sortFloor(Status status,Integer[] floors, Set<Integer> floorList){
Arrays.sort(floors);
if (status.NAME.equals(StatusCommon.downStatus.NAME)){
Arrays.sort(floors,Collections.reverseOrder());
}
for (int floor : floors) {
if (floor>11 || floor<0){
System.out.println("楼层输入错误");
continue;
}
if (!floorList.contains(floor))
floorList.add(floor);
}
}
}
上升状态
package cn.xx.evevator;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
public class UpStatus extends Status{
public UpStatus(String name) {
super(name);
}
@Override
public void judgeAdd(int eveFloor, int personFloor, Set<Integer> floorList) {
if (eveFloor<personFloor){
floorList.add(personFloor);
}
}
@Override
public void handler(int currentFloor, Status status,AbstarctDispatcher abstarctDispatcher) {
abstarctDispatcher.abstractEvevator.status = this;
if (abstarctDispatcher.abstractEvevator.currentFloor>currentFloor){
return;
}
for (Integer floor : abstarctDispatcher.floorList) {
if (abstarctDispatcher.abstractEvevator.currentFloor==floor){
// abstarctDispatcher.abstractEvevator.openDoor(floor);
}else {
abstarctDispatcher.upEve(floor);
}
abstarctDispatcher.floorList.remove(floor);
Floor floorObj = abstarctDispatcher.getFloorObj(floor);
floorObj.notifyPersonLiftHere();
abstarctDispatcher.removeFloorObj(floor);
}
}
@Override
void handler(AbstarctDispatcher abstarctDispatcher,Integer[] floors) {
sortFloor(StatusCommon.upstatus,floors,abstarctDispatcher.floorList);
for (int floor : floors) {
if (floor>11 || floor<0){
System.out.println("楼层输入错误");
continue;
}
if (!abstarctDispatcher.floorList.contains(floor))
abstarctDispatcher.floorList.add(floor);
}
Iterator<Integer> iterator = abstarctDispatcher.floorList.iterator();
while (iterator.hasNext()) {
Integer next = iterator.next();
abstarctDispatcher.abstractEvevator.currentFloor=next;
abstarctDispatcher.upEve(next);
iterator.remove();
}
abstarctDispatcher.abstractEvevator.status = StatusCommon.stopStatus;
}
}
下降状态
package cn.xx.evevator;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;
public class DownStatus extends Status{
public DownStatus(String name) {
super(name);
}
@Override
public void judgeAdd(int eveFloor, int personFloor, Set<Integer> floorList) {
floorList.add(personFloor);
}
@Override
public void handler(int currentFloor, Status status,AbstarctDispatcher abstarctDispatcher) {
abstarctDispatcher.abstractEvevator.status = this;
if (abstarctDispatcher.abstractEvevator.currentFloor<currentFloor){
return;
}
for (Integer floor : abstarctDispatcher.floorList) {
if (abstarctDispatcher.abstractEvevator.currentFloor==floor){
abstarctDispatcher.abstractEvevator.openDoor(floor);
}else {
abstarctDispatcher.downEve(floor);
}
abstarctDispatcher.floorList.remove(floor);
Floor floorObj = abstarctDispatcher.getFloorObj(floor);
floorObj.notifyPersonLiftHere();
abstarctDispatcher.removeFloorObj(floor);
}
}
@Override
void handler(AbstarctDispatcher abstarctDispatcher,Integer[] floors) {
sortFloor(StatusCommon.downStatus,floors,abstarctDispatcher.floorList);
Iterator<Integer> iterator = abstarctDispatcher.floorList.iterator();
while (iterator.hasNext()) {
Integer next = iterator.next();
abstarctDispatcher.abstractEvevator.currentFloor=next;
abstarctDispatcher.downEve(next);
iterator.remove();
}
abstarctDispatcher.abstractEvevator.status = StatusCommon.stopStatus;
}
}
停止状态
package cn.xx.evevator;
import java.util.Set;
public class StopStatus extends Status{
public StopStatus(String name) {
super(name);
}
@Override
public void judgeAdd(int eveFloor, int personFloor, Set<Integer> floorList) {
if (eveFloor>personFloor){
floorList.add(personFloor);
}
}
@Override
public void handler(int currentFloor, Status status,AbstarctDispatcher abstarctDispatcher) {
status.handler(currentFloor,status,abstarctDispatcher);
}
@Override
void handler(AbstarctDispatcher abstarctDispatcher,Integer[] floors) {
}
}
人员类
package cn.xx.evevator;
import org.springframework.util.StringUtils;
public class Person {
protected Floor floor;
protected final AbstarctDispatcher abstarctDispatcher;
protected final Integer[] expectFloor;
protected final String name;
private Person(AbstarctDispatcher abstarctDispatcher, Integer[] expectFloor, String name) {
this.abstarctDispatcher = abstarctDispatcher;
this.expectFloor = expectFloor;
this.name = name;
}
public void setFloor(Floor floor){
this.floor = floor;
abstarctDispatcher.putFloorObj(floor.currentFloor,floor);
abstarctDispatcher.addFloor(floor.currentFloor);
abstarctDispatcher.newPressPanel(name,floor.currentFloor,floor.status);
}
public void press(Integer[] floor){
abstarctDispatcher.pressFloor(floor);
}
public void elevatorIsHere(int floor){
System.out.println(floor+"层到了,"+this.name+"请进电梯!");
press(expectFloor);
}
public static class Builder{
private AbstarctDispatcher abstarctDispatcher;
private Integer[] expectFloor;
private String name;
public Builder(AbstarctDispatcher abstarctDispatcher){
this.abstarctDispatcher = abstarctDispatcher;
}
public Builder expectFloor(Integer[] expectFloor){
this.expectFloor = expectFloor;
return this;
}
public Builder name(String name){
if (StringUtils.isEmpty(name)){
this.name = "";
}else{
this.name = name;
}
return this;
}
public Person build(){
return new Person(abstarctDispatcher,expectFloor,name);
}
}
}
状态common
package cn.xx.evevator;
public class StatusCommon {
public static final UpStatus upstatus = new UpStatus("上升");
public static final DownStatus downStatus = new DownStatus("下降");
public static final StopStatus stopStatus = new StopStatus("停止");
}
三、测试
public class Test {
public static void main(String[] args) {
Dispatcher dispatcher = new Dispatcher();
Evevator evevator = new Evevator(dispatcher);
dispatcher.setAbstractEvevator(evevator);
// 张三在2层按了上行行键 进去电梯后按6楼和8楼
Person person = new Person
.Builder(dispatcher)
.name("张三")
.expectFloor(new Integer[]{3, 1})
.build();
person.setFloor(new Floor(11, StatusCommon.downStatus,person));
// 李四在8层按了上行键 进去电梯后按10楼和11楼
Person person1 = new Person
.Builder(dispatcher)
.name("李四")
.expectFloor(new Integer[]{10,11})
.build();
person1.setFloor(new Floor(8,StatusCommon.upstatus,person1));
// 王五在3层按了上行键 进去电梯后按9楼和11楼
Person person3 = new Person
.Builder(dispatcher)
.name("王五")
.expectFloor(new Integer[]{9,11})
.build();
person3.setFloor(new Floor(3,StatusCommon.upstatus,person3));
// 王五在6层按了下行键 进去电梯后按5楼和1楼
Person person2 = new Person
.Builder(dispatcher)
.name("刘九")
.expectFloor(new Integer[]{1,5})
.build();
person2.setFloor(new Floor(6,StatusCommon.downStatus,person2));
}
}
运行结果:
张三在11层按了下降键
电梯正在上升至:11
11层开门中,停止状态
电梯状态:停止,楼层:11
11层开门中,下降状态
11层到了,张三请进电梯!
3层开门中,下降状态
1层开门中,下降状态
李四在8层按了上升键
电梯正在上升至:8
8层开门中,停止状态
电梯状态:停止,楼层:8
8层到了,李四请进电梯!
10层开门中,上升状态
11层开门中,上升状态
王五在3层按了上升键
电梯正在下降至:3
3层开门中,停止状态
电梯状态:停止,楼层:3
3层到了,王五请进电梯!
9层开门中,上升状态
11层开门中,上升状态
刘九在6层按了下降键
电梯正在下降至:6
6层开门中,停止状态
电梯状态:停止,楼层:6
6层开门中,下降状态
6层到了,刘九请进电梯!
5层开门中,下降状态
1层开门中,下降状态