/// <summary> /// Write a list of products to console with optional search-by-name. /// </summary> public static void DisplayProducts(IProject0Repository repository) { Console.Write(" Enter a product name to search for? (Y/N) : "); string doSearch = Console.ReadLine().ToLower(); string search = null; if (doSearch == "y" || doSearch == "yes") { Console.Write(" "); search = Console.ReadLine(); } Console.WriteLine(); IEnumerable <IProduct> products = repository.GetProducts(search); foreach (IProduct p in products) { Console.WriteLine(p.Name + " " + p.BestBy + " " + p.UnitPrice); } Console.WriteLine(); Console.Write(" Press Enter to continue"); Console.ReadLine(); Console.WriteLine(); }
/// <summary> /// Get new order information from user and add to system. /// </summary> public static void AddOrder(IProject0Repository repository) { List <Products.IProduct> products = new List <Products.IProduct>(repository.GetProducts()); // Get CustomerId int customerId = -1; do { Console.Write(" Enter CustomerId: "); try { customerId = int.Parse(Console.ReadLine()); } catch { customerId = -1; } } while (customerId < 0); // Get LocationId int locationId = -1; do { Console.Write(" Enter LocationId: "); try { locationId = int.Parse(Console.ReadLine()); } catch { locationId = -1; } } while (locationId < 0); // Get OrderTime DateTime orderTime = DateTime.Now; // Get OrderLines List <Orders.OrderLine> orderLines = new List <Orders.OrderLine>(); int count = 0; bool done = false; do { // Get OrderLine ProductId int productId = -1; do { Console.Write(" Enter ProductId: "); try { productId = int.Parse(Console.ReadLine()); } catch { productId = -1; } } while (productId < 0); // Get OrderLine Quantity int quantity = -1; do { Console.Write(" Enter Quantity: "); try { quantity = int.Parse(Console.ReadLine()); } catch { quantity = -1; } } while (quantity < 1); orderLines.Add(new Orders.OrderLine { Product = products[productId], Quantity = quantity }); count++; Console.Write(" Enter another OrderLine? (Y/N) : "); string check = Console.ReadLine().ToLower(); if (check == "y" || check == "yes") { done = false; } else { done = true; } } while (count < 1 || done == false); IOrder order = new Project0.Orders.Order { CustomerId = customerId, LocationId = locationId, OrderTime = orderTime, OrderLines = orderLines }; repository.AddOrder(order); Console.WriteLine(); }
static void MakeOrder(Customer customer, IProject0Repository projectRepository) { _io.Output("Choose a location: \n"); //display all locations var locations = projectRepository.GetLocations(); var locationList = locations.ToList <StoreLocation>(); int locationId = 0; while (locationId == 0) { foreach (var item in locationList) { _io.Output(item.LocationName + "\n"); } string locationInput = _io.Input(); try { //get store id locationId = locations.First(l => l.LocationName.ToLower() == locationInput).Id; } catch (ArgumentException ex) { _io.Output("Store name input invalid."); s_logger.Info(ex); Console.WriteLine(ex.Message); } } //display available products _io.Output("Choose a product: \n"); var products = projectRepository.GetProducts(); var productList = products.Where(p => p.StoreLocationId == locationId && p.Stock > 0).ToList <Product>(); var productObject = new Product(); while (productObject.Name == null) { foreach (var item in productList) { _io.Output($"Name: {item.Name} Stock: {item.Stock} Price: {item.Price} \n"); } var productInput = _io.Input(); try { productObject = productList.First(p => p.Name.ToLower() == productInput); } catch (Exception ex) { _io.Output("Product name or Quantity input invalid. "); s_logger.Info(ex); Console.WriteLine(ex.Message); } } var quantityInput = 0; var quantityBool = true; while (quantityBool) { _io.Output("Choose a quantity: \n"); quantityInput = Int32.Parse(_io.Input()); if (quantityInput > productObject.Stock) { _io.Output("Quantity too large. \n"); } else { quantityBool = false; } } productObject.Stock = productObject.Stock - quantityInput; projectRepository.UpdateProduct(productObject); DateTime timeStamp = DateTime.Now; var newOrder = new Orders { StoreLocationId = locationId, CustomerId = customer.Id, ProductId = productObject.Id, Quantity = quantityInput }; projectRepository.AddOrder(newOrder); projectRepository.Save(); _io.Output("Order Complete."); }