public Product createProduct() { DbStoreContext db = new DbStoreContext();// set up dbcontext ConsoleKey response; var test = ""; while (true) { Console.WriteLine("What would you like to call the new Product?"); test = Console.ReadLine(); //checks input if (Regex.IsMatch(test, regProductTest)) {//add product name newProduct.Name = test; Console.WriteLine("How much should it cost?"); test = Console.ReadLine(); //checks input if (Regex.IsMatch(test, regPriceTest)) {// adds price newProduct.Price = double.Parse(test); Console.WriteLine("Where should we stock the product?" + " Enter a store ID please"); var dbstores = db.StoreLocation.ToArray(); foreach (var store in dbstores) { //print store options Console.WriteLine($"{store.StoreLocationID}: {store.City}"); } //user chooses a location while (true) { response = Console.ReadKey().Key; if (response == ConsoleKey.D1) { newProduct.StoreID = 1; break; } else if (response == ConsoleKey.D2) { newProduct.StoreID = 2; break; } else if (response == ConsoleKey.D3) { newProduct.StoreID = 3; break; } else { Console.WriteLine("invalid, please enter a know store id"); } } //get a quantity for the product while (true) { Console.WriteLine("How many should we stock?"); test = Console.ReadLine(); if (int.Parse(test) > 0) { newProduct.Quantity = int.Parse(test); Console.WriteLine(newProduct.ToString()); break; } else { Console.WriteLine("invalid input"); } } while (true) { //confirm product info Console.WriteLine("Is this information correct?"); Console.WriteLine("Press Y to confirm"); Console.WriteLine("Press N to restart the form"); Console.WriteLine("Press C to cancel"); response = Console.ReadKey(false).Key; if (response == ConsoleKey.Y) { //save to db Console.WriteLine(); db.Add(newProduct); db.SaveChanges(); return(newProduct); } if (response == ConsoleKey.N) { Console.WriteLine(); return(this.createProduct()); } if (response == ConsoleKey.C) { break; } newStore.Products.Add(newProduct); } } else { Console.WriteLine("Invalid input. Please use numbers only"); } } else { Console.WriteLine("Invalid input. Please use letters only"); } } }
public void runStore() { DbStoreContext db = new DbStoreContext(); // set up dbcontext bool broken = true; // bool to get us out of loop ConsoleKey input; // intial console key //Menu options string[] menu = new string[] { "Create new order", "Create new user", "Search User by name", "Look up your Order history", "Update inventory", "Create new product", "Look up location order history", "Exit Program" }; Console.WriteLine("Welcome to the Outdoor Store"); do { //loop through menu options for (int i = 0; i < menu.Length; i++) { int num = i + 1; Console.WriteLine(num + " " + menu[i]); } input = Console.ReadKey().Key; //Create Order if (input == ConsoleKey.D1) { //initialize order Order newOrder = createOrder(); //check that it is not null if (newOrder == null) { Console.WriteLine(); Console.WriteLine($"Order cancelled."); } else if (newOrder != null) {// Add new Order to the db newOrder.OrderTime = DateTime.Now; db.Add(newOrder); db.SaveChanges(); Console.WriteLine(); Console.WriteLine($"Order added as order {newOrder.OrderID}"); } } //Create user else if (input == ConsoleKey.D2) { newUser = createNewUser(); Console.WriteLine("Her is you User ID: " + newUser.CustId); } //User search else if (input == ConsoleKey.D3) { List <Cust> searchResults = findUser(); if (searchResults.Count >= 1) { foreach (Cust p in searchResults) { //Print results p.PrintUserInfo(); } } else { Console.WriteLine("Loooks like nothing came up." + " Try to create a new user!"); Console.WriteLine(); } } //Location search else if (input == ConsoleKey.D4) { List <Order> searchResults = findUserOrderHistory(); if (searchResults.Count >= 1) { //loop through to include product name for (int i = 0; i < searchResults.Count; i++) { List <Product> hold = db.Product.Where(prod => prod.ProductID == searchResults[i].Productid).ToList(); Console.WriteLine(searchResults[i].ToString() + hold[i].Name); } } else { Console.WriteLine("Loooks like nothing came up." + " Try to create a new Order!"); Console.WriteLine(); } } //Adjust inventory else if (input == ConsoleKey.D5) { adjustInventory(); //Update inventory } //create new Product else if (input == ConsoleKey.D6) { createProduct(); } //StoreLocation order history else if (input == ConsoleKey.D7) { List <Order> searchResults = findLocalOrderHistory(); if (searchResults.Count >= 1) { //loops through to print out product names for (int i = 0; i < searchResults.Count; i++) { List <Product> hold = db.Product.Where(prod => prod.ProductID == searchResults[i].Productid).ToList(); Console.WriteLine(searchResults[i].ToString() + hold[i].Name); } } else { Console.WriteLine("Loooks like nothing came up." + " Try to create a new order!"); Console.WriteLine(); } } //exit the loop else if (input == ConsoleKey.D8) { broken = false; Console.WriteLine(); Console.WriteLine(broken); } else { Console.WriteLine(broken); Console.WriteLine("Whoops. Please press a valid key"); } } while (broken == true); }
/* This method creates a new user and adds them to the database*/ public Cust createNewUser() { DbStoreContext db = new DbStoreContext();// set up dbcontext //previous check to see if db was working //var user1 = db.Cust.FirstOrDefault(); //Console.WriteLine($"The user is {user1.CustId},{user1.Fname},{user1.Lname},{user1.State}"); // Loop contains the user to ensure they finish their profile while (true) { // prompts user to enter first name Console.WriteLine("Hi! Please enter your first name?"); test = Console.ReadLine(); // checks to make sure input is valid if (Regex.IsMatch(test, regNameTest)) { break; } Console.WriteLine("Invalid input. Please use letters only for this field."); } //adds first name to user newUser.Fname = test; while (true) { // prompts user to enter last name Console.WriteLine($"Hi, {newUser.Fname}! Please enter your last name"); test = Console.ReadLine(); //checks input if (Regex.IsMatch(test, regNameTest)) { break; } Console.WriteLine("Invalid input. Please use letters only for this field."); } //adds last name newUser.Lname = test; while (true) { //checks input Console.WriteLine("Please enter your address"); test = Console.ReadLine(); if (Regex.IsMatch(test, regAddressTest)) { break; } Console.WriteLine("Invalid input. Please use alphanumeric characters only."); } //adds address newUser.Address = test; while (true) { //checks input Console.WriteLine("Please enter your current city"); test = Console.ReadLine(); if (Regex.IsMatch(test, regCityTest)) { break; } Console.WriteLine("Invalid input. Please use letters only for this field."); } //adds city newUser.City = test; while (true) { //checks input Console.WriteLine("Please enter the abbreviation for your current state"); test = Console.ReadLine(); if (stateAbbreviations.Contains(test.ToUpper())) { break; } Console.WriteLine("Invalid input. Please enter valid abbreviation."); } //adds state newUser.State = test; while (true) { //checks input Console.WriteLine("Please enter your Zip Code "); test = Console.ReadLine(); if (Regex.IsMatch(test, regZipTest)) { break; } Console.WriteLine("Invalid input. Please enter 5 numbers."); } //adds zipcode newUser.Zipcode = test; while (true) { //checks input Console.WriteLine("Please enter your cell phone number"); test = Console.ReadLine(); if (Regex.IsMatch(test, regCellTest)) { break; } Console.WriteLine("Invalid input. Please enter a 10-digit number with no dashes."); } //adds cellphone newUser.Cell = "(" + test.Substring(0, 3) + ") " + test.Substring(3, 3) + "-" + test.Substring(6, 4); while (true) { //checks input Console.WriteLine("Please enter your email address"); test = Console.ReadLine(); if (Regex.IsMatch(test, regEmailTest)) { break; } Console.WriteLine("Invalid Email address. Please try again."); } //adds email newUser.Email = test; //print info newUser.PrintUserInfo(); ConsoleKey response; //confirm info with user do { Console.WriteLine("Is this information correct?"); Console.WriteLine("Press Y to confirm"); Console.WriteLine("Press N to restart the form"); Console.WriteLine("Press C to cancel"); response = Console.ReadKey(false).Key; if (response == ConsoleKey.Y) {// adds to db Console.WriteLine(); db.Add(newUser); db.SaveChanges(); return(newUser); } if (response == ConsoleKey.N) {//returs to start Console.WriteLine(); return(this.createNewUser()); } if (response == ConsoleKey.C) { break; } } while (response != ConsoleKey.Y && response != ConsoleKey.N && response != ConsoleKey.C); return(null); }