/// <summary> /// Handles user input. /// </summary> public void InputLoop() { do { Console.Write("Location name: "); var name = Console.ReadLine(); if (name.Trim() == "") { this.AbortThenExit("No name provided."); return; } var location = new StoreDb.Location(name); var added = this.ApplicationState.DbOptions.AddLocation(location); if (!added) { CliPrinter.Error("A location with that name already exists."); continue; } this.MenuExit(); CliInput.PressAnyKey("Location added."); break; } while (true); }
/// <summary> /// Handle input. /// </summary> public void InputLoop() { var cliOptions = CliInput.GetLineOptions.TrimSpaces | CliInput.GetLineOptions.AcceptEmpty; do { var login = CliInput.GetLine(cliOptions, CliInput.EmailValidator, "Email:"); if (login == "" || login == null) { this.MenuExit(); return; } var password = CliInput.GetPassword("Password:"******"Invalid login."); } } while (true); this.ApplicationState.UserData.RefreshDefaultLocation(); }
/// <summary> /// Handle the input when selecting a location to query for orders. /// </summary> /// <returns>A <c>HandlerMsg</c> indicating what action should be taken.</returns> private HandlerMsg HandleSelectLocationInput() { var inputOptions = CliInput.GetLineOptions.TrimSpaces | CliInput.GetLineOptions.AcceptEmpty; do { var line = CliInput.GetLine(inputOptions, v => true, "Select location number:"); if (line == "" || line == null) { return(HandlerMsg.Exit); } try { var locationNumber = Int32.Parse(line); if (locationNumber > 0 && locationNumber <= this.LocationIds.Count) { this.SelectedLocation = this.LocationIds[locationNumber - 1]; this.CurrentOperatingMode = OperatingMode.ViewOrders; return(HandlerMsg.Continue); } } catch { CliPrinter.Error("Please enter a valid location number."); } } while (true); }
/// <summary> /// Handle the input when viewing a list of all orders for a location. /// </summary> /// <returns>A <c>HandlerMsg</c> indicating what action should be taken.</returns> private HandlerMsg HandleViewOrderInput() { var inputOptions = CliInput.GetLineOptions.TrimSpaces | CliInput.GetLineOptions.AcceptEmpty; do { var line = CliInput.GetLine(inputOptions, v => true, "\nSort by [D]ate / [P]rice\nor enter an order number to view details:"); switch (line) { case "D": case "d": this.SortKey = this.SortKey != OrderSortKey.DateDesc ? OrderSortKey.DateDesc : OrderSortKey.DateAsc; break; case "P": case "p": this.SortKey = this.SortKey != OrderSortKey.PriceDesc ? OrderSortKey.PriceDesc : OrderSortKey.PriceAsc; break; default: if (line == "" || line == null) { this.CurrentOperatingMode = OperatingMode.SelectLocation; return(HandlerMsg.Continue); } try { var orderNum = Int32.Parse(line); if (orderNum > 0 && orderNum <= this.OrderIds.Count) { using (var db = new StoreContext(this.ApplicationState.DbOptions)) { var order = db.GetOrderById(this.OrderIds[orderNum - 1]); var amountCharged = db.GetAmountCharged(order); this.DisplayDetail(db, order, amountCharged); break; } } else { CliPrinter.Error("Invalid order number"); continue; } } catch (Exception) { // We will just ignore parse errors and reprint the menu. break; } } this.PrintMenu(); } while (true); }
/// <summary> /// Handle menu input. /// </summary> public void InputLoop() { Console.WriteLine("Select store for orders:"); var numLocations = 0; IEnumerable <Guid> locationIds = null; using (var db = new StoreContext(this.ApplicationState.DbOptions)) { var locations = db.GetLocations(); var locationsIter = Enumerable.Range(1, locations.Count()).Zip(locations); foreach (var location in locationsIter) { Console.WriteLine($" {location.First}. {location.Second.Name}"); } numLocations = locationsIter.Count(); locationIds = locationsIter.Select(i => i.Second.LocationId).ToList(); } bool validator(int num) { if (num > 0 && num <= numLocations) { return(true); } else { CliPrinter.Error($"Please enter a store number between 1 and {numLocations}"); return(false); } } var locationSelected = CliInput.GetInt(0, validator, "Enter store number:"); var customerId = this.ApplicationState.UserData.CustomerId; var locationId = locationIds.ElementAt(locationSelected - 1 ?? 0); if (this.ApplicationState.DbOptions.SetDefaultLocation(customerId, locationId)) { CliInput.PressAnyKey("Store set."); this.ApplicationState.UserData.OperatingLocationId = locationId; this.ApplicationState.UserData.RefreshDefaultLocation(); this.MenuExit(); return; } else { CliInput.PressAnyKey("An error occurred while setting the store. Please try again."); } }
/// <summary> /// Print this menu. /// </summary> public void PrintMenu() { Console.Clear(); CliPrinter.Title("Order Items"); this.ApplicationState.UserData.RefreshCurrentOrder(); if (this.ApplicationState.UserData.CurrentOrderId == null) { using (var db = new StoreContext(this.ApplicationState.DbOptions)) { var customer = db.GetCustomerById(this.ApplicationState.UserData.CustomerId); var location = db.GetLocationById(this.ApplicationState.UserData.OperatingLocationId); var order = new Order(customer, location); order.Customer = customer; db.Add(order); db.SaveChanges(); this.ApplicationState.UserData.CurrentOrderId = order.OrderId; } } using (var db = new StoreContext(this.ApplicationState.DbOptions)) { var inventory = db.GetProductsAvailable(this.ApplicationState.UserData.OperatingLocationId); var order = db.GetOrderById(this.ApplicationState.UserData.CurrentOrderId); var i = 1; this.InventoryIds = inventory.Select(i => i.LocationInventoryId).ToList(); Console.WriteLine("#\tStock\tName"); foreach (var stock in inventory) { var projectedQuantity = db.ProjectStockBasedOnOrder(order, stock.Product); if (projectedQuantity < 0) { projectedQuantity = 0; } Console.WriteLine($"{i}.\t{projectedQuantity}\t{stock.Product.Name}"); i += 1; } Console.Write("\n----------------------------------------------\n"); } }
/// <summary> /// Print this menu. /// </summary> public void PrintMenu() { Console.Clear(); CliPrinter.Title("Edit Order"); this.ApplicationState.UserData.RefreshCurrentOrder(); this.LineItemIds.Clear(); using (var db = new StoreContext(this.ApplicationState.DbOptions)) { var order = db.GetOrderById(this.ApplicationState.UserData.CurrentOrderId); if (order == null) { return; } if (order.OrderLineItems.Count() == 0) { return; } var itemNameDividerSize = order.OrderLineItems.Max(li => li.Product.Name.Length); Console.WriteLine("#\tQty\tEach\tTotal\tName"); Console.WriteLine($"===\t===\t=====\t=====\t{new string('=', itemNameDividerSize)}"); double orderTotalCost = 0.0; int orderTotalUnits = 0; foreach (var o in order.OrderLineItems) { this.LineItemIds.Add(o.OrderLineItemId); var lineItemTotal = o.Product.Price * o.Quantity; orderTotalCost += lineItemTotal; orderTotalUnits += o.Quantity; Console.WriteLine($"{this.LineItemIds.Count}\t{o.Quantity}\t${o.Product.Price}\t${lineItemTotal}\t{o.Product.Name}"); } Console.WriteLine($"---\t-----\t-----\t-----\t{new string('-', itemNameDividerSize)}"); Console.Write($"\t{orderTotalUnits}\t\t${orderTotalCost}\tTotal\n\n"); } }
/// <summary> /// Print this menu. /// </summary> public void PrintMenu() { Console.Clear(); CliPrinter.Title("Find Customer"); this.DisplaySearchTerm(this.SearchQuery); Console.SetCursorPosition(0, SearchResultTerminalRow); DisplaySearchTerm(this.SearchQuery); ClearResults(); Console.SetCursorPosition(0, SearchResultTerminalRow); Console.WriteLine("============================="); if (this.SearchQuery.Length == 0) { return; } var maxResults = Console.WindowHeight - SearchResultTerminalRow - 4; using (var db = new StoreDb.StoreContext(this.ApplicationState.DbOptions)) { var customers = db.FindCustomerByName(this.SearchQuery); var displayAmount = customers.Count() > maxResults ? maxResults : customers.Count(); Console.Write($"{customers.Count()} customers found. Displaying {displayAmount}.\n\n"); foreach (var customer in customers.Take(displayAmount).OrderBy(c => c.FirstName)) { var phoneNumber = customer.PhoneNumber; if (phoneNumber == null || phoneNumber == "") { phoneNumber = " "; } Console.WriteLine($"{phoneNumber}: {customer.FirstName} {customer.LastName}"); } } }
/// <summary> /// Print this menu. /// </summary> public void PrintMenu() { Console.Clear(); CliPrinter.Title("Review Order"); this.ApplicationState.UserData.RefreshCurrentOrder(); }
/// <summary> /// Print this menu /// </summary> public void PrintMenu() { Console.Clear(); CliPrinter.Title("Login"); }
/// <summary> /// Handle user input. /// </summary> public void InputLoop() { string EmailLoop(DbContextOptions <StoreContext> dbOptions) { var cliOptions = CliInput.GetLineOptions.TrimSpaces | CliInput.GetLineOptions.AcceptEmpty; string login = ""; do { login = CliInput.GetLine(cliOptions, CliInput.EmailValidator, "Email Address:"); if (login == "" || login == null) { return(null); } if (dbOptions.LoginExists(login)) { CliPrinter.Error("That email address is already in use."); } else { break; } } while (true); return(login); } do { var cliOptions = CliInput.GetLineOptions.TrimSpaces | CliInput.GetLineOptions.AcceptEmpty; var firstName = CliInput.GetLine(cliOptions, Customer.ValidateName, "First Name:"); if (firstName == "" || firstName == null) { this.AbortThenExit("Empty entry - exiting."); return; } var lastName = CliInput.GetLine(cliOptions, Customer.ValidateName, "Last Name:"); if (lastName == "" || lastName == null) { this.AbortThenExit("Empty entry - exiting."); return; } var login = EmailLoop(this.ApplicationState.DbOptions); if (login == "" || login == null) { this.AbortThenExit("Empty entry - exiting."); return; } var password = CliInput.GetPassword("Password:"******"") { this.AbortThenExit("Empty entry - exiting."); return; } var customer = new Customer(); // Data pre-validated above. customer.FirstName = firstName; customer.LastName = lastName; customer.Login = login; customer.Password = password; var createResult = this.ApplicationState.DbOptions.CreateUserAccount(customer); if (createResult == CreateUserAccountResult.Ok) { this.ApplicationState.UserData.CustomerId = customer.CustomerId; this.MenuExit(); this.MenuAdd(new StoreCliMenuUser.Main(this.ApplicationState)); CliInput.PressAnyKey("\nAccount created."); break; } else { CliPrinter.Error("An error occurred while creating your account. Please try again."); } } while (true); }
/// <summary> /// Print this menu. /// </summary> public void PrintMenu() { Console.Clear(); CliPrinter.Title("Order History"); switch (this.CurrentOperatingMode) { case OperatingMode.SelectLocation: using (var db = new StoreContext(this.ApplicationState.DbOptions)) { var locations = db.GetLocations(); this.LocationIds.Clear(); var i = 1; foreach (var location in locations) { this.LocationIds.Add(location.LocationId); Console.WriteLine($"{i}. {location.Name}"); i += 1; } Console.WriteLine("\n"); } break; case OperatingMode.ViewOrders: using (var db = new StoreContext(this.ApplicationState.DbOptions)) { var location = db.GetLocationById(this.SelectedLocation); if (location == null) { break; } var orders = db .GetOrderHistory(location) .Select(o => new { OrderId = o.OrderId, Customer = o.Customer, Location = o.Location, TimeCreated = o.TimeCreated, TimeSubmitted = o.TimeSubmitted, TimeFulfilled = o.TimeFulfilled, AmountPaid = o.AmountPaid, OrderLineItem = o.OrderLineItems, AmountCharged = db.GetAmountCharged(o), }).ToList(); if (orders.Count() == 0) { CliPrinter.Error("There are no orders for this location."); this.CurrentOperatingMode = OperatingMode.SelectLocation; CliInput.PressAnyKey(); this.PrintMenu(); return; } var upArrow = '↑'; var downArrow = '↓'; var priceSortSymbol = '-'; var dateSortSymbol = '-'; switch (this.SortKey) { case OrderSortKey.DateDesc: orders = orders.OrderByDescending(o => o.TimeSubmitted).ToList(); dateSortSymbol = downArrow; break; case OrderSortKey.DateAsc: orders = orders.OrderBy(o => o.TimeSubmitted).ToList(); dateSortSymbol = upArrow; break; case OrderSortKey.PriceDesc: orders = orders.OrderByDescending(o => o.AmountCharged).ToList(); priceSortSymbol = downArrow; break; case OrderSortKey.PriceAsc: orders = orders.OrderBy(o => o.AmountCharged).ToList(); priceSortSymbol = upArrow; break; } var historyDisplayAlignment = "{0,-6}{1,-9}{2,-25}"; var priceSortLine = $"{priceSortSymbol}----"; var dateSortLine = $"{dateSortSymbol}---"; Console.WriteLine(historyDisplayAlignment, "Num", "Price", "Date"); Console.WriteLine(historyDisplayAlignment, "---", priceSortLine, dateSortLine); var i = 1; this.OrderIds.Clear(); foreach (var order in orders) { this.OrderIds.Add(order.OrderId); Console.WriteLine(historyDisplayAlignment, i + ".", "$" + order.AmountCharged, order.TimeSubmitted, order.Location.Name); i += 1; } } break; default: break; } }
/// <summary> /// Prints this menu. /// </summary> public void PrintMenu() { Console.Clear(); CliPrinter.Title("Add New Location"); }
/// <summary> /// Print this menu. /// </summary> public void PrintMenu() { Console.Clear(); CliPrinter.Title("Add new customer"); }
/// <summary> /// Print this menu. /// </summary> public void PrintMenu() { Console.Clear(); CliPrinter.Title("Order History"); using (var db = new StoreContext(this.ApplicationState.DbOptions)) { var orders = db .GetOrderHistory(this.ApplicationState.UserData.CustomerId) .Select(o => new { OrderId = o.OrderId, Customer = o.Customer, Location = o.Location, TimeCreated = o.TimeCreated, TimeSubmitted = o.TimeSubmitted, TimeFulfilled = o.TimeFulfilled, AmountPaid = o.AmountPaid, OrderLineItem = o.OrderLineItems, AmountCharged = (double)db.GetAmountCharged(o), }).ToList(); var upArrow = '↑'; var downArrow = '↓'; var priceSortSymbol = '-'; var dateSortSymbol = '-'; var locationSortSymbol = '-'; switch (this.SortKey) { case OrderSortKey.DateDesc: orders = orders.OrderByDescending(o => o.TimeSubmitted).ToList(); dateSortSymbol = downArrow; break; case OrderSortKey.DateAsc: orders = orders.OrderBy(o => o.TimeSubmitted).ToList(); dateSortSymbol = upArrow; break; case OrderSortKey.PriceDesc: orders = orders.OrderByDescending(o => o.AmountCharged).ToList(); priceSortSymbol = downArrow; break; case OrderSortKey.PriceAsc: orders = orders.OrderBy(o => o.AmountCharged).ToList(); priceSortSymbol = upArrow; break; case OrderSortKey.LocationDesc: orders = orders.OrderByDescending(o => o.Location.Name).ToList(); locationSortSymbol = downArrow; break; case OrderSortKey.LocationAsc: orders = orders.OrderBy(o => o.Location.Name).ToList(); locationSortSymbol = upArrow; break; } var historyDisplayAlignment = "{0,-6}{1,-9}{2,-25}{3,-15}"; var priceSortLine = $"{priceSortSymbol}----"; var dateSortLine = $"{dateSortSymbol}---"; var locationSortLine = $"{locationSortSymbol}----"; Console.WriteLine(historyDisplayAlignment, "Num", "Price", "Date", "Store"); Console.WriteLine(historyDisplayAlignment, "---", priceSortLine, dateSortLine, locationSortLine); var i = 1; this.OrderIds.Clear(); foreach (var order in orders) { this.OrderIds.Add(order.OrderId); Console.WriteLine(historyDisplayAlignment, i + ".", "$" + order.AmountCharged, order.TimeSubmitted, order.Location.Name); i += 1; } } }
/// <summary> /// Print this menu. /// </summary> public void PrintMenu() { Console.Clear(); CliPrinter.Title("Create Account"); }
/// <summary> /// Handle input. /// </summary> public void InputLoop() { do { PrintMenu(); using (var db = new StoreContext(this.ApplicationState.DbOptions)) { var location = db.GetLocationById(this.ApplicationState.UserData.OperatingLocationId); var order = db.FindCurrentOrder(location, this.ApplicationState.UserData.CustomerId); if (order == null) { CliPrinter.Error("There was an error retrieving your order. Please try again."); break; } bool itemNumberValidator(int num) { if (num > 0 && num <= this.InventoryIds.Count) { return(true); } else { CliPrinter.Error($"Please enter an inventory number between 1 and {this.InventoryIds.Count}"); return(false); } } var itemIndex = CliInput.GetInt(CliInput.GetIntOptions.AllowEmpty, itemNumberValidator, "Inventory number: ") ?? 0; if (itemIndex == 0) { break; } var inventoryId = this.InventoryIds[itemIndex - 1]; var product = db.GetProductFromInventoryId(inventoryId); var projectedQuantity = db.ProjectStockBasedOnOrder(order, product); if (projectedQuantity <= 0) { CliInput.PressAnyKey("That item is out of stock"); continue; } var orderQuantity = CliInput.GetInt(CliInput.GetIntOptions.AllowEmpty, v => v > 0 && v <= projectedQuantity, $"Quantity [1..{projectedQuantity}]: ") ?? 0; if (orderQuantity == 0) { continue; } db.AddLineItem(order, product, orderQuantity); db.SaveChanges(); CliInput.PressAnyKey("\nAdded to order."); } } while (true); this.MenuExit(); }
/// <summary> /// Print this menu. /// </summary> public void PrintMenu() { Console.Clear(); CliPrinter.Title("Select store"); }