Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.03 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 Wintellect.PowerCollections;
  7.  
  8. namespace OrdersSystem
  9. {
  10.     // Memory Limit cos of stringbuilder
  11.  
  12.     class Order : IComparable<Order>
  13.     {
  14.         public Order(string name, double price, string consumer)
  15.         {
  16.             Name = name;
  17.             Price = price;
  18.             Consumer = consumer;
  19.             ToStringProp = string.Format("{0}{1};{2};{3:F2}{4}", '{', name, consumer, price, '}');
  20.         }
  21.  
  22.         public string Name { get; set; }
  23.         public double Price { get; set; }
  24.         public string Consumer { get; set; }
  25.         public string ToStringProp { get; set; }
  26.  
  27.         public int CompareTo(Order other)
  28.         {
  29.             return this.ToStringProp.CompareTo(other.ToStringProp);
  30.         }
  31.  
  32.         public override string ToString()
  33.         {
  34.             return ToStringProp;
  35.         }
  36.     }
  37.     class Program
  38.     {
  39.         private static MultiDictionary<string, Order> consumers = new MultiDictionary<string, Order>(true);
  40.  
  41.         private static OrderedMultiDictionary<double, Order> prices = new OrderedMultiDictionary<double, Order>(true);
  42.         private const int AddOrder = 9;
  43.         private const int DeleteOrders = 13;
  44.         private const int FindOrdersByPriceRange = 23;
  45.         private const int FindOrdersByConsumer = 21;
  46.         static StringBuilder result = new StringBuilder();
  47.         static void Main()
  48.         {
  49.             int n = int.Parse(Console.ReadLine());
  50.             for (int i = 0; i < n; i++)
  51.             {
  52.                 string line = Console.ReadLine();
  53.                 int index = 8;
  54.                 while (line[index] != ' ')
  55.                 {
  56.                     index++;
  57.                 }
  58.                 index++;
  59.                 switch (index)
  60.                 {
  61.                        
  62.                     case AddOrder:
  63.                         {
  64.                             // name;price;consumer
  65.                             string[] currentParams = line.Substring(index).Split(';');
  66.                             double price = double.Parse(currentParams[1]);
  67.                             Order currentOrder = new Order(currentParams[0], price, currentParams[2]);
  68.  
  69.                             consumers.Add(currentParams[2], currentOrder);
  70.                             prices.Add(price, currentOrder);
  71.                             Console.WriteLine("Order added");
  72.                         }
  73.                         break;
  74.                     case DeleteOrders:
  75.                         {
  76.                             string consumer = line.Substring(index);
  77.                             if (consumers.ContainsKey(consumer))
  78.                             {
  79.                                 var toDelete = consumers[consumer];
  80.  
  81.                                 Console.WriteLine("{0} orders deleted", toDelete.Count);
  82.  
  83.                                 foreach (var order in toDelete)
  84.                                 {
  85.                                     prices.Remove(order.Price, order);
  86.                                 }
  87.  
  88.                                 consumers.Remove(consumer);
  89.                             }
  90.                             else
  91.                             {
  92.                                 Console.WriteLine("No orders found");
  93.                             }
  94.                         }
  95.                         break;
  96.                     case FindOrdersByPriceRange:
  97.                         {
  98.                             string[] currentParams = line.Substring(index).Split(';');
  99.                             double fromPrice = double.Parse(currentParams[0]);
  100.                             double toPrice = double.Parse(currentParams[1]);
  101.                             var orders = prices.Range(fromPrice, true, toPrice, true);
  102.                             if (orders.Values.Count > 0)
  103.                             {
  104.                                 Console.WriteLine(string.Join(Environment.NewLine, orders.Values.OrderBy(o => o)));
  105.                             }
  106.                             else
  107.                             {
  108.                                 Console.WriteLine("No orders found");
  109.                             }
  110.                         }
  111.                         break;
  112.                     case FindOrdersByConsumer:
  113.                         {
  114.                             string consumer = line.Substring(index);
  115.                             if (consumers.ContainsKey(consumer))
  116.                             {
  117.                                 var orders = consumers[consumer];
  118.                                 Console.WriteLine(string.Join(Environment.NewLine, orders.OrderBy(o => o)));
  119.                             }
  120.                             else
  121.                             {
  122.                                 Console.WriteLine("No orders found");
  123.                             }
  124.                         }
  125.                         break;
  126.                 }
  127.             }
  128.             //Console.Write(result);
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement