1

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); 
4
  • 4
    I recommend you create a Car class/object and then have List<Car> rather than using a dictionary like that. Commented Sep 28, 2020 at 14:47
  • You need class Vehicle with property PlateNumber, 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. Commented Sep 28, 2020 at 14:48
  • 1
    Your search clause immediately clears the screen after it finds a value so you'd never see the output. Commented Sep 28, 2020 at 15:13
  • Hi and welcome to StackOverflow. If any answer helped you to solve your problem here. You might consider to mark it as accepted Commented Sep 29, 2020 at 6:08

1 Answer 1

5

but they aren't saved

You stepped into an interesting trap of dictionary syntactic suggar. It has to do with the way you try to add values to the dictionary:

 newVehicle["Registration Number"] = regNumber; 

If the dictionary is empty, then the key "Registration Number" will be created and saved with regNumber as key value pair. On the other hand if this key already exists, then the value on this key will be simply overwritten!

This is why your problem arises and you get the impression that the values are not saved. You should always be able to find the last values that you have entered.

If you would have used the oldscool way of adding values:

newVehicle.Add("Registration Number", regNumber); 

this would have led to an exception that the key already exists. You would have immideately realised that this cannot work.

What should I do in order for my code to work?

You should addapt an object oriented approach and encapsulate all information that belong together in one class:

public class VehicleRepresentation { public string RegistrationNumber { get; set; } public string Brand { get; set; } public string Model { get; set; } public string Type { get; set; } public bool AutoPilot { get; set; } } 

Now you can use a List<VehicleRepresentation> allVehicles to collect all your cars.

VehicleRepresentation vehicle = new VehicleRepresentation(); Console.Write("Registration Number: "); vehicle.RegistrationNumber = Console.ReadLine(); Console.Write("Brand: "); vehicle.Brand = Console.ReadLine(); Console.Write("Model: "); vehicle.Model = Console.ReadLine(); Console.Write("Type (Sedan, Compact, Subcompact): "); vehicle.Type = Console.ReadLine(); Console.Write("Autopilot (Yes, No): "); vehicle.AutoPilot = Console.ReadLine() == "Yes"; Console.Clear(); allVehicles.Add(vehicle); 

Edit:

Addition by WSC:

typically you'd want to create an enum for vehicle.Type rather than a list of user entered strings. Getting that from user input might be a bit cumbersome in a console environment, however.

Search:

The searching can be implemented in different ways. If you want to keep your loop

foreach(var veh in allVehicles) { if(veh.RegistrationNumber == searchingForVehicle) { WriteLine("Value found."); // write here all the information you want to display. } } 

You can of course use also LINQ for filtering. But that you can figure out on your own I guess.

Sign up to request clarification or add additional context in comments.

1 Comment

To build on this, once you add that vehicle to a "List<VehicleRepresentation>" you can then search it with linq: var searchList = vehicleList.Where(x => x.RegistrationNumber.Contains(searchingForVehicle)); Obviously "Where" finding a list of all that match, if there would only ever be one then FirstOrDefault could be used in its place.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.