Esempio n. 1
0
        private void btn_RunThroughSim_Click(object sender, EventArgs e)
        {
            try
            {
                HideWhileRunningLabels();
                while (building.CheckIfSimulationCompleted() == false)
                {
                    building.PopulateFloors(csvParser.GetCurrentTimeParsedListPerson());
                    building.StartElevator(elevator);

                    foreach (Floor f in building.GetFloors())
                    {
                        string text = "";
                        if (f.GetPeopleOnFloor().Count > 0)
                        {
                            foreach (Person p in f.GetPeopleOnFloor())
                            {
                                text += p.End_floor + ", ";
                            }
                        }
                        else
                        {
                            text += " ";
                        }
                        text += "\n";
                        lb_PeopleOnFloors.Items.Add(text);
                    }
                    string currentPeople = "";
                    foreach (Person p in elevator.GetCurrentPassagers())
                    {
                        currentPeople += p.End_floor + ", ";
                    }
                    lb_PeopleInElevator.Items.Add(currentPeople);
                }
                btn_RunThroughSim.Enabled = false;
                btn_StepByStep.Enabled    = false;
                MessageBox.Show("Simulation complete!");
                elevator.CalculateTotalTime();
                ShowAfterRunningLabels();
            }
            catch (Exception)
            {
                MessageBox.Show("Something went wrong, please try again!");
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Adds each person on a specific floor to our elevator, and then removes then from the floor that they were coming from.
 /// </summary>
 /// <param name="elevator">Uses an elevator object in order to
 /// access it's methods and use the same object, instead of creating a new one</param>
 public void AddPersonToElevator(Elevator elevator)
 {
     foreach (Floor f in allFloors.Where(x => x.GetFloorNumber() == elevator.GetCurrentFloor()).Take(1))
     {
         foreach (Person p in f.GetPeopleOnFloor())
         {
             elevator.GetCurrentPassagers().Add(p);
             f.RemovePersonFromFloor(p);
         }
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Method for checking if our elevator and all of our floors are empty or not.
        /// </summary>
        /// <returns>Returns true of false based on of there are people left</returns>
        public bool CheckIfPeopleWaiting(Elevator elevat)
        {
            int peopleLeft = 0;

            foreach (Floor f in allFloors)
            {
                if (f.StillPeopleWaiting() == true)
                {
                    peopleLeft++;
                }
            }
            if (elevat.GetCurrentPassagers().Count > 0)
            {
                foreach (Person p in elevat.GetCurrentPassagers())
                {
                    peopleLeft++;
                }
            }
            if (peopleLeft > 0)
            {
                return(true);
            }
            return(false);
        }
Esempio n. 4
0
        /// <summary>
        /// Main method used for running the simulation.
        /// The algorithm decides whether to go up or down based on where people currently standing in the elevator needs to get off.
        /// As long as there either are people waiting on any floor, or people waiting inside the elevator, the returned bool is true.
        /// </summary>
        /// <param name="elevator">Uses an elevator object in order to
        /// access it's methods and use the same object, instead of creating a new one.</param>
        /// <param name="numFloors">An integer representing the number of floors in our building, it is possible to change this variable</param>
        public void StartElevator(Elevator elevator, int numFloors = 10)
        {
            simulationComplete = false;
            int minFloorNum = 0;
            int maxFloorNum = numFloors - 1;

            if (elevator.GetCurrentPassagers().Count > 0)
            {
                elevator.RemovePersonFromElevator();

                int goingUp   = 0;
                int goingDown = 0;
                foreach (Person p in elevator.GetCurrentPassagers().ToList())
                {
                    if (p.End_floor > elevator.GetCurrentFloor())
                    {
                        goingUp++;
                    }
                    else
                    {
                        goingDown++;
                    }
                }
                if (goingUp > goingDown)
                {
                    elevator.SetCurrentDirection(Direction.DirectionEnum.Up);
                    elevator.MoveElevator(Direction.DirectionEnum.Up);
                }
                else
                {
                    elevator.SetCurrentDirection(Direction.DirectionEnum.Down);
                    elevator.MoveElevator(Direction.DirectionEnum.Down);
                }
                elevator.IncreaseSystemTime();
                IncreaseWaitTime();
            }
            else if (CheckIfPeopleWaiting(elevator) == true)
            {
                int currFloorNum = elevator.GetCurrentFloor();


                if (currFloorNum == minFloorNum)
                {
                    goUp = true;
                    elevator.AddPersonToElevator();
                    elevator.RemovePeopleFromFloor();
                    elevator.SetCurrentDirection(Direction.DirectionEnum.Up);
                    elevator.MoveElevator(Direction.DirectionEnum.Up);
                }
                else if (currFloorNum == maxFloorNum)
                {
                    goUp = false;
                    elevator.AddPersonToElevator();
                    elevator.RemovePeopleFromFloor();
                    elevator.SetCurrentDirection(Direction.DirectionEnum.Down);
                    elevator.MoveElevator(Direction.DirectionEnum.Down);
                }
                else if (goUp == true)
                {
                    elevator.AddPersonToElevator();
                    elevator.RemovePeopleFromFloor();
                    elevator.SetCurrentDirection(Direction.DirectionEnum.Up);
                    elevator.MoveElevator(Direction.DirectionEnum.Up);
                }
                else if (goUp == false)
                {
                    elevator.AddPersonToElevator();
                    elevator.RemovePeopleFromFloor();
                    elevator.SetCurrentDirection(Direction.DirectionEnum.Down);
                    elevator.MoveElevator(Direction.DirectionEnum.Down);
                }
                elevator.IncreaseSystemTime();
                IncreaseWaitTime();
            }
            else if (CheckIfPeopleWaiting(elevator) == false)
            {
                simulationComplete = true;
            }
        }