Advertisement
Adrian_Apostolov

OnlineStore

Dec 3rd, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.30 KB | None | 0 0
  1. namespace OnlineStore
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.IO;
  6.     using System.Linq;
  7.     using System.Text;
  8.     using System.Threading.Tasks;
  9.     using Wintellect.PowerCollections;
  10.  
  11.     public class Product : IComparable<Product>
  12.     {
  13.         public Product(string name, decimal price, string producer)
  14.         {
  15.             this.Name = name;
  16.             this.Price = price;
  17.             this.Producer = producer;
  18.         }
  19.  
  20.         public string Name { get; set; }
  21.  
  22.         public decimal Price { get; set; }
  23.  
  24.         public string Producer { get; set; }
  25.  
  26.         public override string ToString()
  27.         {
  28.             return string.Format("{0};{1};{2:0.00}", this.Name, this.Producer, this.Price);
  29.         }
  30.  
  31.         public int CompareTo(Product other)
  32.         {
  33.             if (this == other)
  34.             {
  35.                 return 0;
  36.             }
  37.             int result = this.Name.CompareTo(other.Name);
  38.             if (result == 0)
  39.             {
  40.                 result = this.Producer.CompareTo(other.Producer);
  41.             }
  42.             if (result == 0)
  43.             {
  44.                 result = this.Price.CompareTo(other.Price);
  45.             }
  46.  
  47.             return result;
  48.         }
  49.     }
  50.  
  51.     public class Startup
  52.     {
  53.         private const string ProductAddedMessage = "Product added";
  54.         private const string ProductDeletedMessage = " products deleted";
  55.         private const string ProductNotFoundMessage = "No products found";
  56.  
  57.         static Dictionary<string, List<Product>> productsByNameAndProducer = new Dictionary<string, List<Product>>();
  58.         static Dictionary<string, List<Product>> productsByName = new Dictionary<string, List<Product>>();
  59.         static Dictionary<decimal, List<Product>> productsByPrice = new Dictionary<decimal, List<Product>>();
  60.         static Dictionary<string, List<Product>> productsByProducer = new Dictionary<string, List<Product>>();
  61.         static StringBuilder sb = new StringBuilder();
  62.  
  63.         static void Main()
  64.         {
  65.             //var input = File.ReadAllText("../../input.txt");
  66.             //var str = new StringReader(input);
  67.             //Console.SetIn(str);
  68.  
  69.             var n = int.Parse(Console.ReadLine());
  70.  
  71.             for (int i = 0; i < n; i++)
  72.             {
  73.                 var command = Console.ReadLine();
  74.                 var indexOfWhitespace = command.IndexOf(' ');
  75.                 var commandName = command.Substring(0, indexOfWhitespace);
  76.                 var commandParameter = command.Substring(indexOfWhitespace + 1);
  77.                 string[] parameters = commandParameter.Split(';');
  78.  
  79.                 if (commandName == "AddProduct")
  80.                 {
  81.                     var product = new Product(parameters[0], decimal.Parse(parameters[1]), parameters[2]);
  82.                     var nameProducer = parameters[0] + parameters[2];
  83.                     if (!productsByName.ContainsKey(product.Name))
  84.                     {
  85.                         productsByName.Add(product.Name, new List<Product>());
  86.                     }
  87.  
  88.                     if (!productsByPrice.ContainsKey(product.Price))
  89.                     {
  90.                         productsByPrice.Add(product.Price, new List<Product>());
  91.                     }
  92.  
  93.                     if (!productsByProducer.ContainsKey(product.Producer))
  94.                     {
  95.                         productsByProducer.Add(product.Producer, new List<Product>());
  96.                     }
  97.  
  98.                     if (!productsByNameAndProducer.ContainsKey(nameProducer))
  99.                     {
  100.                         productsByNameAndProducer.Add(nameProducer, new List<Product>());
  101.                     }
  102.  
  103.                     productsByName[product.Name].Add(product);
  104.                     productsByPrice[product.Price].Add(product);
  105.                     productsByProducer[product.Producer].Add(product);
  106.                     productsByNameAndProducer[nameProducer].Add(product);
  107.  
  108.                     sb.AppendLine(ProductAddedMessage);
  109.                 }
  110.                 else if (commandName == "DeleteProducts")
  111.                 {
  112.                     if (parameters.Count() == 1)
  113.                     {
  114.                         if (!productsByProducer.ContainsKey(parameters[0]))
  115.                         {
  116.                             sb.AppendLine(ProductNotFoundMessage);
  117.  
  118.                         }
  119.                         else
  120.                         {
  121.                             var deletedProduct = productsByProducer[parameters[0]];
  122.  
  123.                             var count = deletedProduct.Count;
  124.                             foreach (var item in deletedProduct)
  125.                             {
  126.                                 productsByNameAndProducer.Remove(item.Name + item.Producer);
  127.                                 productsByName.Remove(item.Name);
  128.                                 productsByPrice.Remove(item.Price);
  129.                             }
  130.  
  131.                             productsByProducer.Remove(parameters[0]);
  132.                             sb.AppendLine(count + ProductDeletedMessage);
  133.                         }
  134.                     }
  135.                     else if (parameters.Count() == 2)
  136.                     {
  137.                         var nameProducer = parameters[0] + parameters[1];
  138.  
  139.                         if (productsByNameAndProducer.ContainsKey(nameProducer))
  140.                         {
  141.  
  142.                             var deletedPorduct = productsByNameAndProducer[nameProducer];
  143.                             var count = deletedPorduct.Count;
  144.                             foreach (var item in deletedPorduct)
  145.                             {
  146.                                 productsByName.Remove(item.Name);
  147.                                 productsByPrice.Remove(item.Price);
  148.                                 productsByProducer.Remove(item.Producer);
  149.                             }
  150.  
  151.                             productsByNameAndProducer.Remove(nameProducer);
  152.                             sb.AppendLine(count + ProductDeletedMessage);
  153.                         }
  154.                         else
  155.                         {
  156.                             sb.AppendLine(ProductNotFoundMessage);
  157.                         }
  158.                     }
  159.                 }
  160.                 else if (commandName == "FindProductsByName")
  161.                 {
  162.  
  163.                     if (!productsByName.ContainsKey(parameters[0]))
  164.                     {
  165.                         sb.AppendLine(ProductNotFoundMessage);
  166.                     }
  167.                     else
  168.                     {
  169.                         var findByName = productsByName[parameters[0]]
  170.                             .OrderBy(s => s.ToString());
  171.  
  172.                         foreach (var item in findByName)
  173.                         {
  174.                             sb.AppendLine("{" + item.ToString() + "}");
  175.                         }
  176.                     }
  177.  
  178.                 }
  179.                 else if (commandName == "FindProductsByPriceRange")
  180.                 {
  181.                     var from = decimal.Parse(parameters[0]);
  182.                     var to = decimal.Parse(parameters[1]);
  183.  
  184.                     var findByPriceRange = productsByPrice
  185.                         .Where(x => x.Key >= from && x.Key <= to).SelectMany(p => p.Value)
  186.                         .OrderBy(s => s.ToString())
  187.                         .ToList();
  188.  
  189.                     if (findByPriceRange.Count > 0)
  190.                     {
  191.                         foreach (var item in findByPriceRange)
  192.                         {
  193.                             sb.AppendLine("{" + item.ToString() + "}");
  194.                         }
  195.                     }
  196.                     else
  197.                     {
  198.                         sb.AppendLine(ProductNotFoundMessage);
  199.                     }
  200.                 }
  201.                 else if (commandName == "FindProductsByProducer")
  202.                 {
  203.  
  204.                     if (!productsByProducer.ContainsKey(parameters[0]))
  205.                     {
  206.                         sb.AppendLine(ProductNotFoundMessage);
  207.  
  208.                     }
  209.                     else
  210.                     {
  211.                         var findByProducer = productsByProducer[parameters[0]]
  212.                             .OrderBy(s => s.ToString());
  213.  
  214.                         foreach (var item in findByProducer)
  215.                         {
  216.                             sb.AppendLine("{" + item.ToString() + "}");
  217.                         }
  218.                     }
  219.                 }
  220.             }
  221.  
  222.             Console.WriteLine(sb.ToString().Trim());
  223.         }
  224.     }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement