Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Linq;
  5.  
  6. namespace _07_Andrey_and_Billiard
  7. {
  8. public class AndreyАndBilliard
  9. {
  10. public static void Main()
  11. {
  12. int entities = int.Parse(Console.ReadLine());
  13.  
  14. var shop = new Dictionary<string, decimal>();
  15. for (int entitiesCntIndex = 1; entitiesCntIndex <= entities; entitiesCntIndex++)
  16. {
  17. var tokens = Console.ReadLine().Split('-').ToArray();
  18. string product = tokens[0];
  19. decimal price = decimal.Parse(tokens[1]);
  20.  
  21. if (shop.ContainsKey(product))
  22. {
  23. shop[product] = price;
  24. }
  25. else
  26. {
  27. shop.Add(product, price);
  28. }
  29. }
  30.  
  31. string line = Console.ReadLine();
  32. List<Customer> customers = new List<Customer>();
  33. while (line != "end of clients")
  34. {
  35. var tokens = line.Split(new char[] { '-', ',' }).ToArray();
  36.  
  37. string customerName = tokens[0];
  38. string customerProduct = tokens[1];
  39. int customerQuantity = int.Parse(tokens[2]);
  40.  
  41. //YOU NEED THIS
  42. if (shop.ContainsKey(customerProduct))
  43. {
  44. var customerShopList = new Dictionary<string, int>();
  45. customerShopList.Add(customerProduct, customerQuantity);
  46. decimal customerBill = shop[customerProduct] * customerQuantity;
  47.  
  48. var customer = new Customer(customerName, customerShopList, customerBill);
  49.  
  50. if (customers.Any(x => x.Name == customerName))
  51. {
  52. var currentCustomer = customers.First(x => x.Name == customerName);
  53.  
  54. if (currentCustomer.ShopList.ContainsKey(customerProduct))
  55. {
  56. currentCustomer.ShopList[customerProduct] += customerQuantity;
  57. currentCustomer.Bill += shop[customerProduct] * customerQuantity;
  58. }
  59. else
  60. {
  61. currentCustomer.ShopList[customerProduct] = customerQuantity;
  62. currentCustomer.Bill += shop[customerProduct] * customerQuantity;
  63. }
  64. }
  65. else
  66. {
  67. customers.Add(customer);
  68. }
  69. }
  70.  
  71. line = Console.ReadLine();
  72. }
  73.  
  74. foreach (var customer in customers.OrderBy(x => x.Name))
  75. {
  76. Console.WriteLine($"{customer.Name}");
  77. foreach (var shoplist in customer.ShopList)
  78. {
  79. var product = shoplist.Key;
  80. var quantitiy = shoplist.Value;
  81. Console.WriteLine($"-- {product} - {quantitiy}");
  82. }
  83. Console.WriteLine($"Bill: {customer.Bill:f2}");
  84. }
  85. Console.WriteLine($"Total bill: {customers.Sum(x=>x.Bill):f2}");
  86. }
  87.  
  88. class Customer
  89. {
  90. public Customer(string name, Dictionary<string, int> shopList, decimal bill)
  91. {
  92. Name = name;
  93. ShopList = shopList;
  94. Bill = bill;
  95. }
  96.  
  97. public string Name { get; set; }
  98.  
  99. public Dictionary<string, int> ShopList { get; set; }
  100.  
  101. public decimal Bill { get; set; }
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement