MilenaPetkanova

AndreyAndBilliard100/100

Nov 3rd, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Customer
  6. {
  7. public string Name { get; set; }
  8. public Dictionary<string, int> ShopList { get; set; }
  9. public decimal Bill { get; set; }
  10. }
  11.  
  12. class AndreyAndBilliard
  13. {
  14. static void Main()
  15. {
  16. var shop = new Dictionary<string, decimal>();
  17.  
  18. int entitiesCount = int.Parse(Console.ReadLine());
  19.  
  20. for (int i = 0; i < entitiesCount; i++)
  21. {
  22. var newEntity = Console.ReadLine().Split('-').ToArray();
  23. string entityName = newEntity[0];
  24. decimal entityPrise = decimal.Parse(newEntity[1]);
  25.  
  26. if (!shop.ContainsKey(entityName))
  27. {
  28. shop.Add(entityName, entityPrise);
  29. }
  30. else
  31. {
  32. shop[entityName] = entityPrise;
  33. }
  34. }
  35.  
  36. List<Customer> allCustomers = new List<Customer>();
  37.  
  38. string order = Console.ReadLine();
  39. while (order != "end of clients")
  40. {
  41. string[] orderArgs = order.Split(new char[] { '-', ',' }, StringSplitOptions.RemoveEmptyEntries);
  42.  
  43. string product = orderArgs[1];
  44. if (shop.ContainsKey(product))
  45. {
  46. string customerName = orderArgs[0];
  47. int quantity = int.Parse(orderArgs[2]);
  48. decimal spent = decimal.Parse(orderArgs[2]) * shop[orderArgs[1]];
  49.  
  50. Customer currentCustomer = new Customer();
  51. currentCustomer.Name = customerName;
  52. currentCustomer.ShopList = new Dictionary<string, int>();
  53. currentCustomer.ShopList.Add(product, quantity);
  54. currentCustomer.Bill = spent;
  55.  
  56. if (allCustomers.Any(x => x.Name == currentCustomer.Name))
  57. {
  58. Customer existingCustomer = allCustomers
  59. .First(x => x.Name == currentCustomer.Name);
  60.  
  61. if (existingCustomer.ShopList.ContainsKey(product))
  62. {
  63. existingCustomer.ShopList[product] += quantity;
  64. existingCustomer.Bill += spent;
  65. }
  66. else
  67. {
  68. existingCustomer.ShopList[product] = quantity;
  69. existingCustomer.Bill += spent;
  70. }
  71. }
  72. else
  73. {
  74. allCustomers.Add(currentCustomer);
  75. }
  76. }
  77.  
  78. order = Console.ReadLine();
  79. }
  80.  
  81. decimal totalBill = 0;
  82. foreach (var customer in allCustomers.OrderBy(x => x.Name))
  83. {
  84. Console.WriteLine(customer.Name);
  85. foreach (var product in customer.ShopList)
  86. {
  87. Console.WriteLine("-- {0} - {1}",
  88. product.Key, product.Value);
  89. }
  90. Console.WriteLine("Bill: {0:f2}", customer.Bill);
  91. totalBill += customer.Bill;
  92. }
  93. Console.WriteLine("Total bill: {0:f2}", totalBill);
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment