From 4707d81da6f1881cb6b65390bc8e43a8ba2e6838 Mon Sep 17 00:00:00 2001 From: vedanthvdev <61700595+vedanthvdev@users.noreply.github.com> Date: Thu, 6 Aug 2020 14:25:34 +0100 Subject: [PATCH 1/3] New class to keep minimum in main --- ElevatorAlgorithm/Algo.cs | 207 +++++++++++++++++++++++++++++++++ ElevatorAlgorithm/StartMain.cs | 194 +----------------------------- 2 files changed, 213 insertions(+), 188 deletions(-) create mode 100644 ElevatorAlgorithm/Algo.cs diff --git a/ElevatorAlgorithm/Algo.cs b/ElevatorAlgorithm/Algo.cs new file mode 100644 index 0000000..b801c56 --- /dev/null +++ b/ElevatorAlgorithm/Algo.cs @@ -0,0 +1,207 @@ +using System; + +namespace ElevatorAlgorithm +{ + class Algo + { + Elevator[] elevator; + Elevator change; + + public Algo(Elevator[] elevators) + { + this.elevator = elevators; + } + + public void elevatorStatus() + { + //Status of all Elevators + for (int j = 0; j < elevator.Length; j++) + { + Console.ForegroundColor = ConsoleColor.Cyan; + Console.Out.WriteLine("\nElevator " + (j + 1) + " is at Floor----> " + elevator[j].getCurrentFloor() + + "\t Number of people on it.....>" + elevator[j].getSum()); + Console.ForegroundColor = ConsoleColor.White; + } + + Console.Out.WriteLine("--------------------------++++++++-------------------------"); + } + + public void Run() + { + // Checking if the user wants Multiple floors call + Console.Out.WriteLine("\nHOW many MULTIPLE FLOORS are people waiting on?"); + + int numFloors = Int32.Parse(Console.ReadLine()); + int[] floor = new int[numFloors]; + int[] people = new int[numFloors]; + + int z = 0; + + for (int i = 0; i < numFloors; i++) + { + + //User selection of desired floor where people are waiting + while (true) + { + Console.Out.WriteLine("Please enter the NEXT FLOOR NUMBER where people are waiting..(" + + elevator[1].getMinFloor() + " to " + elevator[1].getMaxFloor() + ")"); + + floor[i] = Int32.Parse(Console.ReadLine()); + + if (floor[i] < elevator[1].getMinFloor() || floor[i] > elevator[1].getMaxFloor()) + { + //Color Coding for Error Message + Console.ForegroundColor = ConsoleColor.DarkRed; + Console.Out.WriteLine("FLOOR NUMBER INVALID"); + Console.ForegroundColor = ConsoleColor.White; + } + else + { + break; + } + + } + + // Optimizing the BEST ELEVATOR for that location selected by the user + int[] unsorted = new int[elevator.Length]; + int[] sorted = new int[elevator.Length]; + + for (int j = 0; j < elevator.Length; j++) + { + unsorted[j] = Math.Abs(floor[i] - elevator[j].getCurrentFloor()); + sorted[j] = unsorted[j]; + } + + Array.Sort(sorted); + + for (int j = 0; j < sorted.Length; j++) + { + for (int k = 0; k < unsorted.Length; k++) + { + + { + if (elevator[k].getIsIdle() == 0 && sorted[j] == unsorted[k]) + { + change = elevator[k]; //Selecting the desired elevator into temp + z = k + 1; + elevator[k].setIsIdle(1); + Console.ForegroundColor = ConsoleColor.Cyan; + Console.WriteLine("\nElevator " + z + " is selected\n"); + Console.ForegroundColor = ConsoleColor.White; + goto LoopEnd; + + } + } + } + } + LoopEnd: + + // Registering the User input of amount of users waiting on that desired floor + while (true) + { + Console.Out.WriteLine("Enter the NUMBER OF PEOPLE waiting at level " + floor[i] + "----->MAXIMUM " + + change.getMaxCapacity()); + + people[i] = Int32.Parse(Console.ReadLine()); + + change.setSum(people[i]); + if (change.getSum() > 0 && change.getSum() <= (change.getMaxCapacity())) + { + break; + } + else if (change.getSum() <= 0) + { + change.setSum(-people[i]); + Console.ForegroundColor = ConsoleColor.DarkRed; + Console.Out.WriteLine("PLEASE ENTER A POSITIVE INTEGER"); + Console.ForegroundColor = ConsoleColor.White; + } + else + { + change.setSum(-people[i]); + Console.ForegroundColor = ConsoleColor.DarkRed; + Console.Out.WriteLine("SORRY WEIGHT LIMIT OF ELEVATOR " + z + " EXEEDED...\nOnly " + + (change.getMaxCapacity() - change.getSum()) + " CAN COME ON THIS FLOOR "); + Console.ForegroundColor = ConsoleColor.White; + + } + + } + + // Moving to the selected floor Step by Step + change.moveNext(floor[i], z); + + // Assigning the number of people boarding the elevator + change.setPeople(people[i], z); + + //Keeps looping until the people on the elevator is 0 + while (change.getSum() != 0) + { + // selecting the floor where people want to get down + while (true) + { + Console.Out.WriteLine("Please enter your DESTINATION FLOOR(" + elevator[1].getMinFloor() + " to " + + elevator[1].getMaxFloor() + ")"); + + floor[i] = Int32.Parse(Console.ReadLine()); + + if (floor[i] < elevator[1].getMinFloor() || floor[i] > elevator[1].getMaxFloor()) + { + Console.ForegroundColor = ConsoleColor.DarkRed; + Console.Out.WriteLine("FLOOR NUMBER INVALID"); + Console.ForegroundColor = ConsoleColor.White; + } + else + { + break; + } + } + + // Registering the number of people getting down at that floor + while (true) + { + Console.Out.WriteLine("Enter the NUMBER OF PEOPLE getting down at " + floor[i]); + + people[i] = Int32.Parse(Console.ReadLine()); + if (change.getSum() < people[i]) + { + Console.ForegroundColor = ConsoleColor.DarkRed; + Console.Out.WriteLine( + "Sorry there are only " + change.getSum() + " people left on elevator " + z); + Console.ForegroundColor = ConsoleColor.White; + } + else if (people[i] < 0) + { + Console.ForegroundColor = ConsoleColor.DarkRed; + Console.Out.WriteLine("Please enter a positive integer..."); + Console.ForegroundColor = ConsoleColor.White; + } + else + { + change.setSum(-people[i]); + + change.moveNext(floor[i], z); + change.setPeople(-people[i], z); + break; + } + + } + + } + + // forwarding the values from temporary elevator object to desired elevator object + for (int k = 0; k < elevator.Length; k++) + { + if (z == k + 1) + { + elevator[k] = change; + elevator[k].setIsIdle(-1); + Console.WriteLine("\nElevator " + z + " has ended its action and is idle now \n"); + break; + } + + } + } + } + } +} diff --git a/ElevatorAlgorithm/StartMain.cs b/ElevatorAlgorithm/StartMain.cs index ec3fcea..dfdcb8d 100644 --- a/ElevatorAlgorithm/StartMain.cs +++ b/ElevatorAlgorithm/StartMain.cs @@ -26,7 +26,7 @@ static void Main(string[] args) } - //these variables can be changed according to the user in Variables.txt file + //these variables can be changed in Variables.txt file according to user preferences //numOfElevators = termsList[0]; //minFloor = termsList[1]; @@ -46,198 +46,16 @@ static void Main(string[] args) } - //Temporary Elevator Object - Elevator change = null; + Algo a = new Algo(elevator); while (true) { - //Status of all Elevators - for (int j = 0; j < elevator.Length; j++) - { - Console.ForegroundColor = ConsoleColor.Cyan; - Console.Out.WriteLine("\nElevator " + (j + 1) + " is at Floor----> " + elevator[j].getCurrentFloor() - + "\t Number of people on it.....>" + elevator[j].getSum()); - Console.ForegroundColor = ConsoleColor.White; - } - - Console.Out.WriteLine("--------------------------++++++++-------------------------"); - - // Checking if the user wants Multiple floors call - Console.Out.WriteLine("\nHOW many MULTIPLE FLOORS are people waiting on?"); - - int numFloors = Int32.Parse(Console.ReadLine()); - int[] floor = new int[numFloors]; - int[] people = new int[numFloors]; - - int z = 0; - - for (int i = 0; i < numFloors; i++) - { - - //User selection of desired floor where people are waiting - while (true) - { - Console.Out.WriteLine("Please enter the NEXT FLOOR NUMBER where people are waiting..(" - + elevator[1].getMinFloor() + " to " + elevator[1].getMaxFloor() + ")"); - - floor[i] = Int32.Parse(Console.ReadLine()); - - if (floor[i] < elevator[1].getMinFloor() || floor[i] > elevator[1].getMaxFloor()) - { - Console.ForegroundColor = ConsoleColor.DarkRed; //Color Coding for Error Message - Console.Out.WriteLine("FLOOR NUMBER INVALID"); - Console.ForegroundColor = ConsoleColor.White; - } - else - { - break; - } - - } - - // Optimizing the BEST ELEVATOR for that location selected by the user - int[] unsorted = new int[elevator.Length]; - int[] sorted = new int[elevator.Length]; - - for (int j = 0; j < elevator.Length; j++) - { - unsorted[j] = Math.Abs(floor[i] - elevator[j].getCurrentFloor()); - sorted[j] = unsorted[j]; - } - - Array.Sort(sorted); - - for (int j = 0; j < sorted.Length; j++) - { - for (int k = 0; k < unsorted.Length; k++) - { - - { - if (elevator[k].getIsIdle() == 0 && sorted[j] == unsorted[k]) - { - change = elevator[k]; //Selecting the desired elevator into temp - z = k + 1; - elevator[k].setIsIdle(1); - Console.ForegroundColor = ConsoleColor.Cyan; - Console.WriteLine("\nElevator " + z + " is selected\n"); - Console.ForegroundColor = ConsoleColor.White; - goto LoopEnd; - - } - } - } - } - LoopEnd: - - // Registering the User input of amount of users waiting on that desired floor - while (true) - { - Console.Out.WriteLine("Enter the NUMBER OF PEOPLE waiting at level " + floor[i] + "----->MAXIMUM " - + change.getMaxCapacity()); - - people[i] = Int32.Parse(Console.ReadLine()); - - change.setSum(people[i]); - if (change.getSum() > 0 && change.getSum() <= (change.getMaxCapacity())) - { - break; - } - else if (change.getSum() <= 0) - { - change.setSum(-people[i]); - Console.ForegroundColor = ConsoleColor.DarkRed; - Console.Out.WriteLine("PLEASE ENTER A POSITIVE INTEGER"); - Console.ForegroundColor = ConsoleColor.White; - } - else - { - change.setSum(-people[i]); - Console.ForegroundColor = ConsoleColor.DarkRed; - Console.Out.WriteLine("SORRY WEIGHT LIMIT OF ELEVATOR " + z + " EXEEDED...\nOnly " - + (change.getMaxCapacity() - change.getSum()) + " CAN COME ON THIS FLOOR "); - Console.ForegroundColor = ConsoleColor.White; - - } - - } - - // Moving to the selected floor Step by Step - change.moveNext(floor[i], z); - - // Assigning the number of people boarding the elevator - change.setPeople(people[i], z); - - //Keeps looping until the people on the elevator is 0 - while (change.getSum() != 0) - { - // selecting the floor where people want to get down - while (true) - { - Console.Out.WriteLine("Please enter your DESTINATION FLOOR(" + elevator[1].getMinFloor() + " to " - + elevator[1].getMaxFloor() + ")"); - - floor[i] = Int32.Parse(Console.ReadLine()); - - if (floor[i] < elevator[1].getMinFloor() || floor[i] > elevator[1].getMaxFloor()) - { - Console.ForegroundColor = ConsoleColor.DarkRed; - Console.Out.WriteLine("FLOOR NUMBER INVALID"); - Console.ForegroundColor = ConsoleColor.White; - } - else - { - break; - } - } - - // Registering the number of people getting down at that floor - while (true) - { - Console.Out.WriteLine("Enter the NUMBER OF PEOPLE getting down at " + floor[i]); - - people[i] = Int32.Parse(Console.ReadLine()); - if (change.getSum() < people[i]) - { - Console.ForegroundColor = ConsoleColor.DarkRed; - Console.Out.WriteLine( - "Sorry there are only " + change.getSum() + " people left on elevator " + z); - Console.ForegroundColor = ConsoleColor.White; - } - else if (people[i] < 0) - { - Console.ForegroundColor = ConsoleColor.DarkRed; - Console.Out.WriteLine("Please enter a positive integer..."); - Console.ForegroundColor = ConsoleColor.White; - } - else - { - change.setSum(-people[i]); - - change.moveNext(floor[i], z); - change.setPeople(-people[i], z); - break; - } - - } - - } - - // forwarding the values from temporary elevator object to desired elevator object - for (int k = 0; k < elevator.Length; k++) - { - if (z == k + 1) - { - elevator[k] = change; - elevator[k].setIsIdle(-1); - Console.WriteLine("\nElevator " + z + " has ended its action and is idle now \n"); - break; - } - - } - - } + a.elevatorStatus(); + + a.Run(); + } } From 8604b3b58fd3cfe8684e4b9848df22ed18d7038e Mon Sep 17 00:00:00 2001 From: vedanthvdev <61700595+vedanthvdev@users.noreply.github.com> Date: Thu, 6 Aug 2020 14:50:58 +0100 Subject: [PATCH 2/3] Cleared Main --- ElevatorAlgorithm/Algo.cs | 60 +++++++++++++++++++++++++++------- ElevatorAlgorithm/StartMain.cs | 44 ++----------------------- 2 files changed, 51 insertions(+), 53 deletions(-) diff --git a/ElevatorAlgorithm/Algo.cs b/ElevatorAlgorithm/Algo.cs index b801c56..798810f 100644 --- a/ElevatorAlgorithm/Algo.cs +++ b/ElevatorAlgorithm/Algo.cs @@ -1,4 +1,7 @@ using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; namespace ElevatorAlgorithm { @@ -7,10 +10,42 @@ class Algo Elevator[] elevator; Elevator change; - public Algo(Elevator[] elevators) + public Algo() { - this.elevator = elevators; - } + + //Assigning all default values like minFloor,maxFloor,maxCapacity and number of elevators from a text file Variables.txt + + List termsList = new List(); + + String filepath = @"../../../Variables.txt"; + List lines = File.ReadAllLines(filepath).ToList(); + + foreach (var line in lines) + { + + string[] entries = line.Split('='); + + termsList.Add(Int32.Parse(entries[1])); + + } + + //termsList[0] = number of elevators; + //termsList[1] = lowest floor; + //termsList[2] = highest floor; + //termsList[3] = maximun number of people an elevator can carry; + + this.elevator = new Elevator[termsList[0]]; + + //Assigning Lowest Floor + //Highest Floor + //Maximun Capacity + //to all the elevators present + for (int i = 0; i < termsList[0]; i++) + { + this.elevator[i] = new Elevator(termsList[1], termsList[2], termsList[3]); + + } + } public void elevatorStatus() { @@ -23,12 +58,12 @@ public void elevatorStatus() Console.ForegroundColor = ConsoleColor.White; } - Console.Out.WriteLine("--------------------------++++++++-------------------------"); + Console.Out.WriteLine("--------------------------++++++++--------------------------"); } public void Run() { - // Checking if the user wants Multiple floors call + // Checking if the user is calling for Multiple floors Console.Out.WriteLine("\nHOW many MULTIPLE FLOORS are people waiting on?"); int numFloors = Int32.Parse(Console.ReadLine()); @@ -40,17 +75,17 @@ public void Run() for (int i = 0; i < numFloors; i++) { - //User selection of desired floor where people are waiting + //Selecting floor where people are waiting while (true) { Console.Out.WriteLine("Please enter the NEXT FLOOR NUMBER where people are waiting..(" + elevator[1].getMinFloor() + " to " + elevator[1].getMaxFloor() + ")"); - floor[i] = Int32.Parse(Console.ReadLine()); + floor[i] = Int32.Parse(Console.ReadLine()); //user Inputting the floor if (floor[i] < elevator[1].getMinFloor() || floor[i] > elevator[1].getMaxFloor()) { - //Color Coding for Error Message + //Color Coding for Error Messages Console.ForegroundColor = ConsoleColor.DarkRed; Console.Out.WriteLine("FLOOR NUMBER INVALID"); Console.ForegroundColor = ConsoleColor.White; @@ -62,7 +97,7 @@ public void Run() } - // Optimizing the BEST ELEVATOR for that location selected by the user + // Optimizing the BEST ELEVATOR for the location selected by the USER int[] unsorted = new int[elevator.Length]; int[] sorted = new int[elevator.Length]; @@ -82,7 +117,8 @@ public void Run() { if (elevator[k].getIsIdle() == 0 && sorted[j] == unsorted[k]) { - change = elevator[k]; //Selecting the desired elevator into temp + //Selecting the desired elevator into temp + change = elevator[k]; z = k + 1; elevator[k].setIsIdle(1); Console.ForegroundColor = ConsoleColor.Cyan; @@ -96,13 +132,13 @@ public void Run() } LoopEnd: - // Registering the User input of amount of users waiting on that desired floor + // Registering the amount of users waiting on selected floor while (true) { Console.Out.WriteLine("Enter the NUMBER OF PEOPLE waiting at level " + floor[i] + "----->MAXIMUM " + change.getMaxCapacity()); - people[i] = Int32.Parse(Console.ReadLine()); + people[i] = Int32.Parse(Console.ReadLine()); change.setSum(people[i]); if (change.getSum() > 0 && change.getSum() <= (change.getMaxCapacity())) diff --git a/ElevatorAlgorithm/StartMain.cs b/ElevatorAlgorithm/StartMain.cs index dfdcb8d..3f8663d 100644 --- a/ElevatorAlgorithm/StartMain.cs +++ b/ElevatorAlgorithm/StartMain.cs @@ -1,8 +1,4 @@ -using System; -using System.IO; -using System.Collections.Generic; -using System.Linq; - + namespace ElevatorAlgorithm { @@ -10,43 +6,9 @@ class StartMain { static void Main(string[] args) { - //Assigning all default values like minFloor,maxFloor,maxCapacity and number of elevators from a text file - //this could be used but have to create the file according to the location - - List termsList = new List(); - - String filepath = @"../../../Variables.txt"; - List lines = File.ReadAllLines(filepath).ToList(); - - foreach(var line in lines){ - - string[] entries = line.Split('='); - - termsList.Add(Int32.Parse(entries[1])); - - } - - //these variables can be changed in Variables.txt file according to user preferences - - //numOfElevators = termsList[0]; - //minFloor = termsList[1]; - //maxFloor = termsList[2]; - //maxCapacity = termsList[3]; - - - Elevator[] elevator = new Elevator[termsList[0]]; - - // Assigning Lowest Floor - //Highest Floor - //Maximun Capacity - //to all the elevators present - for (int i = 0; i < termsList[0]; i++) - { - elevator[i] = new Elevator(termsList[1], termsList[2], termsList[3]); - - } + - Algo a = new Algo(elevator); + Algo a = new Algo(); while (true) { From adffc4f77814eeeaad8f7568a37b695b5a1d372f Mon Sep 17 00:00:00 2001 From: vedanthvdev <61700595+vedanthvdev@users.noreply.github.com> Date: Fri, 7 Aug 2020 16:38:13 +0100 Subject: [PATCH 3/3] Elevator Interface is called instead of elevator class --- ElevatorAlgorithm/Algo.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ElevatorAlgorithm/Algo.cs b/ElevatorAlgorithm/Algo.cs index 798810f..62d75c8 100644 --- a/ElevatorAlgorithm/Algo.cs +++ b/ElevatorAlgorithm/Algo.cs @@ -7,8 +7,8 @@ namespace ElevatorAlgorithm { class Algo { - Elevator[] elevator; - Elevator change; + ElevatorInterface[] elevator; + ElevatorInterface change; public Algo() {