static void Main() { int commandsNumber = int.Parse(Console.ReadLine()); ShoppingCenter center = new ShoppingCenter(); for (int i = 0; i < commandsNumber; i++) { string line = Console.ReadLine(); int firstSpace = line.IndexOf(" "); string command = line.Substring(0, firstSpace); string[] tokens = line.Substring(firstSpace + 1).Split(';'); switch (command) { case "AddProduct": { string name = tokens[0]; double price = double.Parse(tokens[1]); string producer = tokens[2]; Product p = new Product(name, price, producer); center.Add(p); Console.WriteLine("Product added"); break; } case "DeleteProducts": if (tokens.Length == 1) { int count = center.DeleteProductsByProducer(tokens[0]); if (count == 0) { Console.WriteLine("No products found"); } else { Console.WriteLine(count.ToString() + " products deleted"); } } else { int count = center.DeleteProductsByProducerAndName( tokens[0], tokens[1]); if (count == 0) { Console.WriteLine("No products found"); } else { Console.WriteLine(count.ToString() + " products deleted"); } } break; case "FindProductsByName": List <Product> result = center.FindProductsByName(tokens[0]).ToList(); if (result.Count != 0) { Console.WriteLine(string.Join("\n", result)); } else { Console.WriteLine("No products found"); } break; case "FindProductsByProducer": List <Product> result2 = center.FindProductsByProducer(tokens[0]).ToList(); if (result2.Count != 0) { Console.WriteLine(string.Join("\n", result2)); } else { Console.WriteLine("No products found"); } break; case "FindProductsByPriceRange": IEnumerable <Product> result3 = center.FindProductsByPriceRange( double.Parse(tokens[0]), double.Parse(tokens[1])) .OrderBy(x => x); if (result3.Any()) { Console.WriteLine(string.Join("\n", result3)); } else { Console.WriteLine("No products found"); } break; } } }
static void Main() { int commandsNumber = int.Parse(Console.ReadLine()); ShoppingCenter shoppingCenter = new ShoppingCenter(); for (int i = 0; i < commandsNumber; i++) { string line = Console.ReadLine(); int firstSpace = line.IndexOf(" "); string command = line.Substring(0, firstSpace); string[] tokens = line.Substring(firstSpace + 1).Split(';'); switch (command) { case "AddProduct": string name = tokens[0]; double price = double.Parse(tokens[1]); string producer = tokens[2]; Product p = new Product(name, price, producer); shoppingCenter.Add(p); Console.WriteLine("Product added"); break; case "DeleteProducts": if (tokens.Length == 1) { producer = tokens[0]; int countDeleted = shoppingCenter.DeleteProductsByProducer(producer); if (countDeleted == 0) { Console.WriteLine("No products found"); } else { Console.WriteLine(countDeleted.ToString() + " products deleted"); } } else { name = tokens[0]; producer = tokens[1]; int countDeleted = shoppingCenter.DeleteProductsByProducerAndName(name, producer); if (countDeleted == 0) { Console.WriteLine("No products found"); } else { Console.WriteLine(countDeleted.ToString() + " products deleted"); } } break; case "FindProductsByName": name = tokens[0]; List <Product> result = shoppingCenter.FindProductsByName(name).ToList(); if (result.Count != 0) { Console.WriteLine(string.Join(Environment.NewLine, result)); } else { Console.WriteLine("No products found"); } break; case "FindProductsByProducer": producer = tokens[0]; result = shoppingCenter.FindProductsByProducer(producer).ToList(); if (result.Count != 0) { Console.WriteLine(string.Join(Environment.NewLine, result)); } else { Console.WriteLine("No products found"); } break; case "FindProductsByPriceRange": double priceLow = double.Parse(tokens[0]); double priceHigh = double.Parse(tokens[1]); result = shoppingCenter.FindProductsByPriceRange(priceLow, priceHigh) .ToList(); if (result.Count != 0) { Console.WriteLine(string.Join(Environment.NewLine, result)); } else { Console.WriteLine("No products found"); } break; } } }
static void Main(string[] args) { int numberOfCommands = int.Parse(Console.ReadLine()); ShoppingCenter shoppingCenter = new ShoppingCenter(); output = new StringBuilder(); for (int i = 0; i < numberOfCommands; i++) { string commandLine = Console.ReadLine(); int indexFirstInterval = commandLine.IndexOf(' '); string command = commandLine.Substring(0, indexFirstInterval); string commandParametersString = commandLine.Substring(indexFirstInterval + 1); string[] commandParameters = commandParametersString.Split(new char[] { ';' }); switch (command) { case "AddProduct": shoppingCenter.Add(commandParameters[0], decimal.Parse(commandParameters[1]), commandParameters[2]); output.AppendLine("Product added"); break; case "FindProductsByName": Product[] findByNameResult = shoppingCenter.FindProductByName(commandParameters[0]); Array.Sort(findByNameResult); PrintProductArray(findByNameResult); break; case "FindProductsByPriceRange": Product[] findByPriceResult = shoppingCenter.FindProductByPriceRange(decimal.Parse(commandParameters[0]), decimal.Parse(commandParameters[1])); Array.Sort(findByPriceResult); PrintProductArray(findByPriceResult); break; case "FindProductsByProducer": Product[] findByProducerResult = shoppingCenter.FindProductByProducer(commandParameters[0]); Array.Sort(findByProducerResult); PrintProductArray(findByProducerResult); break; case "DeleteProducts": int deletedProducts; if (commandParameters.Length == 2) { deletedProducts = shoppingCenter.DeleteProduct(commandParameters[0], commandParameters[1]); } else { deletedProducts = shoppingCenter.DeleteProduct(commandParameters[0]); } if (deletedProducts == 0) { output.AppendLine("No products found"); } else { output.AppendLine(deletedProducts + " products deleted"); } break; default: break; } } Console.WriteLine(output.ToString().Trim()); }
public static void Main() { int commandsNumber = int.Parse(Console.ReadLine()); ShoppingCenter center = new ShoppingCenter(); for (int i = 0; i < commandsNumber; i++) { string line = Console.ReadLine(); int firstSpace = line.IndexOf(" "); string command = line.Substring(0, firstSpace); string[] args = line.Substring(firstSpace + 1).Split(';'); List <Product> result = new List <Product>(); switch (command) { case "AddProduct": string name = args[0]; double price = double.Parse(args[1]); string producer = args[2]; Product p = new Product(name, price, producer); center.Add(p); Console.WriteLine("Product added"); break; case "DeleteProducts": int count = 0; if (args.Length == 1) // delete producer { count = center.DeleteProductsByProducer(args[0]); } else { count = center.DeleteProductsByProducerAndName( args[0], args[1]); } if (count == 0) { Console.WriteLine("No products found"); } else { Console.WriteLine(count + " products deleted"); } break; case "FindProductsByName": result = center.FindProductsByName(args[0]).ToList(); break; case "FindProductsByProducer": result = center.FindProductsByProducer(args[0]).ToList(); break; case "FindProductsByPriceRange": result = center.FindProductsByPriceRange( double.Parse(args[0]), double.Parse(args[1])) .OrderBy(x => x) .ToList(); break; } if (command.StartsWith("Find")) { if (result.Count != 0) { Console.WriteLine(string.Join("\n", result)); } else { Console.WriteLine("No products found"); } } } }