I want to create a program where I can save cars based of their models, types and license plates. I try to save all of this information inside a dictionary but they aren't saved as to when I search for them through the 2nd option ("2. Search Vehicle") they aren't found. Here is my code:
What should I do in order for my code to work? I want to be able to search for the cars through their license plates (Swedish license plates, ABC123) but I can't figure out why the dictionary values aren't saved which I enter through my ReadLines()
Dictionary<string, string> newVehicle = new Dictionary<string, string>(); bool sunfleetAdminTools = true; Dictionary<string, string> newVehicle = new Dictionary<string, string>(); do { WriteLine("1. Add Vehicle"); WriteLine("2. Search Vehicle"); WriteLine("3. Log Out"); ConsoleKeyInfo keyInput = ReadKey(true); Clear(); switch (keyInput.Key) { case ConsoleKey.D1: Clear(); bool invalidCarCredentials = true; do { Write("Registration Number: "); string regNumber = ReadLine(); newVehicle["Registration Number"] = regNumber; Write("Brand: "); string brand = ReadLine(); newVehicle["Brand"] = brand; Write("Model: "); string vehicleModel = ReadLine(); newVehicle["Model: "] = vehicleModel; Write("Type (Sedan, Compact, Subcompact): "); string vehicleType = ReadLine(); newVehicle["Type: "] = vehicleType; Write("Autopilot (Yes, No): "); string autoPilot = ReadLine(); newVehicle["Autopilot: "] = autoPilot; Clear(); foreach (KeyValuePair<string, string> kvp in newVehicle) { WriteLine("Car {0}: {1}", kvp.Key, kvp.Value); } WriteLine("Is this correct? (Y)es (N)o"); ConsoleKeyInfo newInput = ReadKey(true); if (newInput.Key == ConsoleKey.Y) { Clear(); WriteLine("Vehicle registered."); Thread.Sleep(2000); Clear(); break; } else if (newInput.Key == ConsoleKey.N) { Clear(); } } while (invalidCarCredentials); break; case ConsoleKey.D2: Write("Search for vehicle by license plate: "); string searchingForVehicle = ReadLine(); if (newVehicle.ContainsValue(searchingForVehicle)) { WriteLine("Value found."); foreach (KeyValuePair<string, string> kvp in newVehicle) { WriteLine("Car {0}: {1}", kvp.Key, kvp.Value); } } else { WriteLine("Car not found."); Thread.Sleep(2000); } Clear(); break; case ConsoleKey.D3: Clear(); WriteLine("Logging out..."); Thread.Sleep(1000); sunfleetAdminTools = false; Environment.Exit(0); break; } } while (sunfleetAdminTools);
Carclass/object and then haveList<Car>rather than using a dictionary like that.Vehiclewith propertyPlateNumber, then after entering several cars the search function make sense. Currently you are checking if any property of car equal to what you enter and then just dump whole dictionary.