Advertisement
desislava_topuzakova

Untitled

Apr 25th, 2020
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ITCareer_OOP_Exam_1
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. //до получаване на Stop shopping -> четем стринг
  12.  
  13. string input = Console.ReadLine();
  14. Dictionary<string, double> products = new Dictionary<string, double>();
  15. double totalSum = 0;
  16.  
  17. while(input != "Stop shopping")
  18. {
  19. //"milk-1.2" -> ["milk", "1.20"]
  20. string productName = input.Split("-")[0];
  21. double productPrice = double.Parse(input.Split("-")[1]);
  22. totalSum += productPrice;
  23.  
  24. //1. да имаме продукта
  25. if (products.ContainsKey(productName))
  26. {
  27. products[productName] = products[productName] + productPrice;
  28. }
  29. //2. да го нямаме
  30. else
  31. {
  32. products.Add(productName, productPrice);
  33. }
  34.  
  35. input = Console.ReadLine();
  36. }
  37.  
  38.  
  39. var sorted = products.OrderBy(pair => pair.Value);
  40.  
  41. foreach(var pair in sorted)
  42. {
  43. Console.WriteLine($"{pair.Key} -> {pair.Value:F2}");
  44. }
  45.  
  46. Console.WriteLine($"Total sum: {totalSum:F2}");
  47.  
  48.  
  49.  
  50.  
  51.  
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement