Advertisement
Ivakis

AndreyAndBillard

Oct 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace AndreyTest
  6. {
  7. class Customer
  8. {
  9. public string Name { get; set; }
  10. public Dictionary<string, int> ProductsAndQuantity { get; set; }
  11. public decimal Bill { get; set; }
  12. }
  13. class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. var goodsAndPrices = new Dictionary<string, decimal>();
  18.  
  19. fillWithGoods(goodsAndPrices);
  20.  
  21. List<Customer> customers = new List<Customer>();
  22.  
  23. string command = Console.ReadLine();
  24.  
  25. while (command != "end of clients")
  26. {
  27. var commandArgs = command.Split('-');
  28. var quantityAndProduct = commandArgs[1].Split(',');
  29.  
  30. var name = commandArgs[0];
  31. var product = quantityAndProduct[0];
  32. var quantity = int.Parse(quantityAndProduct[1]);
  33.  
  34. if (goodsAndPrices.ContainsKey(product))
  35. {
  36. bool customerIsPresent = false;
  37.  
  38.  
  39. Customer customer = new Customer();
  40. customer.Name = name;
  41. customer.ProductsAndQuantity = new Dictionary<string, int>();
  42. customer.ProductsAndQuantity.Add(product, quantity);
  43. customer.Bill = customer.Bill + goodsAndPrices[product] * quantity;
  44.  
  45. foreach (var c in customers)
  46. {
  47. if (c.Name == customer.Name)
  48. {
  49. customerIsPresent = true;
  50. if (c.ProductsAndQuantity.ContainsKey(product))
  51. {
  52. c.ProductsAndQuantity[product] += quantity;
  53. }
  54. else
  55. {
  56. c.ProductsAndQuantity.Add(product, quantity);
  57. }
  58. c.Bill += goodsAndPrices[product] * quantity;
  59. }
  60. }
  61.  
  62. if (!customerIsPresent)
  63. {
  64. customers.Add(customer);
  65. }
  66. }
  67. command = Console.ReadLine();
  68. }
  69.  
  70. printResult(customers);
  71. }
  72.  
  73. private static void printResult(List<Customer> customers)
  74. {
  75. decimal totalBill = 0.0M;
  76.  
  77. foreach (var customer in customers.OrderBy(c => c.Name))
  78. {
  79. Console.WriteLine(customer.Name);
  80. foreach (var product in customer.ProductsAndQuantity)
  81. {
  82. Console.WriteLine($"-- {product.Key} - {product.Value}");
  83. }
  84. Console.WriteLine($"Bill: {customer.Bill:F2}");
  85. totalBill += customer.Bill;
  86. }
  87.  
  88. Console.WriteLine($"Total bill: {totalBill:F2}");
  89. }
  90.  
  91. private static void fillWithGoods(Dictionary<string, decimal> goodsAndPrices)
  92. {
  93. int n = int.Parse(Console.ReadLine());
  94. while (n-- > 0)
  95. {
  96. var commandArgs = Console.ReadLine().Split('-');
  97. var product = commandArgs[0];
  98. var price = decimal.Parse(commandArgs[1]);
  99.  
  100. if (!goodsAndPrices.ContainsKey(product))
  101. {
  102. goodsAndPrices.Add(product, 0);
  103. }
  104.  
  105. goodsAndPrices[product] = price;
  106. }
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement