Advertisement
Guest User

Untitled

a guest
Oct 12th, 2016
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 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. using System.Globalization;
  7. using System.Threading;
  8.  
  9. namespace AndreyAndBilliard
  10. {
  11. class Program
  12. {
  13. private static void AddProducts(Dictionary<string, decimal> products, int number)
  14. {
  15. for (int i = 0; i < number; i++)
  16. {
  17. string[] input = Console.ReadLine().Split('-')
  18. .ToArray();
  19. string productName = input[0];
  20. decimal price = decimal.Parse(input[1]);
  21. if (products.ContainsKey(productName))
  22. {
  23. products[productName] = price;
  24. }
  25. else
  26. {
  27. products.Add(productName, price);
  28. }
  29.  
  30. }
  31. }
  32.  
  33. private static void AddCustomers(List<Customer> customers, Dictionary<string, decimal> products)
  34. {
  35. string input = Console.ReadLine();
  36. int customerIndexCounter = 0;
  37. while (input != "end of clients")
  38. {
  39. string[] custInfo = input.Split(new char[] { '-', ',' }, StringSplitOptions.RemoveEmptyEntries)
  40. .ToArray();
  41. string name = custInfo[0];
  42. string product = custInfo[1];
  43. int quantity = int.Parse(custInfo[2]);
  44. if (products.ContainsKey(product))
  45. {
  46. decimal productPrice = products[product];
  47. if (customers.Select(x => x.Name).Contains(name))
  48. {
  49. foreach (var customer in customers.Where(x => x.Name == name))
  50. {
  51. if (customer.ShopList.ContainsKey(product))
  52. {
  53. customer.ShopList[product] += quantity;
  54.  
  55. }else
  56. {
  57. customer.ShopList.Add(product, quantity);
  58. customer.Bill += quantity * productPrice;
  59. }
  60. }
  61. }
  62. else
  63. {
  64. customers.Add(new Customer
  65. {
  66. Name = name,
  67. ShopList = new Dictionary<string, int>()
  68. });
  69. customers[customerIndexCounter].ShopList.Add(product, quantity);
  70. customers[customerIndexCounter].Bill += quantity * productPrice;
  71. customerIndexCounter++;
  72. }
  73.  
  74. }
  75. input = Console.ReadLine();
  76. }
  77. }
  78.  
  79. private static void PrintCustomers(List<Customer> customers)
  80. {
  81. decimal totalBill = 0;
  82. foreach (var customer in customers.OrderBy(x => x.Name))
  83. {
  84. Console.WriteLine("{0}", customer.Name);
  85. foreach (var item in customer.ShopList)
  86. {
  87. Console.WriteLine("-- {0} - {1}", item.Key, item.Value);
  88. }
  89. Console.WriteLine("Bill: {0:F2}", customer.Bill);
  90. totalBill += customer.Bill;
  91. }
  92. Console.WriteLine("Total bill: {0:F2}", totalBill);
  93. }
  94. static void Main(string[] args)
  95. {
  96. Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
  97. int numOfProducts = int.Parse(Console.ReadLine());
  98. var products = new Dictionary<string, decimal>();
  99. List<Customer> customers = new List<Customer>();
  100. AddProducts(products, numOfProducts);
  101. AddCustomers(customers, products);
  102. PrintCustomers(customers);
  103. }
  104.  
  105. class Customer
  106. {
  107. public string Name { get; set; }
  108. public Dictionary<string, int> ShopList { get; set; }
  109. public decimal Bill { get; set; }
  110. }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement