TodorMitev

ShoppingCenter

Mar 22nd, 2012
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Wintellect.PowerCollections;
  6.  
  7. namespace SampleExam3
  8. {
  9.     #region ProductClass
  10.     public class Product : IComparable<Product>
  11.     {
  12.         private string name;
  13.         private decimal price;
  14.         private string producer;
  15.  
  16.         public Product(string productName, decimal productPrice, string productProducer)
  17.         {
  18.             this.name = productName;
  19.             this.price = productPrice;
  20.             this.producer = productProducer;
  21.         }
  22.  
  23.         public int CompareTo(Product other)
  24.         {
  25.             return this.Name.CompareTo(other.Name);
  26.         }
  27.  
  28.         public override string ToString()
  29.         {
  30.             return String.Format("{3}{0};{1};{2:0.00}{4}", this.Name, this.Producer, this.Price, "{", "}");
  31.         }
  32.  
  33.         public string Name
  34.         {
  35.             get
  36.             {
  37.                 return this.name;
  38.             }
  39.         }
  40.  
  41.         public decimal Price
  42.         {
  43.             get
  44.             {
  45.                 return this.price;
  46.             }
  47.         }
  48.  
  49.         public string Producer
  50.         {
  51.             get
  52.             {
  53.                 return this.producer;
  54.             }
  55.         }
  56.     #endregion
  57.     }
  58.     public class ShoppingCart
  59.     {
  60.         public const string NO_PRODUCTS_FOUND_MSG = "No products found";
  61.         private OrderedMultiDictionary<decimal, Product> cart;
  62.  
  63.         public ShoppingCart()
  64.         {
  65.             cart = new OrderedMultiDictionary<decimal, Product>(true);
  66.         }
  67.  
  68.         public string AddProduct(Product product)
  69.         {
  70.             cart.Add(product.Price, product);
  71.             return "Product added";
  72.         }
  73.  
  74.         public string DeleteProducts(string producer)
  75.         {
  76.             int deletedProducts = 0;
  77.             foreach (KeyValuePair<decimal, ICollection<Product>> pair in cart)
  78.             {
  79.                 for (int i = 0; i < pair.Value.ToList().Count; i++)
  80.                 {
  81.                     if (pair.Value.ToList()[i].Producer == producer)
  82.                     {
  83.                         pair.Value.ToList().RemoveAt(i);
  84.                         deletedProducts++;
  85.                     }
  86.                 }
  87.             }
  88.  
  89.             if (deletedProducts > 0)
  90.             {
  91.                 return String.Format("{0} products deleted", deletedProducts);
  92.             }
  93.             else
  94.             {
  95.                 return NO_PRODUCTS_FOUND_MSG;
  96.             }
  97.         }
  98.  
  99.         public string DeleteProducts(string name, string producer)
  100.         {
  101.             int deletedProducts = 0;
  102.             foreach (KeyValuePair<decimal, ICollection<Product>> pair in cart)
  103.             {
  104.                 for (int i = 0; i < pair.Value.ToList().Count; i++)
  105.                 {
  106.                     if (pair.Value.ToList()[i].Name == name && pair.Value.ToList()[i].Producer == producer)
  107.                     {
  108.                         pair.Value.ToList().RemoveAt(i);
  109.                         deletedProducts++;
  110.                     }
  111.                 }
  112.             }
  113.  
  114.             if (deletedProducts > 0)
  115.             {
  116.                 return String.Format("{0} products deleted", deletedProducts);
  117.             }
  118.             else
  119.             {
  120.                 return NO_PRODUCTS_FOUND_MSG;
  121.             }
  122.         }
  123.  
  124.         public List<Product> FindProductsByName(string name)
  125.         {
  126.             List<Product> foundProducts = new List<Product>();
  127.  
  128.             foreach (KeyValuePair<decimal, ICollection<Product>> pair in cart)
  129.             {
  130.                 for (int i = 0; i < pair.Value.ToList().Count; i++)
  131.                 {
  132.                     if (pair.Value.ToList()[i].Name == name)
  133.                     {
  134.                         foundProducts.Add(pair.Value.ToList()[i]);
  135.                     }
  136.                 }
  137.             }
  138.             return foundProducts;
  139.         }
  140.  
  141.         public List<Product> FindProductsByProducer(string producer)
  142.         {
  143.             List<Product> foundProducts = new List<Product>();
  144.  
  145.             foreach (KeyValuePair<decimal, ICollection<Product>> pair in cart)
  146.             {
  147.                 for (int i = 0; i < pair.Value.ToList().Count; i++)
  148.                 {
  149.                     if (pair.Value.ToList()[i].Producer == producer)
  150.                     {
  151.                         foundProducts.Add(pair.Value.ToList()[i]);
  152.                     }
  153.                 }
  154.             }
  155.             return foundProducts;
  156.         }
  157.  
  158.         public KeyValuePair<decimal, ICollection<Product>>[] FindProductsByPriceRange(decimal from, decimal to)
  159.         {
  160.             return this.cart.Range(from, true, to, true).ToArray();
  161.         }
  162.     }
  163.     public class ShoppingCenter
  164.     {
  165.         static string VisitShoppingCenter(int numberOfCommands)
  166.         {
  167.             StringBuilder result = new StringBuilder();
  168.             int currentCommandNumber = 0;
  169.             ShoppingCart myCart = new ShoppingCart();
  170.  
  171.             while (currentCommandNumber < numberOfCommands)
  172.             {
  173.                 string inputCommand = Console.ReadLine();
  174.                 int indexOfFirstSpace = inputCommand.IndexOf(' ');
  175.                
  176.                 StringBuilder command = new StringBuilder();
  177.                 StringBuilder parameters = new StringBuilder();
  178.                 command.Append(inputCommand.Substring(0,indexOfFirstSpace));
  179.                 parameters.Append(inputCommand.Substring(indexOfFirstSpace+1,inputCommand.Length-indexOfFirstSpace-1));
  180.                 string[] toDo = parameters.ToString().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  181.                 switch (command.ToString())
  182.                 {
  183.                     case ("AddProduct"):
  184.                         {
  185.                             Product productToAdd = new Product(toDo[0].Trim(), Convert.ToDecimal(toDo[1].Trim()), toDo[2].Trim());
  186.                             result.AppendLine(myCart.AddProduct(productToAdd));
  187.                         }
  188.                         break;
  189.                     case ("DeleteProducts"):
  190.                         {
  191.                             int indexOfSeparator = inputCommand.IndexOf(';');
  192.                             if (indexOfSeparator < 0)
  193.                             {
  194.                                 result.AppendLine(myCart.DeleteProducts(toDo[0].Trim()));
  195.                             }
  196.                             else
  197.                             {
  198.                                 result.AppendLine(myCart.DeleteProducts(toDo[0].Trim(), toDo[1].Trim()));
  199.                             }
  200.                         }
  201.                         break;
  202.                     case ("FindProductsByName"):
  203.                         {
  204.                             var foundByName = myCart.FindProductsByName(toDo[0].Trim());
  205.                             if (foundByName.Count != 0)
  206.                             {
  207.                                 foreach (var item in foundByName)
  208.                                 {
  209.                                     result.AppendLine(item.ToString());
  210.                                 }
  211.                             }
  212.                             else
  213.                             {
  214.                                 result.AppendLine(ShoppingCart.NO_PRODUCTS_FOUND_MSG);
  215.                             }
  216.  
  217.                         }
  218.                         break;
  219.                     case ("FindProductsByProducer"):
  220.                         {
  221.                             var foundByProducer = myCart.FindProductsByProducer(toDo[0].Trim());
  222.                             if (foundByProducer.Count!=0)
  223.                             {
  224.                                 foreach (var item in foundByProducer)
  225.                                 {
  226.                                     result.AppendLine(item.ToString());
  227.                                 }
  228.                             }
  229.                             else
  230.                             {
  231.                                 result.AppendLine(ShoppingCart.NO_PRODUCTS_FOUND_MSG);
  232.                             }
  233.                         }
  234.                         break;
  235.                     case ("FindProductsByPriceRange"):
  236.                         {
  237.                             var priceRange = myCart.FindProductsByPriceRange(Convert.ToDecimal(toDo[0].Trim()), Convert.ToDecimal(toDo[1].Trim()));
  238.                             if (priceRange.Length != 0)
  239.                             {
  240.                                 foreach (var item in priceRange)
  241.                                 {
  242.                                     foreach (var val in item.Value)
  243.                                     {
  244.                                         result.AppendLine(val.ToString());
  245.                                     }
  246.                                 }
  247.                             }
  248.                             else
  249.                             {
  250.                                 result.AppendLine(ShoppingCart.NO_PRODUCTS_FOUND_MSG);
  251.                             }
  252.                         }
  253.                         break;
  254.                     default:
  255.                         break;
  256.                 }
  257.                 currentCommandNumber++;
  258.             }
  259.             return result.ToString().TrimEnd();
  260.         }
  261.  
  262.         static void Main(string[] args)
  263.         {
  264.             int numberOfCommandsToExecute = int.Parse(Console.ReadLine());
  265.             string print = VisitShoppingCenter(numberOfCommandsToExecute);
  266.             Console.WriteLine(print);
  267.         }
  268.     }
  269. }
Advertisement
Add Comment
Please, Sign In to add comment