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 Wintellect.PowerCollections;
- namespace Homework
- {
- public class Program
- {
- static Dictionary<string, OrderedBag<Order>> ordersByConsumer = new Dictionary<string, OrderedBag<Order>>();
- static SortedDictionary<decimal, OrderedBag<Order>> ordersByPrice = new SortedDictionary<decimal, OrderedBag<Order>>();
- static StringBuilder builder = new StringBuilder();
- public static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- for (int i = 0; i < n; i++)
- {
- string input = Console.ReadLine();
- int index = input.IndexOf(' ');
- string command = input.Substring(0, index);
- string[] commandLine = input.Substring(index + 1).Split(';').ToArray();
- switch (command)
- {
- case "AddOrder":
- AddOrder(commandLine);
- break;
- case "DeleteOrders":
- DeleteOrders(commandLine);
- break;
- case "FindOrdersByPriceRange":
- FindOrdersByPriceRange(commandLine);
- break;
- case "FindOrdersByConsumer":
- FindOrdersByConsumer(commandLine);
- break;
- }
- }
- Console.WriteLine(builder.ToString().TrimEnd());
- }
- static void AddOrder(string[] commandLine)
- {
- var order = new Order(commandLine[0], decimal.Parse(commandLine[1]), commandLine[2]);
- if (!ordersByConsumer.ContainsKey(commandLine[2]))
- {
- ordersByConsumer[commandLine[2]] = new OrderedBag<Order>();
- }
- ordersByConsumer[commandLine[2]].Add(order);
- if (!ordersByPrice.ContainsKey(decimal.Parse(commandLine[1])))
- {
- ordersByPrice[decimal.Parse(commandLine[1])] = new OrderedBag<Order>();
- }
- ordersByPrice[decimal.Parse(commandLine[1])].Add(order);
- builder.AppendLine($"Order added");
- }
- static void DeleteOrders(string[] commandLine)
- {
- if (!ordersByConsumer.ContainsKey(commandLine[0]))
- {
- builder.AppendLine("No orders found");
- return;
- }
- int count = 0;
- foreach (var item in ordersByConsumer[commandLine[0]])
- {
- ordersByPrice[item.Price].Remove(item);
- count++;
- }
- if (count == 0)
- {
- builder.AppendLine("No orders found");
- }
- else
- {
- ordersByConsumer[commandLine[0]].Clear();
- builder.AppendLine($"{count} orders deleted");
- }
- }
- static void FindOrdersByPriceRange(string[] commandLine)
- {
- decimal min = decimal.Parse(commandLine[0]);
- decimal max = decimal.Parse(commandLine[1]);
- var dict = ordersByPrice.Where(x => x.Key >= min && x.Key <= max);
- var bag = new OrderedBag<Order>();
- foreach (var item in dict)
- {
- foreach (var item1 in item.Value)
- {
- bag.Add(item1);
- }
- }
- if (!bag.Any())
- {
- builder.AppendLine("No orders found");
- }
- else
- {
- builder.AppendLine(string.Join("\r\n", bag));
- }
- }
- static void FindOrdersByConsumer(string[] commandLine)
- {
- if (!ordersByConsumer.ContainsKey(commandLine[0]))
- {
- builder.AppendLine("No orders found");
- return;
- }
- if (!ordersByConsumer[commandLine[0]].Any())
- {
- builder.AppendLine("No orders found");
- return;
- }
- builder.AppendLine(string.Join("\r\n", ordersByConsumer[commandLine[0]]));
- }
- class Order : IComparable<Order>
- {
- public string Name { get; set; }
- public decimal Price { get; set; }
- public string Consumer { get; set; }
- public Order(string name, decimal price, string consumer)
- {
- Name = name;
- Price = price;
- Consumer = consumer;
- }
- public int CompareTo(Order other)
- {
- int comparison = this.Name.CompareTo(other.Name);
- return comparison;
- }
- public override string ToString()
- {
- return string.Format("{{{0};{1};{2:0.00}}}", this.Name, this.Consumer, this.Price);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement