Java电梯模拟

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{@Overrideprotected void pressFloor(Integer[] floors) {if (floors!=null && floors.length>0){this.abstractEvevator.status.handler(this,floors);}}@Overrideprotected void handler(int currentFloor, Status status, AbstarctDispatcher abstarctDispatcher) {this.abstractEvevator.status.handler(currentFloor,status,abstarctDispatcher);}@Overridepublic void upEve(Integer floor) {this.abstractEvevator.up(floor);}@Overridepublic 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);}@Overridevoid up(int floor) {if (floor==this.currentFloor){openDoor(floor);}else {this.currentFloor = floor;System.out.println("电梯正在上升至:" + floor);openDoor(floor);}}@Overridevoid down(int floor) {if (floor==this.currentFloor){openDoor(floor);}else {this.currentFloor = floor;System.out.println("电梯正在下降至:"+floor);openDoor(floor);}}@Overridevoid openDoor(int floor) {System.out.println(floor+"层开门中,"+status.NAME+"状态");}@Overridevoid 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);}@Overrideprotected 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);}@Overridepublic void judgeAdd(int eveFloor, int personFloor, Set<Integer> floorList) {if (eveFloor<personFloor){floorList.add(personFloor);}}@Overridepublic 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);}}@Overridevoid 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);}@Overridepublic void judgeAdd(int eveFloor, int personFloor, Set<Integer> floorList) {floorList.add(personFloor);}@Overridepublic 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);}}@Overridevoid 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);}@Overridepublic void judgeAdd(int eveFloor, int personFloor, Set<Integer> floorList) {if (eveFloor>personFloor){floorList.add(personFloor);}}@Overridepublic void handler(int currentFloor, Status status,AbstarctDispatcher abstarctDispatcher) {status.handler(currentFloor,status,abstarctDispatcher);}@Overridevoid 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层开门中,下降状态

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/717419.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Web3游戏基础设施提供商Stardust为Sui上的游戏开发者提供支持

Stardust将其在钱包服务&#xff08;wallets-as-a-service&#xff09;基础设施和用户获取平台方面的专业知识带到了Sui&#xff0c;为游戏开发者提供了关键的帮助&#xff0c;以吸引玩家。近日&#xff0c;Stardust公司宣布将为Sui游戏开发者调整其成熟的钱包服务&#xff08;…

MySQL:开始深入其数据(四)select子查询

select眼熟吧?(都三节了) 又开始学习了 在 MySQL 中&#xff0c;子查询&#xff08;subquery&#xff09;是指在一个查询内嵌套另一个完整的 SELECT 语句。子查询可以嵌套在 SELECT、INSERT、UPDATE、DELETE 语句中&#xff0c;用于从内部查询结果中获取数据&#xff0c;进而完…

基于springboot的宠物咖啡馆平台的设计与实现论文

基于Spring Boot的宠物咖啡馆平台的设计与实现 摘要 随着信息技术在管理上越来越深入而广泛的应用&#xff0c;管理信息系统的实施在技术上已逐步成熟。本文介绍了基于Spring Boot的宠物咖啡馆平台的设计与实现的开发全过程。通过分析基于Spring Boot的宠物咖啡馆平台的设计与…

每日一题——LeetCode1566.重复至少K次且长度为M的模式

方法一 暴力枚举 var containsPattern function(arr, m, k) {const n arr.length;for (let l 0; l < n - m * k; l) {let offset;for (offset 0; offset < m * k; offset) {if (arr[l offset] ! arr[l offset % m]) {break;}}if (offset m * k) {return true;}}r…

k8s 网络概念与策略控制

一、Kubernetes 基本网络模型 Kubernetes 的容器网络模型可以把它归结为约法三章和四大目标。 1、约法三章 约法三章确保了Kubernetes容器网络模型的基本特性&#xff1a; ① 任意两个 pod 之间可以直接通信&#xff1a;在Kubernetes中&#xff0c;每个 Pod 都被分配了一个…

React-router的创建和第一个组件

需要先学react框架 首先&#xff1a;找到一个文件夹&#xff0c;在文件夹出打开cmd窗口&#xff0c;输入如下图的口令 npx create-react-app demo 然后等待安装 安装完成 接下来进入创建的demo实例 cd demo 然后可以用如下方式打开vscode code . 注意&#xff1a;不要忽略点号与…

Vue--》打造简易直播应用平台项目实战

今天开始使用 vue3 + ts 搭建一个简易直播应用平台项目,因为文章会将项目的每一个地方代码的书写都会讲解到,所以本项目会分成好几篇文章进行讲解,我会在最后一篇文章中会将项目代码开源到我的github上,大家可以自行去进行下载运行,希望本文章对有帮助的朋友们能多多关注本…

支持向量机 SVM | 线性可分:公式推导

目录 一. SVM的优越性二. SVM算法推导小节概念 在开始讲述SVM算法之前&#xff0c;我们先来看一段定义&#xff1a; 支持向量机(Support VecorMachine, SVM)本身是一个二元分类算法&#xff0c;支持线性分类和非线性分类的分类应用&#xff0c;同时通过OvR或者OvO的方式可以应用…

安装Docker及DockerCompose

0.安装Docker Docker 分为 CE 和 EE 两大版本。CE 即社区版&#xff08;免费&#xff0c;支持周期 7 个月&#xff09;&#xff0c;EE 即企业版&#xff0c;强调安全&#xff0c;付费使用&#xff0c;支持周期 24 个月。 Docker CE 分为 stable test 和 nightly 三个更新频道…

10.轮廓系数-机器学习模型性能的常用的评估指标

轮廓系数&#xff08;Silhouette Coefficient&#xff09;是评估聚类算法效果的常用指标之一。它结合了聚类的凝聚度&#xff08;Cohesion&#xff09;和分离度&#xff08;Separation&#xff09;&#xff0c;能够量化聚类结果的紧密度和分离度。 背景 1.聚类分析的背景 在…

CUDA学习笔记01:vs2019环境配置

为了在window11 vs2019下使用CUDA编程&#xff0c;配置了一下环境&#xff0c;但是我电脑一开始自带CUDA&#xff0c;然后再安装的vs2019&#xff0c;这样安装顺序上是不对的&#xff0c;vs2019找不到CUDA配置项&#xff0c;网上找了很多办法貌似都不好使而且很复杂。 那么最快…

c++之拷贝构造和赋值

如果一个构造函数中的第一个参数是类本身的引用&#xff0c;或者是其他的参数都有默认值&#xff0c;则该构造函数为拷贝构造函数。 那么什么是拷贝构造呢&#xff1f;利用同类对象构造一个新对象。 1&#xff0c;函数名和类必须同名。 2&#xff0c;没有返回值。 3&#x…

Linux多线程控制:深入理解与应用(万字详解!)

&#x1f3ac;慕斯主页&#xff1a;修仙—别有洞天 ♈️今日夜电波&#xff1a;どうして (feat. 野田愛実) 0:44━━━━━━️&#x1f49f;──────── 3:01 &#x1f504; ◀️ ⏸ ▶️ …

6.2 指标的应用与设计(12%)

1、指标的作用 用简约的汇总数据量化业务强弱。 2、指标的理解 特点&#xff1a; &#xff08;1&#xff09;指标是游离态的&#xff0c;无法单独实现数据统计 eg&#xff1a;总销售额、总销售量 &#xff08;2&#xff09;需与统计维度结合&#xff0c;明确统计指标的对…

047 内部类

成员内部类用法 /*** 成员内部类** author Admin*/ public class OuterClass {public void say(){System.out.println("这是类的方法");}class InnerClass{public void say(){System.out.println("这是成员内部类的方法");}}public static void main(Stri…

(二)逻辑回归与交叉熵--九五小庞

什么是逻辑回归 线性回归预测的是一个连续值&#xff0c;逻辑回归给出的“是”和“否”的回答 Singmoid sigmoid函数是一个概率分布函数&#xff0c;给定某个输入&#xff0c;它将输出为一个概率值 逻辑回归损失函数 平方差所惩罚的是与损失为同一数量级的情形&#xff0…

SandBox中的JavaAgent技术

8.1 JavaAgent Java Agent 是一种强大的技术&#xff0c;在运行时动态修改已加载类的字节码&#xff0c;为应用程序注入额外的功能和行为。 JDK 1.5 支持静态 Instrumentation&#xff0c;基本的思路是在 JVM 启动的时候添加一个代理&#xff08;javaagent&#xff09;&#…

基于阿里云OSS上传图片实战案例

一、案例描述 基于Springboot框架实现一个上传图片到阿里云服务端保存的小案例。 二、准备工作 基于Springboot免费搭载轻量级阿里云OSS数据存储库&#xff08;将本地文本、照片、视频、音频等上传云服务保存&#xff09;-CSDN博客 三、代码 新建这两个类&#xff1a;一个…

LANA: A Language-Capable Navigator for Instruction Following and Generation

摘要 最近&#xff0c;视觉语言导航&#xff08;VLN&#xff09;——要求机器人代理遵循导航指令——已经取得了巨大的进步。然而&#xff0c;现有文献最强调将指令解释为行动&#xff0c;只提供“愚蠢”的寻路代理。在本文中&#xff0c;我们设计了 LANA&#xff0c;一种支持…

【LeetCode-1143】最长公共子序列(动归)

目录 题目描述 解法1&#xff1a;动态规划 代码实现 题目链接 题目描述 给定两个字符串 text1 和 text2&#xff0c;返回这两个字符串的最长公共子序列的长度。 一个字符串的 子序列 是指这样一个新的字符串&#xff1a;它是由原字符串在不改变字符的相对顺序的情况下删除…