private static void SearchForCustomer(List <Library.Customer> customers) { Library.Customer customerChoice = null; int userInput = -1; List <int> customerIds = new List <int>(); foreach (var cust in customers) { customerIds.Add(cust.CustomerId); } Console.WriteLine("Please enter a valid customer ID to search for"); try { userInput = int.Parse(Console.ReadLine()); if (customerIds.Contains(userInput)) { customerChoice = customers[userInput - 1]; Console.WriteLine($"Customer with ID {userInput} found."); Console.WriteLine($"Customer is: {customerChoice.FirstName} {customerChoice.LastName}"); } else { Console.WriteLine("Invalid customer selection"); } } catch (FormatException) { Console.WriteLine("Invalid entry"); } catch (IndexOutOfRangeException) { Console.WriteLine("Invalid customer"); } }
private static Library.Customer ChooseCustomer(List <Library.Customer> customers) { Library.Customer customerChoice = null; bool validCustomer = false; int userInput = -1; List <int> customerIds = new List <int>(); foreach (var cust in customers) { Console.WriteLine($"\n{cust.CustomerId} | {cust.FirstName} {cust.LastName}"); customerIds.Add(cust.CustomerId); } do { Console.WriteLine("Please select a valid customer ID"); try { userInput = int.Parse(Console.ReadLine()); if (customerIds.Contains(userInput)) { validCustomer = true; customerChoice = customers[userInput - 1]; } else { Console.WriteLine("Invalid customer selection"); } } catch (FormatException) { Console.WriteLine("Invalid entry"); } catch (IndexOutOfRangeException) { Console.WriteLine("Invalid customer"); } } while (!validCustomer); return(customerChoice); }
private static void PlaceOrder(Library.Customer cust, StoreLocation store, Order order, List <Library.Product> products) { int prodChoice, qtyChoice = -1; try { Console.WriteLine($"{store.StoreName} has in stock: \n"); store.DisplayInventoryCatalog(); //ask if they would like to purchase an item Console.WriteLine("Please select an item to purchase"); prodChoice = int.Parse(Console.ReadLine()); Console.WriteLine("Please enter a quantity you would like purchase"); qtyChoice = int.Parse(Console.ReadLine()); order.AddToCart(products[prodChoice], qtyChoice); } catch (FormatException) { Console.WriteLine("You have selected an invalid option. Please allow an associate to assist you with your transaction."); } }
public void PromptUser(DbContextOptions <P0Context> dbContextOptions) { P0Repository p0Repo = new P0Repository(dbContextOptions); //initialize login parameters int customerId = 0; //populate initial stores and their customers from db List <Library.Customer> customerList = new List <Library.Customer>(); List <Library.Store> storeList = p0Repo.GetStores(customerList); while (true) { //Ask if new customer Console.WriteLine(); Console.WriteLine("r:\tReturning Customer"); Console.WriteLine("n:\tNew Customer"); var input = Console.ReadLine(); //Returning customer if (input == "r") { // compare customerID with list of all customers and login if found, else return to login Console.WriteLine("Login with your Customer Id:"); Console.WriteLine(); input = Console.ReadLine(); //present options to returning customer if (customerList.Exists(c => c.Id == Convert.ToInt32(input))) { Library.Customer logCustomer = customerList.Find(c => c.Id == Convert.ToInt32(input)); customerId = logCustomer.Id; var custStores = storeList.FindAll(x => x.Customers.Exists(y => y.Id == customerId)); Console.WriteLine(); Console.WriteLine($"Welcome {logCustomer.Name}"); Console.WriteLine(); Console.WriteLine("p:\tPlace a new order at a store"); Console.WriteLine("s:\tSearch for an account"); Console.WriteLine("h:\tPrint your order history"); Console.WriteLine("t:\tPrint orders from a store"); input = Console.ReadLine(); if (input == "p") { //Create and execute a new order at a target store //read target store, and selections from logged in customer, then creat this order in db Library.Store storeChoice = ReadStoreChoice(storeList); PrintStoreInventory(storeChoice); List <Library.Product> selections = ReadSelections(storeChoice); Library.Order newOrder = new Library.Order(storeChoice, logCustomer, selections); //add order to db p0Repo.CreateOrder(newOrder); //execute order in db and update target store, add customer to store if needed storeChoice = p0Repo.FillOrderDb(storeChoice, newOrder); Console.WriteLine(); } else if (input == "s") { // search for a customer by name var storeChoice = ReadStoreChoice(storeList); string customerName = ReadCustomer(); if (storeChoice.Customers.Exists(x => x.Name == customerName)) { Console.WriteLine(); Console.WriteLine($"{customerName} shops at {storeChoice.Name}"); } else { Console.WriteLine(); Console.WriteLine($"{customerName} does not shop at {storeChoice.Name}"); } } else if (input == "h") { //Print this customer's order history PrintOrderHistory(customerId, storeList); } else if (input == "t") { //print the orders this customer has made from a store var storeChoice = ReadStoreChoice(storeList); PrintCustOrders(customerId, storeChoice); } else { Console.WriteLine(); Console.WriteLine(); } } else { Console.WriteLine(); Console.WriteLine("Invalid CustomerId, Returning to Login"); Console.WriteLine(); } } //create a new customer else if (input == "n") { Console.WriteLine("Enter your full name:"); string customerName = Console.ReadLine(); p0Repo.CreateCustomer(customerName, customerList); Console.WriteLine(); Console.WriteLine($"New account created, your Customer ID is: {Convert.ToString(customerList.Last().Id)}"); Console.WriteLine("Returning to Login."); Console.WriteLine(); } } }
static void Main(string[] args) { //https://github.com/2006-jun15-net/trainer-code/wiki/Project-0-requirements WelcomeMessage(); List <Library.Product> products = new List <Library.Product>(); List <Library.Customer> customers = new List <Library.Customer>(); List <Order> orders = new List <Order>(); List <StoreLocation> stores = new List <StoreLocation>(); using var context = new project0Context(options); var custs = context.Customer.ToList(); var prods = context.Product.ToList(); var stor = context.Store.ToList(); foreach (var entry in custs) { customers.Add(Mapper.MapDbEntryToCustomer(entry)); } foreach (var entry in prods) { products.Add(Mapper.MapDbEntrytoProduct(entry)); } foreach (var entry in stor) { stores.Add(Mapper.MapDbEntryToStoreLocation(entry)); } foreach (var store in stores) { foreach (var prod in products) { store.Inventory.Add(prod, 5); } } Order currentOrder = new Order(); Library.Customer currentCustomer = null; StoreLocation currentStore = null; bool runProgram = true; while (runProgram) { DisplayMenu(); Console.Write("What would you like to do today? "); var userInput = Console.ReadLine().ToLower(); if (ValidateMenuSelectionInput(userInput)) { switch (userInput) { case "a": Console.Write("You have chosen to Add a new customer"); customers.Add(AddCustomer()); break; case "c": Console.WriteLine("You have chosen to checkout"); currentOrder.Checkout(currentStore, currentCustomer); break; case "cc": Console.WriteLine("Please choose the customer placing the order"); currentCustomer = ChooseCustomer(customers); Console.WriteLine("Please choose the store you will place the order from"); currentStore = ChooseStoreLocation(stores); break; case "d": Console.Write("You have chosen to display order details"); currentOrder.DisplayOrderDetails(); break; case "p": try { Console.Write("You have chosen to place a new order"); PlaceOrder(currentCustomer, currentStore, currentOrder, products); } catch (NullReferenceException) { Console.WriteLine("You must first choose a customer and store"); } break; case "s": Console.Write("You have chosen to search for a customer"); SearchForCustomer(customers); break; case "x": Console.Write("You have chosen to Exit the program"); runProgram = false; break; } } else { Console.WriteLine("\nSorry, that is not a valid input. Please enter a valid option."); } } GoodbyeMessage(); }