Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Wintellect.PowerCollections;
  6.  
  7. namespace Homework
  8. {
  9. public class Program
  10. {
  11. static Dictionary<string, OrderedBag<Order>> ordersByConsumer = new Dictionary<string, OrderedBag<Order>>();
  12. static SortedDictionary<decimal, OrderedBag<Order>> ordersByPrice = new SortedDictionary<decimal, OrderedBag<Order>>();
  13.  
  14. static StringBuilder builder = new StringBuilder();
  15.  
  16. public static void Main()
  17. {
  18. int n = int.Parse(Console.ReadLine());
  19.  
  20. for (int i = 0; i < n; i++)
  21. {
  22. string input = Console.ReadLine();
  23. int index = input.IndexOf(' ');
  24. string command = input.Substring(0, index);
  25. string[] commandLine = input.Substring(index + 1).Split(';').ToArray();
  26.  
  27. switch (command)
  28. {
  29. case "AddOrder":
  30. AddOrder(commandLine);
  31. break;
  32. case "DeleteOrders":
  33. DeleteOrders(commandLine);
  34. break;
  35. case "FindOrdersByPriceRange":
  36. FindOrdersByPriceRange(commandLine);
  37. break;
  38. case "FindOrdersByConsumer":
  39. FindOrdersByConsumer(commandLine);
  40. break;
  41. }
  42. }
  43.  
  44. Console.WriteLine(builder.ToString().TrimEnd());
  45. }
  46.  
  47. static void AddOrder(string[] commandLine)
  48. {
  49. var order = new Order(commandLine[0], decimal.Parse(commandLine[1]), commandLine[2]);
  50.  
  51. if (!ordersByConsumer.ContainsKey(commandLine[2]))
  52. {
  53. ordersByConsumer[commandLine[2]] = new OrderedBag<Order>();
  54. }
  55.  
  56. ordersByConsumer[commandLine[2]].Add(order);
  57.  
  58. if (!ordersByPrice.ContainsKey(decimal.Parse(commandLine[1])))
  59. {
  60. ordersByPrice[decimal.Parse(commandLine[1])] = new OrderedBag<Order>();
  61. }
  62.  
  63. ordersByPrice[decimal.Parse(commandLine[1])].Add(order);
  64.  
  65.  
  66. builder.AppendLine($"Order added");
  67. }
  68.  
  69. static void DeleteOrders(string[] commandLine)
  70. {
  71. if (!ordersByConsumer.ContainsKey(commandLine[0]))
  72. {
  73. builder.AppendLine("No orders found");
  74. return;
  75. }
  76.  
  77. int count = 0;
  78.  
  79. foreach (var item in ordersByConsumer[commandLine[0]])
  80. {
  81. ordersByPrice[item.Price].Remove(item);
  82.  
  83. count++;
  84. }
  85.  
  86. if (count == 0)
  87. {
  88. builder.AppendLine("No orders found");
  89. }
  90. else
  91. {
  92. ordersByConsumer[commandLine[0]].Clear();
  93. builder.AppendLine($"{count} orders deleted");
  94. }
  95. }
  96.  
  97. static void FindOrdersByPriceRange(string[] commandLine)
  98. {
  99. decimal min = decimal.Parse(commandLine[0]);
  100. decimal max = decimal.Parse(commandLine[1]);
  101.  
  102.  
  103. var dict = ordersByPrice.Where(x => x.Key >= min && x.Key <= max);
  104.  
  105. var bag = new OrderedBag<Order>();
  106.  
  107. foreach (var item in dict)
  108. {
  109. foreach (var item1 in item.Value)
  110. {
  111. bag.Add(item1);
  112. }
  113. }
  114.  
  115. if (!bag.Any())
  116. {
  117. builder.AppendLine("No orders found");
  118. }
  119. else
  120. {
  121. builder.AppendLine(string.Join("\r\n", bag));
  122. }
  123. }
  124.  
  125. static void FindOrdersByConsumer(string[] commandLine)
  126. {
  127. if (!ordersByConsumer.ContainsKey(commandLine[0]))
  128. {
  129. builder.AppendLine("No orders found");
  130. return;
  131. }
  132.  
  133. if (!ordersByConsumer[commandLine[0]].Any())
  134. {
  135. builder.AppendLine("No orders found");
  136. return;
  137. }
  138.  
  139. builder.AppendLine(string.Join("\r\n", ordersByConsumer[commandLine[0]]));
  140.  
  141. }
  142.  
  143. class Order : IComparable<Order>
  144. {
  145. public string Name { get; set; }
  146. public decimal Price { get; set; }
  147. public string Consumer { get; set; }
  148.  
  149. public Order(string name, decimal price, string consumer)
  150. {
  151. Name = name;
  152. Price = price;
  153. Consumer = consumer;
  154. }
  155.  
  156. public int CompareTo(Order other)
  157. {
  158. int comparison = this.Name.CompareTo(other.Name);
  159.  
  160. return comparison;
  161. }
  162.  
  163. public override string ToString()
  164. {
  165. return string.Format("{{{0};{1};{2:0.00}}}", this.Name, this.Consumer, this.Price);
  166. }
  167. }
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement