private static void AddProduct(ShoppingCenter shoppingCenter, string line, string[] tokens) { var productTokens = SplitLine(line); var name = productTokens[0].Substring(productTokens[0].IndexOf(" ") + 1); shoppingCenter.AddProduct(name, decimal.Parse(productTokens[1]), productTokens[2]); Console.WriteLine("Product added"); }
private static void AddProduct(ShoppingCenter shoppingCenter, string[] args) { if (args.Length < 3) { return; } var name = args[0]; var price = decimal.Parse(args[1]); var producer = args[2]; var product = new Product(name, price, producer); shoppingCenter.AddProduct(product); Console.WriteLine("Product added"); }
static void Main(string[] args) { var lines = int.Parse(Console.ReadLine()); var center = new ShoppingCenter(); for (int i = 0; i < lines; i++) { var input = Console.ReadLine(); var index = input.IndexOf(' '); var command = input.Substring(0, index); var cmdArgs = input.Substring(index + 1).Split(';'); switch (command) { case "AddProduct": Console.WriteLine(center.AddProduct(cmdArgs[0], decimal.Parse(cmdArgs[1]), cmdArgs[2])); break; case "DeleteProducts": Console.WriteLine( cmdArgs.Length == 1 ? center.DeleteProducts(cmdArgs[0]) : center.DeleteProducts(cmdArgs[0], cmdArgs[1])); break; case "FindProductsByName": Console.Write(center.FindProductsByName(cmdArgs[0])); break; case "FindProductsByProducer": Console.Write(center.FindProductsByProducer(cmdArgs[0])); break; case "FindProductsByPriceRange": Console.Write(center.FindProductsByPriceRange(decimal.Parse(cmdArgs[0]), decimal.Parse(cmdArgs[1]))); break; default: throw new ArgumentException(); } } }
public static void Main() { #if DEBUG Console.SetIn(new StreamReader("../../input.txt")); Debug.Listeners.Add(new ConsoleTraceListener()); #endif var date = DateTime.Now; var separator = new[] { ' ' }; var shoppingCenter = new ShoppingCenter(); foreach (int i in Enumerable.Range(0, int.Parse(Console.ReadLine()))) { var line = Console.ReadLine(); var splitted = line.Split(separator, 2); var name = splitted[0]; var parameters = splitted[1].Split(';'); #if DEBUG Output.AppendLine(); Output.AppendLine(line); #endif switch (name) { case "AddProduct": { var product = new Product( name: parameters[0], price: decimal.Parse(parameters[1]), producer: parameters[2] ); shoppingCenter.AddProduct(product); Output.AppendLine("Product added"); break; } case "FindProductsByName": { var products = shoppingCenter.FindProductsByName(parameters[0]); PrintProducts(products); break; } case "FindProductsByProducer": { var products = shoppingCenter.FindProductsByProducer(parameters[0]); PrintProducts(products); break; } case "FindProductsByPriceRange": { var products = shoppingCenter.FindProductsByPriceRange( min: decimal.Parse(parameters[0]), max: decimal.Parse(parameters[1]) ); PrintProducts(products); break; } case "DeleteProducts": { int deletedCount; switch (parameters.Length) { case 1: deletedCount = shoppingCenter.DeleteProducts( producer: parameters[0] ); break; case 2: deletedCount = shoppingCenter.DeleteProducts( name: parameters[0], producer: parameters[1] ); break; default: throw new ArgumentException("DeleteProducts"); } if (deletedCount == 0) { Output.AppendLine("No products found"); break; } Output.AppendLine(deletedCount + " products deleted"); break; } default: throw new ArgumentException("Invalid command: " + name); } } #if !DEBUG || DEBUG Console.Write(Output); #endif Debug.WriteLine(DateTime.Now - date); }
public static void Main() { ShoppingCenter shoppingCenter = new ShoppingCenter(); int numberOfCommands = int.Parse(Console.ReadLine()); for (int i = 0; i < numberOfCommands; i++) { string commandLine = Console.ReadLine(); string command = commandLine.Remove(commandLine.IndexOf(' ')); string[] items = commandLine .Substring(command.Length + 1) .Split(';'); switch (command) { case "AddProduct": Product product = new Product(items[0], double.Parse(items[1], CultureInfo.InvariantCulture), items[2]); shoppingCenter.AddProduct(product); Console.WriteLine("Product added"); break; case "DeleteProducts": int count = 0; if (items.Length == 2) { count = shoppingCenter.DeleteProducts(items[0], items[1]); } else if (items.Length == 1) { count = shoppingCenter.DeleteProducts(items[0]); } string resultToPrint = count > 0 ? $"{count} products deleted" : "No products found"; Console.WriteLine(resultToPrint); break; case "FindProductsByName": IEnumerable <Product> findProducts = shoppingCenter .FindProductsByName(items[0]) .ToList(); if (findProducts.Count() == 0) { Console.WriteLine("No products found"); } else { Console.WriteLine(string.Join("\n", findProducts)); } break; case "FindProductsByProducer": IEnumerable <Product> findProductsByProducer = shoppingCenter .FindProductsByProducer(items[0]) .ToList(); if (findProductsByProducer.Count() == 0) { Console.WriteLine("No products found"); } else { Console.WriteLine(string.Join("\n", findProductsByProducer)); } break; case "FindProductsByPriceRange": IEnumerable <Product> findInRange = shoppingCenter.FindProductsByPriceRange( double.Parse(items[0], CultureInfo.InvariantCulture), double.Parse(items[1], CultureInfo.InvariantCulture)) .ToList(); if (findInRange.Count() == 0) { Console.WriteLine("No products found"); } else { Console.WriteLine(string.Join("\n", findInRange)); } break; } } }
private static void AddProduct(string[] elements, ShoppingCenter shoppingCenter) { shoppingCenter.AddProduct(elements[0], decimal.Parse(elements[1]), elements[2]); Console.WriteLine("Product added"); }
public static void Main() { var sc = new ShoppingCenter(); var commands = int.Parse(Console.ReadLine()); for (int i = 0; i < commands; 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 addName = tokens[0]; double addPrice = double.Parse(tokens[1]); string addProducer = tokens[2]; sc.AddProduct(addName, addPrice, addProducer); Console.WriteLine("Product added"); break; } case "DeleteProducts": { int count = 0; if (tokens.Length == 1) { string currProducer = tokens[0]; count = sc.DeleteProductsByProducer(currProducer); } else { string deleteName = tokens[0]; string deleteProducer = tokens[1]; count = sc.DeleteProductsByProducerAndName(deleteName, deleteProducer); } PrintRemoveMessage(count); break; } case "FindProductsByName": { string findByName = tokens[0]; PrintProducts(sc.FindProductsByName(findByName).ToList()); break; } case "FindProductsByProducer": { string findByProd = tokens[0]; PrintProducts(sc.FindProductsByProducer(findByProd).ToList()); break; } case "FindProductsByPriceRange": { double lowerRange = double.Parse(tokens[0]); double upperRange = double.Parse(tokens[1]); PrintProducts(sc.FindProductsByPriceRange(lowerRange, upperRange).OrderBy(x => x).ToList()); break; } } } }
public static void Main() { ShoppingCenter center = new ShoppingCenter(); int commandsCount = int.Parse(Console.ReadLine()); for (int i = 0; i < commandsCount; i++) { string line = Console.ReadLine(); string command = line.Split(' ').ToArray()[0]; line = line.Substring(line.IndexOf(' ') + 1); switch (command) { case "AddProduct": string[] tokens = line.Split(';').ToArray(); Product p = new Product(tokens[0], double.Parse(tokens[1]), tokens[2]); center.AddProduct(p); Console.WriteLine("Product added"); break; case "FindProductsByName": List <Product> foundByName = center.FindProductsByName(line).ToList(); if (foundByName.Count == 0) { Console.WriteLine("No products found"); } else { Console.WriteLine(string.Join("\n", foundByName)); } break; case "FindProductsByProducer": List <Product> foundByProducer = center.FindProductsByProducer(line).ToList(); if (foundByProducer.Count == 0) { Console.WriteLine("No products found"); } else { Console.WriteLine(string.Join("\n", foundByProducer)); } break; case "FindProductsByPriceRange": string[] prices = line.Split(';').ToArray(); double price1 = double.Parse(prices[0]); double price2 = double.Parse(prices[1]); List <Product> found = center.FindProductsByPriceRange(price1, price2).ToList(); if (found.Count == 0) { Console.WriteLine("No products found"); } else { Console.WriteLine(string.Join("\n", found)); } break; case "DeleteProducts": if (line.Contains(';')) { string[] args = line.Split(';').ToArray(); int deletedProducts = center.DeleteProducts(args[0], args[1]); if (deletedProducts == 0) { Console.WriteLine("No products found"); } else { Console.WriteLine($"{deletedProducts} products deleted"); } } else { int deletedProducts = center.DeleteProducts(line); if (deletedProducts == 0) { Console.WriteLine("No products found"); } else { Console.WriteLine($"{deletedProducts} products deleted"); } } break; } } }
public static void Main() { var shoppingCenter = new ShoppingCenter(); var n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { var input = Console.ReadLine(); var command = input.Split()[0]; var tokens = input.Replace(command, "").Trim().Split(';'); switch (command) { case "AddProduct": shoppingCenter.AddProduct(tokens[0], decimal.Parse(tokens[1]), tokens[2]); Console.WriteLine("Product added"); break; case "DeleteProducts": var count = 0; if (tokens.Length == 1) { count = shoppingCenter.DeleteProducts(tokens[0]); } else if (tokens.Length == 2) { count = shoppingCenter.DeleteProducts(tokens[0], tokens[1]); } if (count == 0) { Console.WriteLine("No products found"); } else { Console.WriteLine($"{count} products deleted"); } break; case "FindProductsByName": var result = shoppingCenter.FindProductsByName(tokens[0]); if (!result.Any()) { Console.WriteLine("No products found"); } else { Console.WriteLine(string.Join(Environment.NewLine, result)); } break; case "FindProductsByProducer": var result2 = shoppingCenter.FindProductsByProducer(tokens[0]); if (!result2.Any()) { Console.WriteLine("No products found"); } else { Console.WriteLine(string.Join(Environment.NewLine, result2)); } break; case "FindProductsByPriceRange": var result3 = shoppingCenter.FindProductsByPriceRange(decimal.Parse(tokens[0]), decimal.Parse(tokens[1])); if (!result3.Any()) { Console.WriteLine("No products found"); } else { Console.WriteLine(string.Join(Environment.NewLine, result3)); } break; } } }