static void Main(string[] args) { ShoppingService shoppingService = new ShoppingService(); ProductService productService = new ProductService(); ShoppingCart cart = new ShoppingCart(); Printer printer = new Printer(); bool canExit = false; do { Console.WriteLine("--------"); Console.WriteLine("please type your command:"); Console.WriteLine("example:"); Console.WriteLine("get all products"); Console.WriteLine("add product {id}"); Console.WriteLine("remove product {id}"); Console.WriteLine("check shoppingcart"); Console.WriteLine("checkout"); Console.WriteLine("q to exit app."); Console.WriteLine("--------"); var command = Console.ReadLine().ToLowerInvariant(); var commandSplits = command.Split(' '); if (command.StartsWith("get all products")) { var products = productService.GetAllProducts(); foreach (var item in products) { Console.WriteLine(item); } continue; } if (command.StartsWith("add product")) { var id = int.Parse(commandSplits[2]); shoppingService.AddShoppingItem(cart, id); shoppingService.CheckShoppingCart(cart); continue; } if (command.StartsWith("remove product")) { var id = int.Parse(commandSplits[2]); shoppingService.RemoveShoppingItem(cart, id); shoppingService.CheckShoppingCart(cart); continue; } if (command.StartsWith("check shoppingcart")) { shoppingService.CheckShoppingCart(cart); continue; } if (command.StartsWith("checkout")) { var receipt = shoppingService.Checkout(cart); printer.PrintReceipt(receipt); canExit = true; continue; } if (command == "q") { canExit = true; continue; } Console.WriteLine("Wrong command, please try again."); } while (!canExit); Console.WriteLine("Bye."); Console.ReadLine(); }
static void Main(string[] args) { ProductService productService = new ProductService(); ShoppingService shoppingService = new ShoppingService(); ShoppingCart shoppingCart = new ShoppingCart(); string command = ""; do { Console.WriteLine("============ Product Admin ============="); Console.WriteLine("Please type your command:"); Console.WriteLine("Add to cart id quantity"); Console.WriteLine("Remove from cart id quantity"); Console.WriteLine("Check shoppingCart"); Console.WriteLine("Checkout"); Console.WriteLine("Find product id"); Console.WriteLine("List all products"); Console.WriteLine("quit"); Console.WriteLine("-----------------------------------------"); command = Console.ReadLine().ToLowerInvariant(); var commandSplit = command.Split(' '); if (command.StartsWith("add to cart")) { try { var id = int.Parse(commandSplit[3]); var quantity = int.Parse(commandSplit[4]); var shoppingItem = shoppingService.AddShoppingItem(shoppingCart, id, quantity); Console.WriteLine(shoppingItem); } catch (Exception e) { Console.WriteLine(e.Message); } continue; } if (command.StartsWith("remove from cart")) { try { var id = int.Parse(commandSplit[3]); var quantity = int.Parse(commandSplit[4]); var shoppingItem = shoppingService.RemoveFromCart(shoppingCart, id, quantity); if (shoppingItem != null) { Console.WriteLine(shoppingItem); } } catch (Exception e) { Console.WriteLine(e.Message); } continue; } if (command.StartsWith("check shoppingcart")) { Console.WriteLine(shoppingService.CheckShoppingCart(shoppingCart)); continue; } if (command.StartsWith("checkout")) { Receipt receipt = shoppingService.Checkout(shoppingCart); shoppingService.SaveReceipt(receipt); Console.WriteLine(receipt.ToString()); continue; } if (command.StartsWith("find product")) { var id = int.Parse(commandSplit[2]); var product = productService.GetPRoductById(id); if (product == null) { Console.WriteLine($"Product id={id} does not exist"); } else { Console.WriteLine("Found this product:"); Console.WriteLine(product); } continue; } if (command.StartsWith("list all product")) { Console.WriteLine(productService.ListAllProducts()); continue; } } while (command != "quit"); }