Teo_Yanchev

Dictionaries - 04. Orders - C# Fundamentals

Oct 4th, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace _04._Orders
  5. {
  6. class Orders
  7. {
  8. static void Main()
  9. {
  10. Dictionary<string, List<double>> products = new Dictionary<string, List<double>>();
  11.  
  12. string command = Console.ReadLine();
  13. while (command != "buy")
  14. {
  15. string[] currentProduct = command.Split();
  16. string typeProduct = currentProduct[0];
  17. double price = double.Parse(currentProduct[1]);
  18. double quantity = double.Parse(currentProduct[2]);
  19.  
  20. if (!products.ContainsKey(typeProduct))
  21. {
  22. products[typeProduct] = new List<double>();
  23.  
  24. products[typeProduct].Add(price);
  25. products[typeProduct].Add(quantity);
  26. }
  27. else if (products.ContainsKey(typeProduct))
  28. {
  29. products[typeProduct][1] += quantity;
  30. if (products[typeProduct][0] != price)
  31. {
  32. products[typeProduct].RemoveAt(0);
  33. products[typeProduct].Insert(0, price);
  34. }
  35. }
  36.  
  37. command = Console.ReadLine();
  38. }
  39.  
  40. foreach (var totalPrice in products)
  41. {
  42. List<double> priceNquantity = totalPrice.Value;
  43.  
  44. double price = priceNquantity[0];
  45. double quantity = priceNquantity[0];
  46.  
  47. double total = price * quantity;
  48.  
  49. Console.WriteLine($"{totalPrice.Key} -> {total:f2}");
  50. }
  51. }
  52. }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment