Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Andrey_and_Billiard
  6. {
  7. public static void Main()
  8. {
  9. var entitiesAmount = int.Parse(Console.ReadLine());
  10.  
  11. var productsPrices = new Dictionary<string, double>();
  12. GetProductsAndPrices(entitiesAmount, productsPrices);
  13.  
  14. var line = Console.ReadLine();
  15. var customers = new List<Customer>();
  16.  
  17. while (!line.Equals("end of clients"))
  18. {
  19.  
  20. var clientData = line
  21. .Split("- ,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  22.  
  23. var clientName = clientData[0];
  24. var productName = clientData[1];
  25.  
  26. if (productsPrices.ContainsKey(productName))
  27. {
  28. var productQuantity = int.Parse(clientData[2]);
  29.  
  30. if (customers.Any(c => c.Name == clientName))
  31. {
  32. var customer = customers.First(c => c.Name == clientName);
  33.  
  34. if (!customers.First(c => c.Name == clientName).ShopList.ContainsKey(productName))
  35. {
  36. customer.ShopList.Add(productName, productQuantity);
  37. customer.Bill += productQuantity * productsPrices[productName];
  38.  
  39. }
  40. else
  41. {
  42. customer.ShopList[productName] += productQuantity;
  43.  
  44. customer.Bill += productQuantity * productsPrices[productName];
  45. }
  46.  
  47. }
  48.  
  49. else
  50. {
  51. var currentCustomer = new Customer();
  52.  
  53. currentCustomer.Name = clientName;
  54. currentCustomer.ShopList = new Dictionary<string, long>();
  55. currentCustomer.ShopList.Add(productName, productQuantity);
  56. currentCustomer.Bill += productQuantity * productsPrices[productName];
  57.  
  58. customers.Add(currentCustomer);
  59. }
  60. }
  61. line = Console.ReadLine();
  62. }
  63.  
  64. double totalBill = 0;
  65.  
  66. foreach (var customer in customers.OrderBy(x => x.Name))
  67. {
  68. Console.WriteLine(customer.Name);
  69.  
  70. foreach (var item in customer.ShopList)
  71. {
  72. Console.WriteLine($"-- {item.Key} - {item.Value}");
  73. }
  74.  
  75. Console.WriteLine($"Bill: {customer.Bill:F2}");
  76.  
  77. totalBill += customer.Bill;
  78. }
  79.  
  80. Console.WriteLine($"Total bill: {totalBill:F2}");
  81. }
  82.  
  83. private static void GetProductsAndPrices(int entitiesAmount, Dictionary<string, double> productsPrices)
  84. {
  85. for (int i = 0; i < entitiesAmount; i++)
  86. {
  87. var productData = Console.ReadLine()
  88. .Split('-');
  89.  
  90. var productName = productData[0];
  91. var productPrice = double.Parse(productData[1]);
  92.  
  93. if (!productsPrices.ContainsKey(productName))
  94. {
  95. productsPrices.Add(productName, productPrice);
  96. }
  97. else
  98. {
  99. productsPrices[productName] = productPrice;
  100. }
  101. }
  102. }
  103. }
  104.  
  105. public class Customer
  106. {
  107. public string Name { get; set; }
  108.  
  109. public Dictionary<string, long> ShopList { get; set; }
  110.  
  111. public double Bill { get; set; }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement