Advertisement
bullit3189

Orders - Dictionaries

Jan 23rd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. public class Program
  6. {
  7. public static void Main()
  8. {
  9. var priceProduct = new Dictionary<string, decimal>();
  10. var quantityProduct = new Dictionary<string, int>();
  11.  
  12. while (true)
  13. {
  14. string[] tokens = Console.ReadLine().Split();
  15.  
  16. string product = tokens[0];
  17.  
  18. if (product == "buy")
  19. {
  20. break;
  21. }
  22.  
  23. decimal price = decimal.Parse(tokens[1]);
  24. int quantity = int.Parse(tokens[2]);
  25.  
  26. if (priceProduct.ContainsKey(product)==false || quantityProduct.ContainsKey(product)==false)
  27. {
  28. priceProduct[product]=price;
  29. quantityProduct[product]=quantity;
  30. }
  31. else
  32. {
  33. priceProduct[product]=price;
  34. quantityProduct[product]+=quantity;
  35. }
  36. }
  37.  
  38. foreach (var kvp in quantityProduct)
  39. {
  40. string product = kvp.Key;
  41. int quantity = kvp.Value;
  42. decimal price = priceProduct[product];
  43. decimal totalPrice = price*quantity;
  44.  
  45. Console.WriteLine("{0} -> {1}",product, totalPrice);
  46. }
  47.  
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement