Im facing a problem with searching for a product concept, i have succeded with making it possible to create a product, and giving a warning message if the user tried to create a product with the same articleId.
But if i create more than one product, and search for a specifc one im getiing the information of all the created product in the dictionary.
Here im declaring a dictionary with both type int and string
Dictionary<int, string> products = new Dictionary<int, string>();
bool applicationRunning = true;
Console.CursorVisible = false;
and here is the menu
do
{
Console.WriteLine("Välkommen till FreakyFashion, välj ett alternativ nedan!");
Console.WriteLine("");
Console.WriteLine("(1) Ny produkt");
Console.WriteLine("(2) Sök produkt");
Console.WriteLine("(3) Avsluta");
ConsoleKeyInfo userInput;
bool invalidSelection = true;
do
{
userInput = Console.ReadKey(true);
invalidSelection = !(
userInput.Key == ConsoleKey.D1 ||
userInput.Key == ConsoleKey.NumPad1 ||
userInput.Key == ConsoleKey.D2 ||
userInput.Key == ConsoleKey.NumPad2 ||
userInput.Key == ConsoleKey.D3 ||
userInput.Key == ConsoleKey.NumPad3
);
} while (invalidSelection);
Console.Clear();
Console.CursorVisible = true;
And as you could see here, this where the user writes down the information of the product that is going to be created
switch (userInput.Key)
{
case ConsoleKey.D1:
case ConsoleKey.NumPad1:
Console.Write("Artikelnummer: ");
int articleId = int.Parse(Console.ReadLine());
Console.Write("Namn: ");
string nameOfProduct = (Console.ReadLine());
Console.Clear();
Here im showing the user what he gave for a name for the product and what he gave for a number for articleId, and im also asking the user if the information is correct then press 'J' to save the information in dictionary, otherwise press 'N' to start over
Console.WriteLine(("Namn: ") nameOfProduct ("\n Artikelnummer: ") articleId "\n Stämmer detta? (J)a (N)ej");
//Console.ReadKey(true);
//products.Add(articleId, nameOfProduct);
ConsoleKey key = Console.ReadKey(true).Key;
if (key.Equals(ConsoleKey.J) && products.ContainsKey(articleId))
{
Console.WriteLine("Artikelnummer är redan registerad");
Thread.Sleep(2000);
break;
}
else if (key.Equals(ConsoleKey.N))
{
Console.Clear();
Thread.Sleep(1000);
break;
}
else if (key.Equals(ConsoleKey.J))
{
products.Add(articleId, nameOfProduct);
Console.WriteLine("Produkten Registerat");
Thread.Sleep(2000);
Console.Clear();
break;
}
break;
Here is when im facing the problem, if the user for example created three diffrent products with three diffrent names and id's, and the user searched for one specific product, the program for som reason givs the information of every singel product the user created even if the user did not search for it
case ConsoleKey.D2:
case ConsoleKey.NumPad2:
Console.Write("Ange produkt namn: ");
nameOfProduct = Console.ReadLine();
if (products.ContainsValue(nameOfProduct))
{
foreach (KeyValuePair<int, string> s in products)
Console.WriteLine("Namn: " s.Value "\n" "Artikelnummer: " s.Key);
Console.ReadKey(true);
}
break;
We can exit the program here
case ConsoleKey.D3:
case ConsoleKey.NumPad3:
applicationRunning = false;
break;
}
Console.Clear();
} while (applicationRunning);
CodePudding user response:
The reason is this block:
if (products.ContainsValue(nameOfProduct))
{
foreach (KeyValuePair<int, string> s in products)
Console.WriteLine("Namn: " s.Value "\n" "Artikelnummer: " s.Key);
Console.ReadKey(true);
}
If your condition is true then it goes to a foreach
loop and just writes all items from products
dictionary. To solve this you can add additional condition: products.Where(product => product.Value.Equals(nameOfProduct))
or rewrite full block like the following:
var searched = products.Where(product => product.Value.Equals(nameOfProduct)).ToList();
if(searched.Any())
{
foreach (KeyValuePair<int, string> s in searched)
Console.WriteLine("Namn: " s.Value "\n" "Artikelnummer: " s.Key);
Console.ReadKey(true);
}