C#控制台模拟电梯工作原理 C#控制台模拟电梯工作原理
人气:0想了解C#控制台模拟电梯工作原理的相关内容吗,在本文为您仔细讲解C#控制台模拟电梯工作原理的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#,控制台,模拟电梯,下面大家一起来学习吧。
每天上下楼都是乘坐电梯的,就想电梯的工作原理是什么呢?于是自己写了个控制台程序来模拟一下电梯的工作原理!
采用面向对象的编程思想!将电梯拆解为两部分;
第一部分就是每个楼层的控制器(每个楼层都有叫梯按钮的哈,一个向上一个向下)
第二部分就电梯间了。电梯间里有楼层按钮,你想上那个楼层就可以按哪个按钮了!
技术难点:状态刷新、命令顺序、电梯运行
核心代码一:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Elevator { /// <summary> /// 楼层类,每个楼层有向上叫梯命令和向下叫梯命令 /// </summary> public class Floor { Elevator elevator; /// <summary> /// 楼层号 /// </summary> private int _iFloorNo; public int IFloorNo { get { return _iFloorNo; } set { _iFloorNo = value; } } /// <summary> /// 上行需求 /// </summary> private bool _boolUp = false; /// <summary> /// 下行需求 /// </summary> private bool _boolDown = false; private bool _boolStop = false; public bool BoolStop { get { return _boolStop; } set { _boolStop = value; } } #region 构造函数 public Floor(int f,Elevator e) { _iFloorNo = f; elevator = e; } public Floor(int f) { _iFloorNo = f; } public Floor() { } #endregion /// <summary> /// 获取本层是否停靠,是否为命令层 /// </summary> /// <returns>停靠true;过false;</returns> public bool GetStatus() { return _boolDown || _boolUp; } /// <summary> /// 上行返回1;下行返回-1;本层为目的地返回0; /// </summary> /// <returns></returns> public int GetStatusFlag() { if (_boolDown) { return -1; } else if(_boolUp) { return 1; } else if(_boolStop) { return 0; } else { return -999; } } /// <summary> /// 上楼命令 /// </summary> public void CommandUp() { _boolUp = true; elevator.GoToCommandFloor(); } /// <summary> /// 下楼命令 /// </summary> public void CommandDown() { _boolDown = true; elevator.GoToCommandFloor(); } /// <summary> /// 楼层到达状态刷新 /// </summary> public void Refresh() { _boolUp = false; _boolDown = false; _boolStop = false; } /// <summary> /// 楼层比较看楼层号 /// </summary> /// <param name="floor"></param> /// <returns></returns> public int Compare(Floor floor) { int result = 1; if (this._iFloorNo > floor._iFloorNo) { result = 1; } else if (this._iFloorNo < floor._iFloorNo) { result = -1; } else { result = 0; } return result; } } }
核心代码二:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace Elevator { /// <summary> /// 电梯类,有楼层选择方法 /// </summary> public class Elevator { #region 属性 /// <summary> /// 当前楼层 /// </summary> public Floor _floorCurrent ; /// <summary> /// 全部楼层 /// </summary> public List<Floor> _floorAll = new List<Floor>(); #endregion /// <summary> /// 我想在所输入参数楼层停止 /// </summary> /// <param name="floor">想要停止的楼层</param> public void CommandStop(Floor floor) { floor.BoolStop = true; GoToTargetFloor(); } /// <summary> /// 电梯向上运行,运行到floor层 /// </summary> /// <param name="floor"></param> public void GoUp(Floor floor) { if (_floorCurrent.Compare(floor) < 0) { Thread.Sleep(300); Console.WriteLine("上行:" + _floorCurrent.IFloorNo); int index = _floorAll.IndexOf(_floorCurrent); _floorCurrent = _floorAll[index + 1]; GoUp(floor); } else { Thread.Sleep(500); Reach(floor); } } /// <summary> /// 电梯向下运行 /// </summary> public void GoDown(Floor floor) { if (_floorCurrent.Compare(floor) > 0) { Thread.Sleep(300); Console.WriteLine("下行:" + _floorCurrent.IFloorNo); int index = _floorAll.IndexOf(_floorCurrent); _floorCurrent = _floorAll[index - 1]; GoDown(floor); } else { Thread.Sleep(500); Reach(floor); } } /// <summary> /// 前往命令层,循环列表是否有命令层 /// </summary> public void GoToCommandFloor() { foreach (var item in _floorAll) { if (item.GetStatus()) { if (_floorCurrent.Compare(item) < 0) { GoUp(item); } else if (_floorCurrent.Compare(item) > 0) { GoDown(item); } } } } /// <summary> /// 前往目标楼层 /// </summary> public void GoToTargetFloor() { foreach (var item in _floorAll) { if (item.GetStatusFlag()==0) { if (_floorCurrent.Compare(item) < 0) { GoUp(item); } else if (_floorCurrent.Compare(item) > 0) { GoDown(item); } } } } /// <summary> /// 到达楼层命令 /// </summary> public void Reach(Floor f) { Console.WriteLine("电梯门打开,停靠层:"+f.IFloorNo); f.Refresh(); } } }
核心代码三:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace Elevator { class Program { static void Main(string[] args) { Elevator elevator = new Elevator(); List<Floor> lfloor = new List<Floor>(); Floor f1 = new Floor(1, elevator); Floor f2 = new Floor(2, elevator); Floor f3 = new Floor(3, elevator); Floor f4 = new Floor(4, elevator); Floor f5 = new Floor(5, elevator); Floor f6 = new Floor(6, elevator); Floor f7 = new Floor(7, elevator); Floor f8 = new Floor(8, elevator); Floor f9 = new Floor(9, elevator); Floor f10 = new Floor(10, elevator); lfloor.Add(f1); lfloor.Add(f2); lfloor.Add(f3); lfloor.Add(f4); lfloor.Add(f5); lfloor.Add(f6); lfloor.Add(f7); lfloor.Add(f8); lfloor.Add(f9); lfloor.Add(f10); elevator._floorAll = lfloor; //设置当前楼层 elevator._floorCurrent = f1; //2楼叫梯 想下楼 f2.CommandDown(); //目标楼层为1楼 elevator.CommandStop(f1); //4楼叫梯 f4.CommandUp(); //目标楼层为8楼 elevator.CommandStop(f8); Console.ReadLine(); } } }
以上所述就是本文的全部内容了,希望对大家熟练掌握C#能够有所帮助。
加载全部内容