Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace ShoppingCenterWithBag
- {
- using System;
- using System.Text;
- using Wintellect.PowerCollections;
- /// <summary>
- /// Slow for BGCoder - gets 60/100 and time limits the bigger tests.
- /// </summary>
- public class ShoppingCenterWithBag
- {
- private const string ProductAdded = "Product added";
- private const string XProductsDeleted = " products deleted";
- private const string NOProductsFound = "No products found";
- private const string IncorrectCommand = "Incorrect command";
- private static readonly OrderedBag<Product> center = new OrderedBag<Product>();
- public static void Main()
- {
- StringBuilder answer = new StringBuilder();
- int commands = int.Parse(Console.ReadLine());
- for (int i = 1; i <= commands; i++)
- {
- string command = Console.ReadLine();
- string commandResult = ProcessCommand(command);
- answer.AppendLine(commandResult);
- }
- Console.Write(answer);
- }
- public static string ProcessCommand(string command)
- {
- int indexOfFirstSpace = command.IndexOf(' ');
- string method = command.Substring(0, indexOfFirstSpace);
- string parameterValues = command.Substring(indexOfFirstSpace + 1);
- string[] parameters = parameterValues.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
- string commandResult;
- switch (method)
- {
- case "AddProduct":
- {
- commandResult = AddProduct(parameters[0], parameters[1], parameters[2]);
- break;
- }
- case "DeleteProducts":
- {
- if (parameters.Length == 1)
- {
- commandResult = DeleteProducts(parameters[0]);
- }
- else
- {
- commandResult = DeleteProducts(parameters[0], parameters[1]);
- }
- break;
- }
- case "FindProductsByName":
- {
- commandResult = FindProductsByName(parameters[0]);
- break;
- }
- case "FindProductsByPriceRange":
- {
- commandResult = FindProductsByPriceRange(parameters[0], parameters[1]);
- break;
- }
- case "FindProductsByProducer":
- {
- commandResult = FindProductsByProducer(parameters[0]);
- break;
- }
- default:
- {
- commandResult = IncorrectCommand;
- break;
- }
- }
- return commandResult;
- }
- private static string FindProductsByProducer(string producer)
- {
- int counter = 0;
- var found = center.FindAll(x => x.Producer == producer);
- StringBuilder results = new StringBuilder();
- foreach (var row in found)
- {
- results.Append("{");
- results.AppendFormat("{0};{1};{2:f2}", row.Name, row.Producer, row.Price);
- results.Append("}");
- results.AppendLine();
- counter++;
- }
- if (counter == 0)
- {
- return NOProductsFound;
- }
- else
- {
- results.Length--;
- results.Length--;
- return results.ToString();
- }
- }
- private static string FindProductsByPriceRange(string priceFrom, string priceTo)
- {
- int counter = 0;
- var rows = center.FindAll(x => x.Price >= decimal.Parse(priceFrom) && x.Price <= decimal.Parse(priceTo));
- StringBuilder results = new StringBuilder();
- foreach (var row in rows)
- {
- results.Append("{");
- results.AppendFormat("{0};{1};{2:f2}", row.Name, row.Producer, row.Price);
- results.Append("}");
- results.AppendLine();
- counter++;
- }
- if (counter == 0)
- {
- return NOProductsFound;
- }
- else
- {
- results.Length--;
- results.Length--;
- return results.ToString();
- }
- }
- private static string FindProductsByName(string product)
- {
- int counter = 0;
- var found = center.FindAll(x => x.Name == product);
- StringBuilder results = new StringBuilder();
- foreach (var item in found)
- {
- results.Append("{");
- results.AppendFormat("{0};{1};{2:f2}", item.Name, item.Producer, item.Price);
- results.Append("}");
- results.AppendLine();
- counter++;
- }
- if (counter == 0)
- {
- return NOProductsFound;
- }
- else
- {
- results.Length--;
- results.Length--;
- return results.ToString();
- }
- }
- private static string DeleteProducts(string producer)
- {
- int counter = 0;
- counter = center.RemoveAll(x => x.Producer == producer).Count;
- if (counter == 0)
- {
- return NOProductsFound;
- }
- else
- {
- return counter + XProductsDeleted;
- }
- }
- private static string DeleteProducts(string product, string producer)
- {
- int counter = 0;
- counter = center.RemoveAll(x => x.Producer == producer && x.Name == product ).Count;
- if (counter == 0)
- {
- return NOProductsFound;
- }
- else
- {
- return counter + XProductsDeleted;
- }
- }
- private static string AddProduct(string name, string price, string producer)
- {
- Product product = new Product(name, decimal.Parse(price), producer);
- center.Add(product);
- return ProductAdded;
- }
- }
- public class Product : IComparable<Product>
- {
- public string Name { get; set; }
- public string Producer { get; set; }
- public decimal Price { get; set; }
- public Product(string name, decimal price, string producer )
- {
- this.Name = name;
- this.Producer = producer;
- this.Price = price;
- }
- public int CompareTo(Product other)
- {
- int resultOfCompare = Name.CompareTo(other.Name);
- if (resultOfCompare == 0)
- {
- resultOfCompare = Producer.CompareTo(other.Producer);
- }
- if (resultOfCompare == 0)
- {
- resultOfCompare = Price.CompareTo(other.Price);
- }
- return resultOfCompare;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement