Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Wintellect.PowerCollections;
- namespace SampleExam3
- {
- #region ProductClass
- public class Product : IComparable<Product>
- {
- private string name;
- private decimal price;
- private string producer;
- public Product(string productName, decimal productPrice, string productProducer)
- {
- this.name = productName;
- this.price = productPrice;
- this.producer = productProducer;
- }
- public int CompareTo(Product other)
- {
- return this.Name.CompareTo(other.Name);
- }
- public override string ToString()
- {
- return String.Format("{3}{0};{1};{2:0.00}{4}", this.Name, this.Producer, this.Price, "{", "}");
- }
- public string Name
- {
- get
- {
- return this.name;
- }
- }
- public decimal Price
- {
- get
- {
- return this.price;
- }
- }
- public string Producer
- {
- get
- {
- return this.producer;
- }
- }
- #endregion
- }
- public class ShoppingCart
- {
- public const string NO_PRODUCTS_FOUND_MSG = "No products found";
- private OrderedMultiDictionary<decimal, Product> cart;
- public ShoppingCart()
- {
- cart = new OrderedMultiDictionary<decimal, Product>(true);
- }
- public string AddProduct(Product product)
- {
- cart.Add(product.Price, product);
- return "Product added";
- }
- public string DeleteProducts(string producer)
- {
- int deletedProducts = 0;
- foreach (KeyValuePair<decimal, ICollection<Product>> pair in cart)
- {
- for (int i = 0; i < pair.Value.ToList().Count; i++)
- {
- if (pair.Value.ToList()[i].Producer == producer)
- {
- pair.Value.ToList().RemoveAt(i);
- deletedProducts++;
- }
- }
- }
- if (deletedProducts > 0)
- {
- return String.Format("{0} products deleted", deletedProducts);
- }
- else
- {
- return NO_PRODUCTS_FOUND_MSG;
- }
- }
- public string DeleteProducts(string name, string producer)
- {
- int deletedProducts = 0;
- foreach (KeyValuePair<decimal, ICollection<Product>> pair in cart)
- {
- for (int i = 0; i < pair.Value.ToList().Count; i++)
- {
- if (pair.Value.ToList()[i].Name == name && pair.Value.ToList()[i].Producer == producer)
- {
- pair.Value.ToList().RemoveAt(i);
- deletedProducts++;
- }
- }
- }
- if (deletedProducts > 0)
- {
- return String.Format("{0} products deleted", deletedProducts);
- }
- else
- {
- return NO_PRODUCTS_FOUND_MSG;
- }
- }
- public List<Product> FindProductsByName(string name)
- {
- List<Product> foundProducts = new List<Product>();
- foreach (KeyValuePair<decimal, ICollection<Product>> pair in cart)
- {
- for (int i = 0; i < pair.Value.ToList().Count; i++)
- {
- if (pair.Value.ToList()[i].Name == name)
- {
- foundProducts.Add(pair.Value.ToList()[i]);
- }
- }
- }
- return foundProducts;
- }
- public List<Product> FindProductsByProducer(string producer)
- {
- List<Product> foundProducts = new List<Product>();
- foreach (KeyValuePair<decimal, ICollection<Product>> pair in cart)
- {
- for (int i = 0; i < pair.Value.ToList().Count; i++)
- {
- if (pair.Value.ToList()[i].Producer == producer)
- {
- foundProducts.Add(pair.Value.ToList()[i]);
- }
- }
- }
- return foundProducts;
- }
- public KeyValuePair<decimal, ICollection<Product>>[] FindProductsByPriceRange(decimal from, decimal to)
- {
- return this.cart.Range(from, true, to, true).ToArray();
- }
- }
- public class ShoppingCenter
- {
- static string VisitShoppingCenter(int numberOfCommands)
- {
- StringBuilder result = new StringBuilder();
- int currentCommandNumber = 0;
- ShoppingCart myCart = new ShoppingCart();
- while (currentCommandNumber < numberOfCommands)
- {
- string inputCommand = Console.ReadLine();
- int indexOfFirstSpace = inputCommand.IndexOf(' ');
- StringBuilder command = new StringBuilder();
- StringBuilder parameters = new StringBuilder();
- command.Append(inputCommand.Substring(0,indexOfFirstSpace));
- parameters.Append(inputCommand.Substring(indexOfFirstSpace+1,inputCommand.Length-indexOfFirstSpace-1));
- string[] toDo = parameters.ToString().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
- switch (command.ToString())
- {
- case ("AddProduct"):
- {
- Product productToAdd = new Product(toDo[0].Trim(), Convert.ToDecimal(toDo[1].Trim()), toDo[2].Trim());
- result.AppendLine(myCart.AddProduct(productToAdd));
- }
- break;
- case ("DeleteProducts"):
- {
- int indexOfSeparator = inputCommand.IndexOf(';');
- if (indexOfSeparator < 0)
- {
- result.AppendLine(myCart.DeleteProducts(toDo[0].Trim()));
- }
- else
- {
- result.AppendLine(myCart.DeleteProducts(toDo[0].Trim(), toDo[1].Trim()));
- }
- }
- break;
- case ("FindProductsByName"):
- {
- var foundByName = myCart.FindProductsByName(toDo[0].Trim());
- if (foundByName.Count != 0)
- {
- foreach (var item in foundByName)
- {
- result.AppendLine(item.ToString());
- }
- }
- else
- {
- result.AppendLine(ShoppingCart.NO_PRODUCTS_FOUND_MSG);
- }
- }
- break;
- case ("FindProductsByProducer"):
- {
- var foundByProducer = myCart.FindProductsByProducer(toDo[0].Trim());
- if (foundByProducer.Count!=0)
- {
- foreach (var item in foundByProducer)
- {
- result.AppendLine(item.ToString());
- }
- }
- else
- {
- result.AppendLine(ShoppingCart.NO_PRODUCTS_FOUND_MSG);
- }
- }
- break;
- case ("FindProductsByPriceRange"):
- {
- var priceRange = myCart.FindProductsByPriceRange(Convert.ToDecimal(toDo[0].Trim()), Convert.ToDecimal(toDo[1].Trim()));
- if (priceRange.Length != 0)
- {
- foreach (var item in priceRange)
- {
- foreach (var val in item.Value)
- {
- result.AppendLine(val.ToString());
- }
- }
- }
- else
- {
- result.AppendLine(ShoppingCart.NO_PRODUCTS_FOUND_MSG);
- }
- }
- break;
- default:
- break;
- }
- currentCommandNumber++;
- }
- return result.ToString().TrimEnd();
- }
- static void Main(string[] args)
- {
- int numberOfCommandsToExecute = int.Parse(Console.ReadLine());
- string print = VisitShoppingCenter(numberOfCommandsToExecute);
- Console.WriteLine(print);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment