Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Wintellect.PowerCollections;
- namespace OrdersSystem
- {
- // Memory Limit cos of stringbuilder
- class Order : IComparable<Order>
- {
- public Order(string name, double price, string consumer)
- {
- Name = name;
- Price = price;
- Consumer = consumer;
- ToStringProp = string.Format("{0}{1};{2};{3:F2}{4}", '{', name, consumer, price, '}');
- }
- public string Name { get; set; }
- public double Price { get; set; }
- public string Consumer { get; set; }
- public string ToStringProp { get; set; }
- public int CompareTo(Order other)
- {
- return this.ToStringProp.CompareTo(other.ToStringProp);
- }
- public override string ToString()
- {
- return ToStringProp;
- }
- }
- class Program
- {
- private static MultiDictionary<string, Order> consumers = new MultiDictionary<string, Order>(true);
- private static OrderedMultiDictionary<double, Order> prices = new OrderedMultiDictionary<double, Order>(true);
- private const int AddOrder = 9;
- private const int DeleteOrders = 13;
- private const int FindOrdersByPriceRange = 23;
- private const int FindOrdersByConsumer = 21;
- static StringBuilder result = new StringBuilder();
- static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- for (int i = 0; i < n; i++)
- {
- string line = Console.ReadLine();
- int index = 8;
- while (line[index] != ' ')
- {
- index++;
- }
- index++;
- switch (index)
- {
- case AddOrder:
- {
- // name;price;consumer
- string[] currentParams = line.Substring(index).Split(';');
- double price = double.Parse(currentParams[1]);
- Order currentOrder = new Order(currentParams[0], price, currentParams[2]);
- consumers.Add(currentParams[2], currentOrder);
- prices.Add(price, currentOrder);
- Console.WriteLine("Order added");
- }
- break;
- case DeleteOrders:
- {
- string consumer = line.Substring(index);
- if (consumers.ContainsKey(consumer))
- {
- var toDelete = consumers[consumer];
- Console.WriteLine("{0} orders deleted", toDelete.Count);
- foreach (var order in toDelete)
- {
- prices.Remove(order.Price, order);
- }
- consumers.Remove(consumer);
- }
- else
- {
- Console.WriteLine("No orders found");
- }
- }
- break;
- case FindOrdersByPriceRange:
- {
- string[] currentParams = line.Substring(index).Split(';');
- double fromPrice = double.Parse(currentParams[0]);
- double toPrice = double.Parse(currentParams[1]);
- var orders = prices.Range(fromPrice, true, toPrice, true);
- if (orders.Values.Count > 0)
- {
- Console.WriteLine(string.Join(Environment.NewLine, orders.Values.OrderBy(o => o)));
- }
- else
- {
- Console.WriteLine("No orders found");
- }
- }
- break;
- case FindOrdersByConsumer:
- {
- string consumer = line.Substring(index);
- if (consumers.ContainsKey(consumer))
- {
- var orders = consumers[consumer];
- Console.WriteLine(string.Join(Environment.NewLine, orders.OrderBy(o => o)));
- }
- else
- {
- Console.WriteLine("No orders found");
- }
- }
- break;
- }
- }
- //Console.Write(result);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement