Advertisement
svetlai

Online Store

Nov 8th, 2015
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.67 KB | None | 0 0
  1. namespace OnlineStore
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.     using System.Threading;
  8.  
  9.     public class OnlineStoreSolution
  10.     {
  11.         private readonly static StringBuilder output = new StringBuilder();
  12.  
  13.         public static void Main()
  14.         {
  15.             Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
  16.  
  17.             int n = int.Parse(Console.ReadLine());
  18.             var store = new Store();
  19.  
  20.             for (int i = 0; i < n; i++)
  21.             {
  22.                 var commandLine = Console.ReadLine();
  23.                 var command = Command.Parse(commandLine);
  24.  
  25.                 ProcessCommand(command, store);
  26.             }
  27.  
  28.             Console.WriteLine(output.ToString().Trim());
  29.         }
  30.  
  31.         private static void ProcessCommand(Command command, Store store)
  32.         {
  33.             switch (command.Name)
  34.             {
  35.                 case "AddProduct":
  36.                     output.AppendLine(store.AddProduct(command.Params[0], decimal.Parse(command.Params[1]), command.Params[2]));
  37.                     break;
  38.                 case "DeleteProducts":
  39.                     if (command.Params.Length > 1)
  40.                     {
  41.                         output.AppendLine(store.DeleteProduct(command.Params[1], command.Params[0]));
  42.                     }
  43.                     else
  44.                     {
  45.                         output.AppendLine(store.DeleteProduct(command.Params[0]));
  46.                     }
  47.                     break;
  48.                 case "FindProductsByName":
  49.                     output.AppendLine(store.FindProductsByName(command.Params[0]));
  50.                     break;
  51.                 case "FindProductsByPriceRange":
  52.                     output.AppendLine(store.FindProductsByPriceRange(decimal.Parse(command.Params[0]),
  53.                         decimal.Parse(command.Params[1])));
  54.                     break;
  55.                 case "FindProductsByProducer":
  56.                     output.AppendLine(store.FindProductsByProducer(command.Params[0]));
  57.                     break;
  58.             }
  59.         }
  60.     }
  61.  
  62.     public class Product //: IComparable
  63.     {
  64.         public Product(string name, decimal price, string producer)
  65.         {
  66.             this.Name = name;
  67.             this.Price = price;
  68.             this.Producer = producer;
  69.         }
  70.  
  71.         public string Name { get; set; }
  72.  
  73.         public decimal Price { get; set; }
  74.  
  75.         public string Producer { get; set; }
  76.  
  77.         //public int CompareTo(object obj)
  78.         //{
  79.         //    var other = obj as Product;
  80.         //    int result = this.Name.CompareTo(other.Name);
  81.         //    if (result == 0)
  82.         //    {
  83.         //        result = this.Producer.CompareTo(other.Producer);
  84.  
  85.         //        if (result == 0)
  86.         //        {
  87.         //            result = this.Price.CompareTo(other.Price);
  88.  
  89.         //            //if (result == 0 && !ReferenceEquals(this, other))
  90.         //            //{
  91.         //            //    return 1;
  92.         //            //}
  93.         //        }
  94.         //    }
  95.  
  96.         //    return result;
  97.         //}
  98.  
  99.         public override string ToString()
  100.         {
  101.             return string.Format("{{{0};{1};{2:0.00}}}", this.Name, this.Producer, this.Price);
  102.         }
  103.     }
  104.  
  105.     public class Store
  106.     {
  107.         private const string ProductAddedMsg = "Product added";
  108.         private const string ProductDeletedMsg = "{0} products deleted";
  109.         private const string NoProductsFoundMsg = "No products found";
  110.  
  111.         private readonly Dictionary<decimal, List<Product>> productsByPrice;
  112.         private readonly Dictionary<string, List<Product>> productsByProducer;
  113.         private readonly Dictionary<string, List<Product>> productsByNameAndProducer;
  114.         private readonly Dictionary<string, List<Product>> productsByName;
  115.  
  116.         public Store()
  117.         {
  118.             this.productsByPrice = new Dictionary<decimal, List<Product>>();
  119.             this.productsByProducer = new Dictionary<string, List<Product>>();
  120.             this.productsByNameAndProducer = new Dictionary<string, List<Product>>();
  121.             this.productsByName = new Dictionary<string, List<Product>>();
  122.         }
  123.  
  124.         public string AddProduct(string name, decimal price, string producer)
  125.         {
  126.             var product = new Product(name, price, producer);
  127.  
  128.             if (!(this.productsByPrice.ContainsKey(product.Price)))
  129.             {
  130.                 this.productsByPrice[product.Price] = new List<Product>();
  131.             }
  132.  
  133.             if (!(this.productsByProducer.ContainsKey(product.Producer)))
  134.             {
  135.                 this.productsByProducer[product.Producer] = new List<Product>();
  136.             }
  137.  
  138.             if (!(this.productsByName.ContainsKey(product.Name)))
  139.             {
  140.                 this.productsByName[product.Name] = new List<Product>();
  141.             }
  142.  
  143.             if (!(this.productsByNameAndProducer.ContainsKey(product.Name + product.Producer)))
  144.             {
  145.                 this.productsByNameAndProducer[product.Name + product.Producer] = new List<Product>();
  146.             }
  147.  
  148.             productsByPrice[product.Price].Add(product);
  149.             productsByProducer[product.Producer].Add(product);
  150.             productsByName[product.Name].Add(product);
  151.             productsByNameAndProducer[product.Name + product.Producer].Add(product);
  152.  
  153.             return ProductAddedMsg;
  154.         }
  155.  
  156.         public string DeleteProduct(string producer)
  157.         {
  158.             if (!this.productsByProducer.ContainsKey(producer))
  159.             {
  160.                 return NoProductsFoundMsg;
  161.             }
  162.  
  163.             var toDelete = this.productsByProducer[producer];
  164.  
  165.             if (toDelete.Count == 0)
  166.             {
  167.                 return NoProductsFoundMsg;
  168.             }
  169.  
  170.             int count = toDelete.Count;
  171.             foreach (var product in toDelete)
  172.             {
  173.                 this.productsByNameAndProducer[product.Name + product.Producer].Remove(product);
  174.                 this.productsByName[product.Name].Remove(product);
  175.                 this.productsByPrice[product.Price].Remove(product);
  176.             }
  177.  
  178.             this.productsByProducer.Remove(producer);
  179.  
  180.             return string.Format(ProductDeletedMsg, count);
  181.         }
  182.  
  183.         public string DeleteProduct(string producer, string name)
  184.         {
  185.             string key = name + producer;
  186.             if (!this.productsByNameAndProducer.ContainsKey(key))
  187.             {
  188.                 return NoProductsFoundMsg;
  189.             }
  190.  
  191.             var toDelete = this.productsByNameAndProducer[key];
  192.  
  193.             if (toDelete.Count == 0)
  194.             {
  195.                 return NoProductsFoundMsg;
  196.             }
  197.  
  198.             int count = toDelete.Count;
  199.             foreach (var product in toDelete)
  200.             {
  201.                 this.productsByName[product.Name].Remove(product);
  202.                 this.productsByPrice[product.Price].Remove(product);
  203.                 this.productsByProducer[product.Producer].Remove(product);
  204.             }
  205.  
  206.             this.productsByNameAndProducer.Remove(key);
  207.  
  208.             return string.Format(ProductDeletedMsg, count);
  209.         }
  210.  
  211.         public string FindProductsByName(string name)
  212.         {
  213.             if (!this.productsByName.ContainsKey(name))
  214.             {
  215.                 return NoProductsFoundMsg;
  216.             }
  217.  
  218.             var result = productsByName[name];
  219.             if (result.Count == 0)
  220.             {
  221.                 return NoProductsFoundMsg;
  222.             }
  223.  
  224.             return GetProductsListAsString(result);
  225.         }
  226.  
  227.         public string FindProductsByPriceRange(decimal minPrice, decimal maxPrice)
  228.         {
  229.             var result = productsByPrice
  230.                 .Where(p => p.Key >= minPrice && p.Key <= maxPrice)
  231.                 .SelectMany(p => p.Value)
  232.                 .ToList();
  233.  
  234.             if (result.Count == 0)
  235.             {
  236.                 return NoProductsFoundMsg;
  237.             }
  238.  
  239.             return GetProductsListAsString(result);
  240.         }
  241.  
  242.         public string FindProductsByProducer(string producer)
  243.         {
  244.             if (!this.productsByProducer.ContainsKey(producer))
  245.             {
  246.                 return NoProductsFoundMsg;
  247.             }
  248.  
  249.             var result = this.productsByProducer[producer];
  250.             if (result.Count == 0)
  251.             {
  252.                 return NoProductsFoundMsg;
  253.             }
  254.  
  255.             return this.GetProductsListAsString(result);
  256.         }
  257.  
  258.         private IEnumerable<Product> SortCollection(IEnumerable<Product> collection)
  259.         {
  260.             return collection.OrderBy(p => p.ToString());
  261.         }
  262.  
  263.         private string GetProductsListAsString(IEnumerable<Product> result)
  264.         {
  265.             var sorted = this.SortCollection(result);
  266.             var sb = new StringBuilder();
  267.             foreach (var product in sorted)
  268.             {
  269.                 sb.AppendLine(product.ToString());
  270.             }
  271.  
  272.             return sb.ToString().Trim();
  273.         }
  274.     }
  275.  
  276.     public class Command
  277.     {
  278.         public Command(string name, string[] parameters)
  279.         {
  280.             this.Name = name;
  281.             this.Params = parameters;
  282.         }
  283.  
  284.         public string Name { get; set; }
  285.  
  286.         public string[] Params { get; set; }
  287.  
  288.         public static Command Parse(string input)
  289.         {
  290.             var commandEnd = input.IndexOf(' ');
  291.             string commandName = input.Substring(0, input.IndexOf(' '));
  292.             var parameters = input.Substring(commandEnd + 1)
  293.                 .Split(new char[] { ';' });
  294.  
  295.             return new Command(commandName, parameters);
  296.         }
  297.     }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement