plamen27

Andrey and Billiard fixed

Oct 12th, 2016
1,198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _7.Andrey_and_billiard
  6. {
  7. class Customer
  8. {
  9. public string Name { get; set; }
  10. public Dictionary<string, double> ShopList { get; set; }
  11. public double Bill { get; set; }
  12.  
  13. }
  14. class Program
  15. {
  16. static void Main()
  17. {
  18.  
  19. Dictionary<string, double> menu = new Dictionary<string, double>();
  20. int numberOfItems = int.Parse(Console.ReadLine());
  21. for (int i = 0; i < numberOfItems; i++)
  22. {
  23. string[] input = Console.ReadLine().Split('-');
  24. if (!menu.ContainsKey(input[0]))
  25. {
  26. menu.Add(input[0], double.Parse(input[1]));
  27. }
  28. else
  29. {
  30. menu[input[0]] = double.Parse(input[1]);
  31. }
  32.  
  33. }
  34. List<Customer> allCustomers = new List<Customer>();
  35. while (true)
  36. {
  37. string info = Console.ReadLine();
  38. if (info == "end of clients")
  39. {
  40. break;
  41. }
  42. string[] clientInfo = info.Split('-', ',');
  43. string customerName = clientInfo[0];
  44. string product = clientInfo[1];
  45. double quantity = double.Parse(clientInfo[2]);
  46.  
  47. if (!menu.ContainsKey(product)) { continue; }
  48.  
  49. Customer client = new Customer();
  50. client.ShopList = new Dictionary<string, double>();
  51. client.Name = clientInfo[0];
  52. client.ShopList.Add(product, quantity);
  53.  
  54. if (allCustomers.Any(c => c.Name == customerName))
  55. {
  56. Customer existingCustomer = allCustomers.First(c => c.Name == customerName);
  57. if (existingCustomer.ShopList.ContainsKey(product))
  58. {
  59. existingCustomer.ShopList[product] += quantity;
  60. }
  61. else
  62. {
  63. existingCustomer.ShopList[product] = quantity;
  64. }
  65. }
  66. else
  67. {
  68. allCustomers.Add(client);
  69. }
  70. }
  71. /// Това го правим само за да направим bill-a:
  72. foreach (var customer in allCustomers)
  73. {
  74. foreach (var item in customer.ShopList)
  75. {
  76.  
  77. foreach (var product in menu)
  78. {
  79. if (item.Key == product.Key) { customer.Bill += item.Value * product.Value; }
  80. }
  81. }
  82. }
  83. var ordered = allCustomers
  84. .OrderBy(x => x.Name)
  85. .ThenBy(x => x.Bill)
  86. .ToList();
  87. foreach (var customer in ordered)
  88. {
  89. Console.WriteLine(customer.Name);
  90. foreach (KeyValuePair<string, double> item in customer.ShopList)
  91. {
  92. Console.WriteLine($"-- {item.Key} - {item.Value}");
  93. }
  94. Console.WriteLine("Bill: {0:f2}", customer.Bill);
  95.  
  96. }
  97. Console.WriteLine("Total bill: {0:F2}", allCustomers.Sum(c => c.Bill));
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment