Advertisement
sylviapsh

Shopping Center With Dictionary

Jun 24th, 2013
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.60 KB | None | 0 0
  1. namespace ShoppingCenterWithDic
  2. {
  3.     using System;
  4.     using System.Linq;
  5.     using System.Text;
  6.     using System.Collections.Generic;
  7.     using Wintellect.PowerCollections;
  8.  
  9.     public class Product
  10.     {
  11.         public string Name { get; private set; }
  12.  
  13.         public string Producer { get; private set; }
  14.  
  15.         public decimal Price { get; private set; }
  16.  
  17.         public Product(string name, string producer, decimal price)
  18.         {
  19.             this.Name = name;
  20.             this.Producer = producer;
  21.             this.Price = price;
  22.         }
  23.  
  24.         public override string ToString()
  25.         {
  26.             var result = "{" + this.Name + ";" + this.Producer + ";" + this.Price.ToString("0.00") + "}";
  27.             return result;
  28.         }
  29.     }
  30.  
  31.     public class ShoppingCenter
  32.     {
  33.         private readonly MultiDictionary<string, Product> byName =
  34.             new MultiDictionary<string, Product>(true);
  35.  
  36.         private readonly MultiDictionary<string, Product> byProducer =
  37.             new MultiDictionary<string, Product>(true);
  38.  
  39.         private readonly MultiDictionary<Tuple<string, string>, Product> byNameAndProducer =
  40.             new MultiDictionary<Tuple<string, string>, Product>(true);
  41.  
  42.         private readonly OrderedMultiDictionary<decimal, Product> byPrice =
  43.             new OrderedMultiDictionary<decimal, Product>(true,
  44.                 (x, y) => x.CompareTo(y),
  45.                 (x, y) => 0);
  46.  
  47.         public void AddProduct(Product product)
  48.         {
  49.             this.byName[product.Name].Add(product);
  50.             this.byProducer[product.Producer].Add(product);
  51.             this.byNameAndProducer[Tuple.Create(product.Name, product.Producer)].Add(product);
  52.             this.byPrice[product.Price].Add(product);
  53.         }
  54.  
  55.         public IEnumerable<Product> FindProductsByName(string name)
  56.         {
  57.             var products = this.byName[name];
  58.             return products;
  59.         }
  60.  
  61.         public IEnumerable<Product> FindProductsByProducer(string producer)
  62.         {
  63.             var products = this.byProducer[producer];
  64.             return products;
  65.         }
  66.  
  67.         public IEnumerable<Product> FindProductsByPriceRange(decimal min, decimal max)
  68.         {
  69.             var products = this.byPrice.Range(min, true, max, true).Values;
  70.             return products;
  71.         }
  72.  
  73.         public int DeleteProducts(string producer)
  74.         {
  75.             var products = this.byProducer[producer];
  76.             var count = products.Count;
  77.  
  78.             foreach (var product in products)
  79.             {
  80.                 this.byName[product.Name].Remove(product);
  81.                 this.byNameAndProducer[Tuple.Create(product.Name, product.Producer)].Remove(product);
  82.                 this.byPrice[product.Price].Remove(product);
  83.             }
  84.  
  85.             this.byProducer.Remove(producer);
  86.  
  87.             return count;
  88.         }
  89.  
  90.         public int DeleteProducts(string name, string producer)
  91.         {
  92.             var nameAndProducer = Tuple.Create(name, producer);
  93.  
  94.             var products = this.byNameAndProducer[nameAndProducer];
  95.             var count = products.Count;
  96.  
  97.             foreach (var product in products)
  98.             {
  99.                 this.byName[product.Name].Remove(product);
  100.                 this.byProducer[product.Producer].Remove(product);
  101.                 this.byPrice[product.Price].Remove(product);
  102.             }
  103.  
  104.             this.byNameAndProducer.Remove(nameAndProducer);
  105.  
  106.             return count;
  107.         }
  108.     }
  109.  
  110.     public static class Program
  111.     {
  112.         private static readonly StringBuilder Output = new StringBuilder();
  113.  
  114.         private static void PrintProducts(IEnumerable<Product> products)
  115.         {
  116.             if (!products.Any())
  117.             {
  118.                 Output.AppendLine("No products found");
  119.                 return;
  120.             }
  121.  
  122.             var sorted = products
  123.                                  .OrderBy(x => x.Name)
  124.                                  .ThenBy(x => x.Producer)
  125.                                  .ThenBy(x => x.Price);
  126.  
  127.             foreach (var product in sorted)
  128.             {
  129.                 Output.AppendLine(product.ToString());
  130.             }
  131.         }
  132.  
  133.         public static void Main()
  134.         {
  135.             var separator = new[] { ' ' };
  136.  
  137.             var shoppingCenter = new ShoppingCenter();
  138.  
  139.             foreach (int i in Enumerable.Range(0, int.Parse(Console.ReadLine())))
  140.             {
  141.                 var line = Console.ReadLine();
  142.                 var splitted = line.Split(separator, 2);
  143.                 var name = splitted[0];
  144.                 var parameters = splitted[1].Split(';');
  145.  
  146.                 switch (name)
  147.                 {
  148.                     case "AddProduct":
  149.                         {
  150.                             var product = new Product(
  151.                                 name: parameters[0],
  152.                                 price: decimal.Parse(parameters[1]),
  153.                                 producer: parameters[2]);
  154.  
  155.                             shoppingCenter.AddProduct(product);
  156.  
  157.                             Output.AppendLine("Product added");
  158.                             break;
  159.                         }
  160.  
  161.                     case "FindProductsByName":
  162.                         {
  163.                             var products = shoppingCenter.FindProductsByName(parameters[0]);
  164.  
  165.                             PrintProducts(products);
  166.  
  167.                             break;
  168.                         }
  169.  
  170.                     case "FindProductsByProducer":
  171.                         {
  172.                             var products = shoppingCenter.FindProductsByProducer(parameters[0]);
  173.  
  174.                             PrintProducts(products);
  175.  
  176.                             break;
  177.                         }
  178.  
  179.                     case "FindProductsByPriceRange":
  180.                         {
  181.                             var products = shoppingCenter.FindProductsByPriceRange(
  182.                                 min: decimal.Parse(parameters[0]),
  183.                                 max: decimal.Parse(parameters[1]));
  184.  
  185.                             PrintProducts(products);
  186.  
  187.                             break;
  188.                         }
  189.  
  190.                     case "DeleteProducts":
  191.                         {
  192.                             int deletedCount;
  193.  
  194.                             switch (parameters.Length)
  195.                             {
  196.                                 case 1:
  197.                                     deletedCount = shoppingCenter.DeleteProducts(
  198.                                         producer: parameters[0]);
  199.  
  200.                                     break;
  201.                                 case 2:
  202.                                     deletedCount = shoppingCenter.DeleteProducts(
  203.                                         name: parameters[0],
  204.                                         producer: parameters[1]);
  205.  
  206.                                     break;
  207.                                 default:
  208.                                     throw new ArgumentException("DeleteProducts");
  209.                             }
  210.  
  211.                             if (deletedCount == 0)
  212.                             {
  213.                                 Output.AppendLine("No products found");
  214.                                 break;
  215.                             }
  216.  
  217.                             Output.AppendLine(deletedCount + " products deleted");
  218.  
  219.                             break;
  220.                         }
  221.  
  222.                     default:
  223.                         throw new ArgumentException("Invalid command: " + name);
  224.                 }
  225.             }
  226.  
  227.             Console.Write(Output);
  228.         }
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement