Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _07_Andrey_and_Billiard
  6. {
  7. public class AndreyАndBilliard
  8. {
  9. public static void Main()
  10. {
  11. //Read all shop items
  12. int entities = int.Parse(Console.ReadLine());
  13. var shop = new Dictionary<string, decimal>();
  14. for (int entitiesCntIndex = 1; entitiesCntIndex <= entities; entitiesCntIndex++)
  15. {
  16. var tokens = Console.ReadLine().Split('-').ToArray();
  17. string product = tokens[0];
  18. decimal price = decimal.Parse(tokens[1]);
  19.  
  20. if (shop.ContainsKey(product))
  21. {
  22. shop[product] = price;
  23. }
  24. else
  25. {
  26. shop.Add(product, price);
  27. }
  28. }
  29.  
  30. //Read all customers
  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. //Get customer info
  38. string customerName = tokens[0];
  39. string customerProduct = tokens[1];
  40. int customerQuantity = int.Parse(tokens[2]);
  41.  
  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 the user has been made a purchase already
  51. if (customers.Any(x=>x.Name == customerName))
  52. {
  53. var currentCustomer = customers.First(x => x.Name == customerName);
  54.  
  55. if (currentCustomer.ShopList.ContainsKey(customerProduct))
  56. {
  57. //If the user re-purchase an item
  58. currentCustomer.ShopList[customerProduct] += customerQuantity;
  59. currentCustomer.Bill += shop[customerProduct] * customerQuantity;
  60. }
  61. else
  62. {
  63. //If the user make a new order
  64. currentCustomer.ShopList[customerProduct] = customerQuantity;
  65. currentCustomer.Bill += shop[customerProduct] * customerQuantity;
  66. }
  67. }
  68. else
  69. {
  70. customers.Add(customer);
  71. }
  72. }
  73.  
  74. line = Console.ReadLine();
  75. }
  76.  
  77. //Print all customers and total bill
  78. foreach (var customer in customers.OrderBy(x => x.Name))
  79. {
  80. Console.WriteLine($"{customer.Name}");
  81. foreach (var shoplist in customer.ShopList)
  82. {
  83. var product = shoplist.Key;
  84. var quantitiy = shoplist.Value;
  85. Console.WriteLine($"-- {product} - {quantitiy}");
  86. }
  87. Console.WriteLine($"Bill: {customer.Bill:f2}");
  88. }
  89. Console.WriteLine($"Total bill: {customers.Sum(x=>x.Bill):f2}");
  90. }
  91.  
  92. class Customer
  93. {
  94. public Customer(string name, Dictionary<string, int> shopList, decimal bill)
  95. {
  96. Name = name;
  97. ShopList = shopList;
  98. Bill = bill;
  99. }
  100.  
  101. public string Name { get; set; }
  102.  
  103. public Dictionary<string, int> ShopList { get; set; }
  104.  
  105. public decimal Bill { get; set; }
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement