Advertisement
loter

Andrey and Billiard2

Aug 9th, 2018
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Last_Exe
  6. {
  7. class Client
  8. {
  9. public string Name { get; set; }
  10. public Dictionary<string, int> Order { get; set; }
  11. public decimal Bill { get; set; }
  12. }
  13.  
  14. class Program
  15. {
  16. static void Main(string[] args)
  17. {
  18. int n = int.Parse(Console.ReadLine());
  19. Dictionary<string, decimal> priceList = new Dictionary<string, decimal>(); // всички продукти и цени
  20. List<Client> clients = new List<Client>(); // всички клиенти + техните поръчки и сметка
  21.  
  22. for (int i = 0; i < n; i++)
  23. {
  24. string[] beverige = Console.ReadLine().Split('-').ToArray();
  25. if (!priceList.ContainsKey(beverige[0]))
  26. {
  27. priceList.Add(beverige[0], decimal.Parse(beverige[1]));
  28. }
  29. priceList[beverige[0]] = decimal.Parse(beverige[1]);
  30. }
  31.  
  32. string[] currentOrder = Console.ReadLine().Split(new char[] { '-', ',', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  33. while (currentOrder[0] != "end")
  34. {
  35. string name = currentOrder[0];
  36. string product = currentOrder[1];
  37. int quantity = int.Parse(currentOrder[2]);
  38.  
  39. Dictionary<string, int> current = new Dictionary<string, int>(); // текуща поръчка - продукт и брой
  40.  
  41. if (priceList.ContainsKey(product)) // проверка дали продуктът съществува
  42. {
  43. var clientIneed = clients.FirstOrDefault(x => x.Name == name); // проверка дали клиентът съществува
  44.  
  45. if (clientIneed == null)
  46. {
  47. var client = new Client();
  48. client.Name = name;
  49. current.Add(product, quantity);
  50. client.Order = current;
  51.  
  52. foreach (var item in priceList.Where(x => x.Key == product))
  53. {
  54. client.Bill += quantity * item.Value;
  55. }
  56. clients.Add(client);
  57. }
  58.  
  59. else
  60. {
  61. var productIneedKVP = clientIneed.Order.FirstOrDefault(x => x.Key == product); // проверка дали клиентът е поръчвал от същия продукт
  62. var productIneed = productIneedKVP.Key;
  63.  
  64. if (productIneed == null)
  65. {
  66. clientIneed.Order.Add(product, quantity);
  67. foreach (var item in priceList.Where(x => x.Key == product))
  68. {
  69. clientIneed.Bill += quantity * item.Value;
  70. }
  71. }
  72.  
  73. else
  74. {
  75. clientIneed.Order[productIneed] += quantity;
  76. foreach (var item in priceList.Where(x => x.Key == product))
  77. {
  78. clientIneed.Bill += quantity * item.Value;
  79. }
  80. }
  81. }
  82. }
  83. currentOrder = Console.ReadLine().Split(new char[] { '-', ',', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  84. }
  85.  
  86. decimal totalBill = 0;
  87. foreach (var item in clients.OrderBy(x => x.Name))
  88. {
  89. Console.WriteLine($"{item.Name}");
  90.  
  91. foreach (var itemm in item.Order)
  92. {
  93. Console.WriteLine($"-- {itemm.Key} - { itemm.Value}");
  94. }
  95.  
  96. Console.WriteLine($"Bill: {item.Bill:f2}");
  97. totalBill += item.Bill;
  98. }
  99. Console.WriteLine($"Total bill: {totalBill:f2}");
  100. }
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement