I have to calculate the price of an order based on the input from the user using a Dictionary, but I can't seem to get it right.
- Chicken Strips - $3.50
- French Fries - $2.50
- Hamburger - $4.00
- Hotdog - $3.50
- Large Drink - $1.75
- Medium Drink - $1.50
- Milk Shake - $2.25
- Salad - $3.75
- Small Drink - $1.25
For example, if the input is 236, the price of the order should be $8.
Converting my LINQ query to double didn't work (my query is also definitely wrong). I don't really know any LINQ but as you can't really use a for loop for a dictionary as they aren't indexed, I don't really have any other choice.
Dictionary<int, double> items = new Dictionary<int, double>()
{
{ 1, 3.50 },
{ 2, 2.50 },
{ 3, 4.00 },
{ 4, 3.50 },
{ 5, 1.75 },
{ 6, 1.50 },
{ 7, 2.25 },
{ 8, 3.75 },
{ 9, 1.25 }
};
string order = Console.ReadLine();
double sum = 0;
for (int i = 0; i < order.Length; i++)
{
sum += Convert.ToDouble(items.Where(x => x.Key == int.Parse(order[i].ToString())).Select(x => x.Value));
}
Console.WriteLine(sum);
Console.ReadKey();
Well, the output should be the correct amount of dollars based on the input from the user.