Advertisement
viraco4a

AndreyAndBilliard

Feb 28th, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace AndreyAndBilliard
  6. {
  7.  
  8. class Customer
  9. {
  10. public string Name { get; set; }
  11. public Dictionary<string, int> Products { get; set; }
  12.  
  13. public void AddProduct(Dictionary<string, int> productToAdd)
  14. {
  15. string productName = productToAdd.ElementAt(0).Key;
  16. int quantity = productToAdd.ElementAt(0).Value;
  17. if (!Products.ContainsKey(productName))
  18. {
  19. Products.Add(productName, quantity);
  20. }
  21. else
  22. {
  23. Products[productName] += quantity;
  24. }
  25. }
  26.  
  27. public decimal GetCustomerBill(Dictionary<string, decimal> priceList)
  28. {
  29. decimal bill = 0.0m;
  30. foreach (var product in Products)
  31. {
  32. bill += product.Value * priceList[product.Key];
  33. }
  34. return bill;
  35. }
  36. }
  37.  
  38. class Program
  39. {
  40. static void Main()
  41. {
  42. var priceList = GeneratePriceList(int.Parse(Console.ReadLine()));
  43. var customerList = new List<Customer>();
  44. AddClients(priceList, customerList);
  45. decimal totalBill = 0.0m;
  46. foreach (var customer in customerList.OrderBy(s => s.Name))
  47. {
  48. Console.WriteLine(customer.Name);
  49. foreach (var product in customer.Products)
  50. {
  51. Console.WriteLine($"-- {product.Key} - {product.Value}");
  52. };
  53. decimal customerBill = customer.GetCustomerBill(priceList);
  54. totalBill += customerBill;
  55. Console.WriteLine($"Bill: {customerBill:F2}");
  56. }
  57. Console.WriteLine($"Total bill: {totalBill:F2}");
  58. }
  59.  
  60. private static void AddClients(Dictionary<string, decimal> priceList, List<Customer> customerList)
  61. {
  62. string input = Console.ReadLine();
  63. while (input != "end of clients")
  64. {
  65. var splitted = ReadInput(input);
  66. if (priceList.ContainsKey(splitted[1]))
  67. {
  68. var newCustomer = ReadCustomer(splitted);
  69. if (!customerList.Any(s => s.Name == newCustomer.Name))
  70. {
  71. customerList.Add(newCustomer);
  72. }
  73. else
  74. {
  75. var productToAdd = newCustomer.Products;
  76. foreach (var customer in customerList)
  77. {
  78. if (customer.Name == newCustomer.Name)
  79. {
  80. customer.AddProduct(newCustomer.Products);
  81. break;
  82. }
  83. }
  84. }
  85. }
  86. input = Console.ReadLine();
  87. }
  88. }
  89.  
  90. private static Customer ReadCustomer(string[] splitted)
  91. {
  92. return new Customer
  93. {
  94. Name = splitted[0],
  95. Products = new Dictionary<string, int>() { { splitted[1], int.Parse(splitted[2]) } }
  96. };
  97. }
  98.  
  99. private static string[] ReadInput(string input)
  100. {
  101. return input
  102. .Split(new char[] { '-', ',' }, StringSplitOptions.RemoveEmptyEntries);
  103. }
  104.  
  105. private static Dictionary<string, decimal> GeneratePriceList(int n)
  106. {
  107. var priceList = new Dictionary<string, decimal>();
  108. for (int i = 0; i < n; i++)
  109. {
  110. var input = Console.ReadLine()
  111. .Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
  112. priceList[input[0]] = decimal.Parse(input[1]);
  113. }
  114. return priceList;
  115. }
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement