C#控制台模拟电梯工作原理
每天上下楼都是乘坐电梯的,就想电梯的工作原理是什么呢?于是自己写了个控制台程序来模拟一下电梯的工作原理!
采用面向对象的编程思想!将电梯拆解为两部分;
第一部分就是每个楼层的控制器(每个楼层都有叫梯按钮的哈,一个向上一个向下)
第二部分就电梯间了。电梯间里有楼层按钮,你想上那个楼层就可以按哪个按钮了!
技术难点:状态刷新、命令顺序、电梯运行
核心代码一:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespaceElevator { ///<summary> ///楼层类,每个楼层有向上叫梯命令和向下叫梯命令 ///</summary> publicclassFloor { Elevatorelevator; ///<summary> ///楼层号 ///</summary> privateint_iFloorNo; publicintIFloorNo { get{return_iFloorNo;} set{_iFloorNo=value;} } ///<summary> ///上行需求 ///</summary> privatebool_boolUp=false; ///<summary> ///下行需求 ///</summary> privatebool_boolDown=false; privatebool_boolStop=false; publicboolBoolStop { get{return_boolStop;} set{_boolStop=value;} } #region构造函数 publicFloor(intf,Elevatore) { _iFloorNo=f; elevator=e; } publicFloor(intf) { _iFloorNo=f; } publicFloor() { } #endregion ///<summary> ///获取本层是否停靠,是否为命令层 ///</summary> ///<returns>停靠true;过false;</returns> publicboolGetStatus() { return_boolDown||_boolUp; } ///<summary> ///上行返回1;下行返回-1;本层为目的地返回0; ///</summary> ///<returns></returns> publicintGetStatusFlag() { if(_boolDown) { return-1; } elseif(_boolUp) { return1; } elseif(_boolStop) { return0; } else { return-999; } } ///<summary> ///上楼命令 ///</summary> publicvoidCommandUp() { _boolUp=true; elevator.GoToCommandFloor(); } ///<summary> ///下楼命令 ///</summary> publicvoidCommandDown() { _boolDown=true; elevator.GoToCommandFloor(); } ///<summary> ///楼层到达状态刷新 ///</summary> publicvoidRefresh() { _boolUp=false; _boolDown=false; _boolStop=false; } ///<summary> ///楼层比较看楼层号 ///</summary> ///<paramname="floor"></param> ///<returns></returns> publicintCompare(Floorfloor) { intresult=1; if(this._iFloorNo>floor._iFloorNo) { result=1; } elseif(this._iFloorNo<floor._iFloorNo) { result=-1; } else { result=0; } returnresult; } } }
核心代码二:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading; namespaceElevator { ///<summary> ///电梯类,有楼层选择方法 ///</summary> publicclassElevator { #region属性 ///<summary> ///当前楼层 ///</summary> publicFloor_floorCurrent; ///<summary> ///全部楼层 ///</summary> publicList<Floor>_floorAll=newList<Floor>(); #endregion ///<summary> ///我想在所输入参数楼层停止 ///</summary> ///<paramname="floor">想要停止的楼层</param> publicvoidCommandStop(Floorfloor) { floor.BoolStop=true; GoToTargetFloor(); } ///<summary> ///电梯向上运行,运行到floor层 ///</summary> ///<paramname="floor"></param> publicvoidGoUp(Floorfloor) { if(_floorCurrent.Compare(floor)<0) { Thread.Sleep(300); Console.WriteLine("上行:"+_floorCurrent.IFloorNo); intindex=_floorAll.IndexOf(_floorCurrent); _floorCurrent=_floorAll[index+1]; GoUp(floor); } else{ Thread.Sleep(500); Reach(floor); } } ///<summary> ///电梯向下运行 ///</summary> publicvoidGoDown(Floorfloor) { if(_floorCurrent.Compare(floor)>0) { Thread.Sleep(300); Console.WriteLine("下行:"+_floorCurrent.IFloorNo); intindex=_floorAll.IndexOf(_floorCurrent); _floorCurrent=_floorAll[index-1]; GoDown(floor); } else{ Thread.Sleep(500); Reach(floor); } } ///<summary> ///前往命令层,循环列表是否有命令层 ///</summary> publicvoidGoToCommandFloor() { foreach(varitemin_floorAll) { if(item.GetStatus()) { if(_floorCurrent.Compare(item)<0) { GoUp(item); } elseif(_floorCurrent.Compare(item)>0) { GoDown(item); } } } } ///<summary> ///前往目标楼层 ///</summary> publicvoidGoToTargetFloor(){ foreach(varitemin_floorAll) { if(item.GetStatusFlag()==0) { if(_floorCurrent.Compare(item)<0) { GoUp(item); } elseif(_floorCurrent.Compare(item)>0) { GoDown(item); } } } } ///<summary> ///到达楼层命令 ///</summary> publicvoidReach(Floorf) { Console.WriteLine("电梯门打开,停靠层:"+f.IFloorNo); f.Refresh(); } } }
核心代码三:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading; namespaceElevator { classProgram { staticvoidMain(string[]args) { Elevatorelevator=newElevator(); List<Floor>lfloor=newList<Floor>(); Floorf1=newFloor(1,elevator); Floorf2=newFloor(2,elevator); Floorf3=newFloor(3,elevator); Floorf4=newFloor(4,elevator); Floorf5=newFloor(5,elevator); Floorf6=newFloor(6,elevator); Floorf7=newFloor(7,elevator); Floorf8=newFloor(8,elevator); Floorf9=newFloor(9,elevator); Floorf10=newFloor(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#能够有所帮助。