// Add Vehicle and Print it public void AddPrintPark(GarageHandler garageHandler, string valueKey) { string valueregNo; string valueColor; int IntValue; printAction("Enter vehicle regNo:"); valueregNo = Console.ReadLine(); printAction("Enter vehicle color:"); valueColor = Console.ReadLine(); if (valueKey == "1") { IntValue = InputVehiclePark(valueKey); garageHandler.Add(new Cars("car", valueregNo, valueColor, 4, IntValue)); printAction($"This car park: {valueregNo}, {valueColor}, {4},{IntValue}"); } else if (valueKey == "2") { IntValue = InputVehiclePark(valueKey); garageHandler.Add(new Motorcycles("motorcycle", valueregNo, valueColor, 2, IntValue)); printAction($"This motorcycle park: {valueregNo}, {valueColor}, {2},{IntValue} "); } else if (valueKey == "3") { IntValue = InputVehiclePark(valueKey); garageHandler.Add(new Mopeds("Mopeds", valueregNo, valueColor, 2, IntValue)); printAction($"This mopeds park: {valueregNo}, {valueColor}, {2},{IntValue}"); } else if (valueKey == "4") { IntValue = InputVehiclePark(valueKey); garageHandler.Add(new Airplane("Airplane", valueregNo, valueColor, 2, IntValue)); printAction($"This airplane park: {valueregNo}, {valueColor},{2},{IntValue}"); } else if (valueKey == "5") { IntValue = InputVehiclePark(valueKey); garageHandler.Add(new Bus("Bus", valueregNo, valueColor, 4, IntValue)); printAction($"This bus park: {valueregNo}, {valueColor},{4},{IntValue}"); } else if (valueKey == "6") { IntValue = InputVehiclePark(valueKey); garageHandler.Add(new Boat("Boat", valueregNo, valueColor, 0, IntValue)); printAction($"This boat park: {valueregNo}, {valueColor},{0},{IntValue}"); } else if (valueKey == "7") { IntValue = InputVehiclePark(valueKey); garageHandler.Add(new Bicycle("Bicycle", valueregNo, valueColor, 2, IntValue)); printAction($"This bicycle park: {valueregNo}, {valueColor},{2},{IntValue}"); } }
// Remove Vehicle and Print it public void RemovePrintPark(GarageHandler garageHandler, string valueKey) { string valueregNo; string valueColor; int IntValue; printAction("Enter vehicle regNo:"); valueregNo = Console.ReadLine(); printAction("Enter vehicle color:"); valueColor = Console.ReadLine(); if (valueKey == "1") { IntValue = InputVehiclePark(valueKey); garageHandler.RemoveRegNumHandler(valueregNo); printAction($"This car unpark: {valueregNo}, {valueColor}, {4}, {IntValue}"); } else if (valueKey == "2") { IntValue = InputVehiclePark(valueKey); garageHandler.RemoveRegNumHandler(valueregNo); printAction($"This motorcycle unpark: {valueregNo}, {valueColor}, {2}, {IntValue} "); } else if (valueKey == "3") { IntValue = InputVehiclePark(valueKey); garageHandler.RemoveRegNumHandler(valueregNo); printAction($"This mopeds unpark: {valueregNo}, {valueColor}, {2},{IntValue}"); } else if (valueKey == "4") { IntValue = InputVehiclePark(valueKey); garageHandler.RemoveRegNumHandler(valueregNo); printAction($"This airplane unpark: {valueregNo}, {valueColor},{2}, {IntValue}"); } else if (valueKey == "5") { IntValue = InputVehiclePark(valueKey); garageHandler.RemoveRegNumHandler(valueregNo); printAction($"This bus unpark: {valueregNo}, {valueColor},{4}, {IntValue}"); } else if (valueKey == "6") { IntValue = InputVehiclePark(valueKey); garageHandler.RemoveRegNumHandler(valueregNo); printAction($"This boat unpark: {valueregNo}, {valueColor},{0}, {IntValue}"); } else if (valueKey == "7") { IntValue = InputVehiclePark(valueKey); garageHandler.RemoveRegNumHandler(valueregNo); printAction($"This bicycle unpark: {valueregNo}, {valueColor},{2}, {IntValue}"); } }
static void Main(string[] args) { //Action<string> printActioMain = i => Console.WriteLine(i); UI ui = new UI(); var garageHandler = new GarageHandler(10); var garage = new Garage <Vehicle>(20); garageHandler.Add(new Cars("Car", "112AB", "red", 4, 12)); garageHandler.Add(new Cars("Car", "112B", "red", 4, 12)); garageHandler.Add(new Motorcycles("motorcycles", "112b", "green", 2, 13)); garageHandler.Add(new Motorcycles("motorcycles", "112b", "green", 2, 13)); garageHandler.Add(new Airplane("Airplane", "132AA", "red", 2, 15)); garageHandler.Add(new Bus("Bus", "132aa", "white", 2, 1)); while (true) { Console.WriteLine("Please navigate through the menu by inputting the number \n(1, 2, 3 ,4, 5, 6, 7, 0) of your choice" + "\n1. List all parked vehicles" + "\n2. List all vehicle types currently parked in the garage and how many of each kind" + "\n3. Park and unpark specific vehicles from the garage" + "\n4. Find a vehicles based on color and wheels " + "\n5. Search on a vehicle based on Reg Number" + "\n6. Set maximum capacity of the Garage " + "\n7. Save and load from File" + "\n0. Exit the application"); char input = ' '; //Creates the character input to be used with the switch-case below. try { input = Console.ReadLine()[0]; //Tries to set input to the first char in an input line } catch (IndexOutOfRangeException) //If the input line is empty, we ask the users for some input. { Console.Clear(); Console.WriteLine("Please enter some input!"); } switch (input) { case '1': foreach (var v in garageHandler.GetVehicleInfo()) { Console.WriteLine($"this vehicle park: {v.Name}, {v.Color}, {v.RegNo}"); } break; case '2': garageHandler.CountVehicleHandler(); break; case '3': Console.WriteLine("Type '+' or '-' to park or unpark from the Garage or zero to exit :"); string input1 = Console.ReadLine(); char nav = input1[0]; ui.ParkUnPark(nav, input1, garageHandler); break; case '4': Console.WriteLine("Enter vehicle color:"); string valueColor = Console.ReadLine(); Console.WriteLine("Enter vehicle numWheels:"); string valuenumWheels = Console.ReadLine(); garageHandler.FindVehicleColorwheelsHandler(valueColor, ui.Validinput(valuenumWheels)); break; case '5': Console.WriteLine("Enter vehicle Reg number:"); string valueRegNum = Console.ReadLine(); garageHandler.SearchVehicleHandler(valueRegNum); break; case '6': Console.WriteLine("Please enter garage capacity:"); string input2 = Console.ReadLine(); garageHandler.CapacityGarageHandler(input2); break; case '7': //GarageFile obj = new GarageFile(); //obj.id = 1; //obj.name = ".Net"; IFormatter formatter = new BinaryFormatter(); FileStream stream = new FileStream(@"C:\Users\Elev\Desktop\ExampleNew.txt", FileMode.Create, FileAccess.Write); formatter.Serialize(stream, garage); stream.Close(); stream = new FileStream(@"C:\Users\Elev\Desktop\ExampleNew.txt", FileMode.Open, FileAccess.Read); Garage <Vehicle> objnew = (Garage <Vehicle>)formatter.Deserialize(stream); foreach (var v in garage) { Console.WriteLine($"this vehicle park: {v.Name}, {v.Color}, {v.RegNo}"); } //Console.WriteLine(objnew.name); //Console.WriteLine(objnew.id); Console.ReadKey(); break; case '0': return; default: Console.WriteLine("Please enter some valid input (0, 1, 2, 3, 4, 5, 6)"); break; } } }
//Push and pop to garage public void ParkUnPark(char nav, string input, GarageHandler garageHandler) { string valuekey = input.Substring(1); switch (nav) { case '0': { return; } case '+': { printAction("Enter Key number to add vehicle: " + "\n1 Car" + "\n2 Motorcycles" + "\n3 Mopeds" + "\n4 Airplane" + "\n5 Bus" + "\n6 Boat" + "\n7 Bicycle"); string valueKey = Console.ReadLine(); if (valueKey != "1" && valueKey != "2" && valueKey != "3" && valueKey != "4" && valueKey != "5" && valueKey != "6" && valueKey != "7") { printAction($"Your enter key is wrong!"); } else { AddPrintPark(garageHandler, valueKey); } break; } case '-': { Console.WriteLine("Enter Key number to remove vehicle:" + "\n1 Car" + "\n2 Motorcycles" + "\n3 Mopeds" + "\n4 Airplane" + "\n5 Bus" + "\n6 Boat" + "\n7 Bicycle"); string valueKey = Console.ReadLine(); if (valueKey != "1" && valueKey != "2" && valueKey != "3" && valueKey != "4" && valueKey != "5" && valueKey != "6" && valueKey != "7") { printAction("Your enter key is wrong!"); } else { RemovePrintPark(garageHandler, valueKey); } break; } } }