C console simulates elevator working principle

  • 2021-06-29 11:48:13
  • OfStack

Every day I take the elevator upstairs and downstairs, just think how the elevator works?So I wrote a console program to simulate how 1 elevator works!
Use object-oriented programming ideas!Disassemble the elevator into two parts;
The first part is the controller of each floor (each floor has a call button, one up and one down)
Part 2 is the elevator room.There are floor buttons in the elevator room. You can press which button if you want to go up that floor!
Technical difficulties: status refresh, command order, elevator operation

Core Code 1:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Elevator
{
  /// <summary>
  ///  Floor class, each floor has a call-up and call-down commands 
  /// </summary>
  public class Floor
  {
    Elevator elevator;
    /// <summary>
    ///  Floor number 
    /// </summary>
    private int _iFloorNo;
 
    public int IFloorNo
    {
      get { return _iFloorNo; }
      set { _iFloorNo = value; }
    }
    /// <summary>
    ///  Up demand 
    /// </summary>
    private bool _boolUp = false;
    /// <summary>
    ///  Downstream Requirements 
    /// </summary>
    private bool _boolDown = false;
 
    private bool _boolStop = false;
 
    public bool BoolStop
    {
      get { return _boolStop; }
      set { _boolStop = value; }
    }
 
    #region  Constructor 
     
    
    public Floor(int f,Elevator e)
    {
      _iFloorNo = f;
      elevator = e;
    }
 
    public Floor(int f)
    {
      _iFloorNo = f;
    }
 
    public Floor()
    {
    }
    #endregion
 
    /// <summary>
    ///  Gets whether the layer is docked or command-level 
    /// </summary>
    /// <returns> Stop true ;too false ; </returns>
    public bool GetStatus()
    {
      return _boolDown || _boolUp;
    }
 
    /// <summary>
    ///  Up Return 1 ;Down Return -1 ;Layer returns for destination 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>
    ///  Upstairs Command 
    /// </summary>
    public void CommandUp()
    {
      _boolUp = true;
      elevator.GoToCommandFloor();
    }
 
    /// <summary>
    ///  Downstairs Command 
    /// </summary>
    public void CommandDown()
    {
      _boolDown = true;
      elevator.GoToCommandFloor();
    }
 
    /// <summary>
    ///  Floor arrival refresh 
    /// </summary>
    public void Refresh()
    {
      _boolUp = false;
      _boolDown = false;
      _boolStop = false;
    }
 
 
    /// <summary>
    ///  Floor Number by Floor Comparison 
    /// </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;
    }
  }
}

Core Code 2:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
 
namespace Elevator
{
  /// <summary>
  ///  Elevator class, with floor selection method 
  /// </summary>
  public class Elevator
  {
    #region  attribute 
 
    /// <summary>
    ///  Current Floor 
    /// </summary>
    public Floor _floorCurrent ;
    /// <summary>
    ///  All floors 
    /// </summary>
    public List<Floor> _floorAll = new List<Floor>();
 
    #endregion
 
    /// <summary>
    ///  I want to stop on the floor with the input parameter 
    /// </summary>
    /// <param name="floor"> Floor you want to stop </param>
    public void CommandStop(Floor floor)
    {
      floor.BoolStop = true;
      GoToTargetFloor();
    }
 
 
    /// <summary>
    ///  Elevator running up , Run to floor layer 
    /// </summary>
    /// <param name="floor"></param>
    public void GoUp(Floor floor)
    {
      if (_floorCurrent.Compare(floor) < 0)
      {
      Thread.Sleep(300);
      Console.WriteLine(" Upstream: " + _floorCurrent.IFloorNo);
      int index = _floorAll.IndexOf(_floorCurrent);
      _floorCurrent = _floorAll[index + 1];
      GoUp(floor);
      }
      else {
        Thread.Sleep(500);
        Reach(floor);
      }
    }
 
    /// <summary>
    ///  Elevator running down 
    /// </summary>
    public void GoDown(Floor floor)
    {
      if (_floorCurrent.Compare(floor) > 0)
      {
        Thread.Sleep(300);
        Console.WriteLine(" Down: " + _floorCurrent.IFloorNo);
        int index = _floorAll.IndexOf(_floorCurrent);
        _floorCurrent = _floorAll[index - 1];
        GoDown(floor);
      }
      else {
        Thread.Sleep(500);
        Reach(floor);
      }
    }
 
    /// <summary>
    ///  Go to Command Layer , Is there a command layer in the loop list 
    /// </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>
    ///  Go to target floor 
    /// </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>
    ///  Arrive Floor Command 
    /// </summary>
    public void Reach(Floor f)
    {
      Console.WriteLine(" Elevator door open , Docking floor: "+f.IFloorNo);
      f.Refresh();
    }
 
  }
}

Core Code 3:


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;
      // Set Current Floor 
      elevator._floorCurrent = f1;
      //2 The building is called a ladder   Want to go downstairs 
      f2.CommandDown();
      // The target floor is 1 floor 
      elevator.CommandStop(f1);
      //4 The building is called a ladder 
      f4.CommandUp();
      // The target floor is 8 floor 
      elevator.CommandStop(f8);
 
      Console.ReadLine();
    }
  }
}

This is all about this article, and I hope it will be helpful for you to master C#skillfully.


Related articles: