Advertisement
MilenaPetkanova

Andrey And Billiard

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