Advertisement
sylviapsh

Shopping Center With Bag - time limits

Jun 24th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.50 KB | None | 0 0
  1. namespace ShoppingCenterWithBag
  2. {
  3.     using System;
  4.     using System.Text;
  5.     using Wintellect.PowerCollections;
  6.  
  7.     /// <summary>
  8.     /// Slow for BGCoder - gets 60/100 and time limits the bigger tests.
  9.     /// </summary>
  10.     public class ShoppingCenterWithBag
  11.     {
  12.         private const string ProductAdded = "Product added";
  13.  
  14.         private const string XProductsDeleted = " products deleted";
  15.  
  16.         private const string NOProductsFound = "No products found";
  17.  
  18.         private const string IncorrectCommand = "Incorrect command";
  19.  
  20.         private static readonly OrderedBag<Product> center = new OrderedBag<Product>();
  21.  
  22.         public static void Main()
  23.         {
  24.             StringBuilder answer = new StringBuilder();
  25.  
  26.             int commands = int.Parse(Console.ReadLine());
  27.  
  28.             for (int i = 1; i <= commands; i++)
  29.             {
  30.                 string command = Console.ReadLine();
  31.  
  32.                 string commandResult = ProcessCommand(command);
  33.  
  34.                 answer.AppendLine(commandResult);
  35.             }
  36.  
  37.             Console.Write(answer);
  38.         }
  39.  
  40.         public static string ProcessCommand(string command)
  41.         {
  42.             int indexOfFirstSpace = command.IndexOf(' ');
  43.  
  44.             string method = command.Substring(0, indexOfFirstSpace);
  45.  
  46.             string parameterValues = command.Substring(indexOfFirstSpace + 1);
  47.  
  48.             string[] parameters = parameterValues.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  49.  
  50.             string commandResult;
  51.  
  52.             switch (method)
  53.             {
  54.                 case "AddProduct":
  55.                     {
  56.                         commandResult = AddProduct(parameters[0], parameters[1], parameters[2]);
  57.  
  58.                         break;
  59.                     }
  60.  
  61.                 case "DeleteProducts":
  62.                     {
  63.                         if (parameters.Length == 1)
  64.                         {
  65.                             commandResult = DeleteProducts(parameters[0]);
  66.                         }
  67.                         else
  68.                         {
  69.                             commandResult = DeleteProducts(parameters[0], parameters[1]);
  70.                         }
  71.  
  72.                         break;
  73.                     }
  74.  
  75.                 case "FindProductsByName":
  76.                     {
  77.                         commandResult = FindProductsByName(parameters[0]);
  78.  
  79.                         break;
  80.                     }
  81.  
  82.                 case "FindProductsByPriceRange":
  83.                     {
  84.                         commandResult = FindProductsByPriceRange(parameters[0], parameters[1]);
  85.  
  86.                         break;
  87.                     }
  88.  
  89.                 case "FindProductsByProducer":
  90.                     {
  91.                         commandResult = FindProductsByProducer(parameters[0]);
  92.  
  93.                         break;
  94.                     }
  95.  
  96.                 default:
  97.                     {
  98.                         commandResult = IncorrectCommand;
  99.  
  100.                         break;
  101.                     }
  102.             }
  103.  
  104.             return commandResult;
  105.         }
  106.  
  107.         private static string FindProductsByProducer(string producer)
  108.         {
  109.             int counter = 0;
  110.             var found = center.FindAll(x => x.Producer == producer);
  111.  
  112.             StringBuilder results = new StringBuilder();
  113.             foreach (var row in found)
  114.             {
  115.                 results.Append("{");
  116.                 results.AppendFormat("{0};{1};{2:f2}", row.Name, row.Producer, row.Price);
  117.                 results.Append("}");
  118.                 results.AppendLine();
  119.                 counter++;
  120.             }
  121.  
  122.             if (counter == 0)
  123.             {
  124.                 return NOProductsFound;
  125.             }
  126.             else
  127.             {
  128.                 results.Length--;
  129.                 results.Length--;
  130.                 return results.ToString();
  131.             }
  132.         }
  133.  
  134.         private static string FindProductsByPriceRange(string priceFrom, string priceTo)
  135.         {
  136.             int counter = 0;
  137.             var rows = center.FindAll(x => x.Price >= decimal.Parse(priceFrom) && x.Price <= decimal.Parse(priceTo));
  138.  
  139.             StringBuilder results = new StringBuilder();
  140.             foreach (var row in rows)
  141.             {
  142.                 results.Append("{");
  143.                 results.AppendFormat("{0};{1};{2:f2}", row.Name, row.Producer, row.Price);
  144.                 results.Append("}");
  145.                 results.AppendLine();
  146.                 counter++;
  147.             }
  148.  
  149.             if (counter == 0)
  150.             {
  151.                 return NOProductsFound;
  152.             }
  153.             else
  154.             {
  155.                 results.Length--;
  156.                 results.Length--;
  157.                 return results.ToString();
  158.             }
  159.         }
  160.  
  161.         private static string FindProductsByName(string product)
  162.         {
  163.             int counter = 0;
  164.             var found = center.FindAll(x => x.Name == product);
  165.            
  166.             StringBuilder results = new StringBuilder();
  167.             foreach (var item in found)
  168.             {
  169.                 results.Append("{");
  170.                 results.AppendFormat("{0};{1};{2:f2}", item.Name, item.Producer, item.Price);
  171.                 results.Append("}");
  172.                 results.AppendLine();
  173.                 counter++;
  174.             }
  175.  
  176.             if (counter == 0)
  177.             {
  178.                 return NOProductsFound;
  179.             }
  180.             else
  181.             {
  182.                 results.Length--;
  183.                 results.Length--;
  184.                 return results.ToString();
  185.             }
  186.         }
  187.  
  188.         private static string DeleteProducts(string producer)
  189.         {
  190.             int counter = 0;
  191.             counter = center.RemoveAll(x => x.Producer == producer).Count;
  192.  
  193.             if (counter == 0)
  194.             {
  195.                 return NOProductsFound;
  196.             }
  197.             else
  198.             {
  199.                 return counter + XProductsDeleted;
  200.             }
  201.         }
  202.  
  203.         private static string DeleteProducts(string product, string producer)
  204.         {
  205.             int counter = 0;
  206.             counter = center.RemoveAll(x => x.Producer == producer && x.Name == product ).Count;
  207.  
  208.             if (counter == 0)
  209.             {
  210.                 return NOProductsFound;
  211.             }
  212.             else
  213.             {
  214.                 return counter + XProductsDeleted;
  215.             }
  216.         }
  217.  
  218.         private static string AddProduct(string name, string price, string producer)
  219.         {
  220.             Product product = new Product(name, decimal.Parse(price), producer);
  221.             center.Add(product);
  222.  
  223.             return ProductAdded;
  224.         }
  225.     }
  226.  
  227.     public class Product : IComparable<Product>
  228.     {
  229.         public string Name { get; set; }
  230.         public string Producer { get; set; }
  231.         public decimal Price { get; set; }
  232.  
  233.         public Product(string name, decimal price, string producer )
  234.         {
  235.             this.Name = name;
  236.             this.Producer = producer;
  237.             this.Price = price;
  238.         }
  239.  
  240.         public int CompareTo(Product other)
  241.         {
  242.  
  243.             int resultOfCompare = Name.CompareTo(other.Name);
  244.  
  245.             if (resultOfCompare == 0)
  246.             {
  247.  
  248.                 resultOfCompare = Producer.CompareTo(other.Producer);
  249.  
  250.             }
  251.  
  252.             if (resultOfCompare == 0)
  253.             {
  254.  
  255.                 resultOfCompare = Price.CompareTo(other.Price);
  256.  
  257.             }
  258.  
  259.             return resultOfCompare;
  260.  
  261.         }
  262.     }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement