Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace OnlineStore
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- public class OnlineStoreSolution
- {
- private readonly static StringBuilder output = new StringBuilder();
- public static void Main()
- {
- Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
- int n = int.Parse(Console.ReadLine());
- var store = new Store();
- for (int i = 0; i < n; i++)
- {
- var commandLine = Console.ReadLine();
- var command = Command.Parse(commandLine);
- ProcessCommand(command, store);
- }
- Console.WriteLine(output.ToString().Trim());
- }
- private static void ProcessCommand(Command command, Store store)
- {
- switch (command.Name)
- {
- case "AddProduct":
- output.AppendLine(store.AddProduct(command.Params[0], decimal.Parse(command.Params[1]), command.Params[2]));
- break;
- case "DeleteProducts":
- if (command.Params.Length > 1)
- {
- output.AppendLine(store.DeleteProduct(command.Params[1], command.Params[0]));
- }
- else
- {
- output.AppendLine(store.DeleteProduct(command.Params[0]));
- }
- break;
- case "FindProductsByName":
- output.AppendLine(store.FindProductsByName(command.Params[0]));
- break;
- case "FindProductsByPriceRange":
- output.AppendLine(store.FindProductsByPriceRange(decimal.Parse(command.Params[0]),
- decimal.Parse(command.Params[1])));
- break;
- case "FindProductsByProducer":
- output.AppendLine(store.FindProductsByProducer(command.Params[0]));
- break;
- }
- }
- }
- public class Product //: IComparable
- {
- public Product(string name, decimal price, string producer)
- {
- this.Name = name;
- this.Price = price;
- this.Producer = producer;
- }
- public string Name { get; set; }
- public decimal Price { get; set; }
- public string Producer { get; set; }
- //public int CompareTo(object obj)
- //{
- // var other = obj as Product;
- // int result = this.Name.CompareTo(other.Name);
- // if (result == 0)
- // {
- // result = this.Producer.CompareTo(other.Producer);
- // if (result == 0)
- // {
- // result = this.Price.CompareTo(other.Price);
- // //if (result == 0 && !ReferenceEquals(this, other))
- // //{
- // // return 1;
- // //}
- // }
- // }
- // return result;
- //}
- public override string ToString()
- {
- return string.Format("{{{0};{1};{2:0.00}}}", this.Name, this.Producer, this.Price);
- }
- }
- public class Store
- {
- private const string ProductAddedMsg = "Product added";
- private const string ProductDeletedMsg = "{0} products deleted";
- private const string NoProductsFoundMsg = "No products found";
- private readonly Dictionary<decimal, List<Product>> productsByPrice;
- private readonly Dictionary<string, List<Product>> productsByProducer;
- private readonly Dictionary<string, List<Product>> productsByNameAndProducer;
- private readonly Dictionary<string, List<Product>> productsByName;
- public Store()
- {
- this.productsByPrice = new Dictionary<decimal, List<Product>>();
- this.productsByProducer = new Dictionary<string, List<Product>>();
- this.productsByNameAndProducer = new Dictionary<string, List<Product>>();
- this.productsByName = new Dictionary<string, List<Product>>();
- }
- public string AddProduct(string name, decimal price, string producer)
- {
- var product = new Product(name, price, producer);
- if (!(this.productsByPrice.ContainsKey(product.Price)))
- {
- this.productsByPrice[product.Price] = new List<Product>();
- }
- if (!(this.productsByProducer.ContainsKey(product.Producer)))
- {
- this.productsByProducer[product.Producer] = new List<Product>();
- }
- if (!(this.productsByName.ContainsKey(product.Name)))
- {
- this.productsByName[product.Name] = new List<Product>();
- }
- if (!(this.productsByNameAndProducer.ContainsKey(product.Name + product.Producer)))
- {
- this.productsByNameAndProducer[product.Name + product.Producer] = new List<Product>();
- }
- productsByPrice[product.Price].Add(product);
- productsByProducer[product.Producer].Add(product);
- productsByName[product.Name].Add(product);
- productsByNameAndProducer[product.Name + product.Producer].Add(product);
- return ProductAddedMsg;
- }
- public string DeleteProduct(string producer)
- {
- if (!this.productsByProducer.ContainsKey(producer))
- {
- return NoProductsFoundMsg;
- }
- var toDelete = this.productsByProducer[producer];
- if (toDelete.Count == 0)
- {
- return NoProductsFoundMsg;
- }
- int count = toDelete.Count;
- foreach (var product in toDelete)
- {
- this.productsByNameAndProducer[product.Name + product.Producer].Remove(product);
- this.productsByName[product.Name].Remove(product);
- this.productsByPrice[product.Price].Remove(product);
- }
- this.productsByProducer.Remove(producer);
- return string.Format(ProductDeletedMsg, count);
- }
- public string DeleteProduct(string producer, string name)
- {
- string key = name + producer;
- if (!this.productsByNameAndProducer.ContainsKey(key))
- {
- return NoProductsFoundMsg;
- }
- var toDelete = this.productsByNameAndProducer[key];
- if (toDelete.Count == 0)
- {
- return NoProductsFoundMsg;
- }
- int count = toDelete.Count;
- foreach (var product in toDelete)
- {
- this.productsByName[product.Name].Remove(product);
- this.productsByPrice[product.Price].Remove(product);
- this.productsByProducer[product.Producer].Remove(product);
- }
- this.productsByNameAndProducer.Remove(key);
- return string.Format(ProductDeletedMsg, count);
- }
- public string FindProductsByName(string name)
- {
- if (!this.productsByName.ContainsKey(name))
- {
- return NoProductsFoundMsg;
- }
- var result = productsByName[name];
- if (result.Count == 0)
- {
- return NoProductsFoundMsg;
- }
- return GetProductsListAsString(result);
- }
- public string FindProductsByPriceRange(decimal minPrice, decimal maxPrice)
- {
- var result = productsByPrice
- .Where(p => p.Key >= minPrice && p.Key <= maxPrice)
- .SelectMany(p => p.Value)
- .ToList();
- if (result.Count == 0)
- {
- return NoProductsFoundMsg;
- }
- return GetProductsListAsString(result);
- }
- public string FindProductsByProducer(string producer)
- {
- if (!this.productsByProducer.ContainsKey(producer))
- {
- return NoProductsFoundMsg;
- }
- var result = this.productsByProducer[producer];
- if (result.Count == 0)
- {
- return NoProductsFoundMsg;
- }
- return this.GetProductsListAsString(result);
- }
- private IEnumerable<Product> SortCollection(IEnumerable<Product> collection)
- {
- return collection.OrderBy(p => p.ToString());
- }
- private string GetProductsListAsString(IEnumerable<Product> result)
- {
- var sorted = this.SortCollection(result);
- var sb = new StringBuilder();
- foreach (var product in sorted)
- {
- sb.AppendLine(product.ToString());
- }
- return sb.ToString().Trim();
- }
- }
- public class Command
- {
- public Command(string name, string[] parameters)
- {
- this.Name = name;
- this.Params = parameters;
- }
- public string Name { get; set; }
- public string[] Params { get; set; }
- public static Command Parse(string input)
- {
- var commandEnd = input.IndexOf(' ');
- string commandName = input.Substring(0, input.IndexOf(' '));
- var parameters = input.Substring(commandEnd + 1)
- .Split(new char[] { ';' });
- return new Command(commandName, parameters);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement