/// <summary> /// User interface that handles removing a store from the database /// </summary> /// <param name="sc">The StoreController</param> private static void DeleteStore(StoreController sc) { Console.WriteLine("Select store you want to delete:"); sc.DisplayStores(); Console.WriteLine("Enter the store ID to delete: "); string userIn = Console.ReadLine(); int sid; while (!int.TryParse(userIn, out sid)) { Console.WriteLine("Invalid input. Not a number."); Console.WriteLine("Enter the store ID to delete: "); userIn = Console.ReadLine(); } if (sc.repository.GetAll().Any(s => s.StoreId == sid)) { try { sc.repository.Delete(sid); sc.repository.Save(); Console.WriteLine("Store deleted successfully!"); } catch (Exception) { Console.WriteLine($"Error occured when trying to delete store with ID: {sid}"); } } else { Console.WriteLine($"No store with ID: {sid}"); } }
static void Main(string[] args) { Console.WriteLine("Hello and Welcome to Gomez and Company!"); string displayHome = "How could we assist you today?\n" + "1: Choose or create new customer to begin order\n" + "2: Place an order\n" + "3: Search for customer by Name\n" + "4: Display details of an order\n" + "5: Display order history for customer\n" + "6: Display order history for store\n" + "0: Exit"; CustomerController customerControl = new CustomerController(); OrderController orderControl = new OrderController(); ProductController productControl = new ProductController(); StoreController storeControl = new StoreController(); Customers newCustomer = null; bool loggedIn = true; while (loggedIn == true) { Console.WriteLine(displayHome); Console.WriteLine("Please select a task"); string userInput = Console.ReadLine(); int input; while (!int.TryParse(userInput, out input)) { Console.WriteLine("Please select a valid option."); userInput = Console.ReadLine(); } switch (input) { case 1: newCustomer = CreateCustomer(customerControl); break; case 2: if (newCustomer == null) { Console.WriteLine("Select and existing account or create a new user to continue."); } else { PlaceOrder(newCustomer, storeControl, productControl, orderControl); } break; case 3: Console.WriteLine("Enter the first name of the customer"); string firstName = Console.ReadLine(); Console.WriteLine("Enter the last name of the customer"); string lastName = Console.ReadLine(); customerControl.SearchCustomerByName(firstName, lastName); break; case 4: Console.WriteLine("Please select the order for which you would like to see the details!"); orderControl.DisplayOrders(); Console.WriteLine("Please enter a valid Order ID "); userInput = Console.ReadLine(); int orderId; while (!int.TryParse(userInput, out orderId)) { Console.WriteLine("Please select a valid order ID."); userInput = Console.ReadLine(); } orderControl.DisplayOrderDetails(orderId); break; case 5: if (newCustomer == null) { Console.WriteLine("Please select a customer to view order history."); } else { Console.WriteLine($" {newCustomer.FirstName} {newCustomer.LastName}:"); orderControl.DisplayOrderDetailsOfCustomer(newCustomer.CustomerId); } break; case 6: Console.WriteLine("For which store would you like to see their order history?"); storeControl.DisplayStores(); Console.WriteLine("Please enter a store ID to see it's order history:"); userInput = Console.ReadLine(); int storeId; while (!int.TryParse(userInput, out storeId)) { Console.WriteLine("Please select a valid store ID."); userInput = Console.ReadLine(); } if (storeControl.repository.GetAll().Any(s => s.StoreLocationId == storeId)) { Console.WriteLine($" {storeControl.repository.GetById(storeId).Name}"); orderControl.DisplayOrderDetailsOfStore(storeId); } else { Console.WriteLine($"Sorry but there is no store with that ID."); } break; case 0: loggedIn = false; break; default: Console.WriteLine("Please select a valid option or 0 to exit."); break; } } }
private static void PlaceOrder(Customers existingCustomer, StoreController storeControl, ProductController productControl, OrderController orderControl) { Console.WriteLine("Please choose the store that you would like to place an order with "); storeControl.DisplayStores(); Console.WriteLine("Enter the store ID: "); string userInput = Console.ReadLine(); int storeId; while (!int.TryParse(userInput, out storeId)) { Console.WriteLine("Please select a valid Store ID."); userInput = Console.ReadLine(); } if (storeControl.repository.GetAll().Any(s => s.StoreLocationId == storeId)) { var currentStore = storeControl.repository.GetById(storeId); using var context = new storeapplicationContext(GeneralRepository <StoreLocations> .Options); var inventory = context.Inventory .Include(i => i.Product) .Where(i => i.LocationId == storeId) .ToList(); Dictionary <Products, int> cartProducts = new Dictionary <Products, int>(); while (true) { Console.WriteLine("Please select an item to add to your order:"); foreach (var item in inventory) { Console.WriteLine($" {item.Product.ProductId} {item.Product.Name} ${item.Product.Price.ToString("0.##")} Amount in stock: {item.Quantity}\n"); } Console.WriteLine("Please enter a product ID to add item to your order or please type 0 to exit and complete your order"); userInput = Console.ReadLine(); int productId; while (!int.TryParse(userInput, out productId)) { Console.WriteLine("Please respond with a valid ID."); userInput = Console.ReadLine(); } if (cartProducts != null) { while (cartProducts.Keys.Any(p => p.ProductId == productId)) { Console.WriteLine("Item was already added to order."); foreach (var item in inventory) { Console.WriteLine($"ID: {item.Product.ProductId} Product Name: {item.Product.Name} Price: ${item.Product.Price} Amount in stock: {item.Quantity}\n"); } Console.WriteLine("Enter a product ID to add item to your order( or please type 0 to exit the order):"); userInput = Console.ReadLine(); while (!int.TryParse(userInput, out productId)) { Console.WriteLine("Please enter a valid product ID."); Console.WriteLine("Enter a product ID to add item to your order( or please type 0 to exit the order):"); userInput = Console.ReadLine(); } } } if (productId == 0) { break; } if (inventory.Any(i => i.Product.ProductId == productId)) { var p = productControl.repository.GetById(productId); Console.WriteLine($"How many {p.Name}s do you want to add to the order:"); userInput = Console.ReadLine(); int qty; while (!int.TryParse(userInput, out qty)) { Console.WriteLine("Please put in a valid amount."); Console.WriteLine($"How many {p.Name}s do you want to add to the order:"); userInput = Console.ReadLine(); } if (qty > 0) { Inventory inventoryLine = inventory.First(i => i.Product.ProductId == productId); if (inventoryLine.Quantity == 0) { Console.WriteLine($"{p.Name} no longer in stock."); } else if (inventoryLine.Quantity < qty) { Console.WriteLine("You can't order more products than are available."); } else { cartProducts.Add(p, qty); inventoryLine.Quantity -= qty; context.Update(inventoryLine); context.SaveChanges(); Console.WriteLine("Product added to order!"); } } else { Console.WriteLine("Quantity must be positive."); } } else { Console.WriteLine($"Unfortunately product number {productId} is not available in this store."); } } if (cartProducts.Count == 0) { Console.WriteLine("No products were added to order."); } else { decimal totalCostOfOrder = 0; foreach (var item in cartProducts.Keys) { totalCostOfOrder += (item.Price * cartProducts[item]); } Console.WriteLine("Your total comes out to $" + totalCostOfOrder.ToString("0.##")); Orders newOrder = new Orders { CustomerId = existingCustomer.CustomerId, LocationId = currentStore.StoreLocationId, Total = totalCostOfOrder, TimeOfOrder = DateTime.Now }; orderControl.repository.Add(newOrder); orderControl.repository.Save(); newOrder = orderControl.repository.GetAll().First(o => o.CustomerId.Equals(existingCustomer.CustomerId)); foreach (var item in cartProducts.Keys) { var product = context.Products .Include(p => p.OrderLines) .First(p => p.ProductId == item.ProductId); product.OrderLines.Add(new OrderLines { OrderId = newOrder.OrderId, Order = newOrder, Amount = cartProducts[item] }); } context.SaveChanges(); } } else { Console.WriteLine($"A store does not exist with that ID. Please use a valid store ID."); } }
/// <summary> /// User interface that guides the user to add Products to a single store /// </summary> /// <param name="pc">The ProductController</param> /// <param name="sc">The StoreController</param> private static void AddProductToStore(ProductController pc, StoreController sc) { //Select the store to add products to and make sure it exists Console.WriteLine("Which store do you want to add products to: \n"); sc.DisplayStores(); Console.WriteLine("Enter Store ID: "); string userIn = Console.ReadLine(); int sid; while (!int.TryParse(userIn, out sid)) { Console.WriteLine("Invalid input. Not a number."); userIn = Console.ReadLine(); } if (sc.repository.GetAll().Any(s => s.StoreId == sid)) { //Display the full set of products that exist in the database Console.WriteLine($"Which products do you want to add to the store: {sc.repository.GetById(sid).StoreName}"); pc.DisplayProducts(); //Select the product id, making sure it exists, to link a product to the store Console.WriteLine("Enter Product ID to add to store: "); userIn = Console.ReadLine(); int pid; while (!int.TryParse(userIn, out pid)) { Console.WriteLine("Invalid input. Not a number."); userIn = Console.ReadLine(); } if (pc.repository.GetAll().Any(p => p.ProductId == pid)) { //Get that product to be later added to the store inventory Products product = pc.repository.GetById(pid); //Enter the amount of products to be stored in the stores inventory Console.WriteLine("Enter the quantity of the product to be added:"); userIn = Console.ReadLine(); int qty; while (!int.TryParse(userIn, out qty)) { Console.WriteLine("Invalid input. Not a number."); userIn = Console.ReadLine(); } //Try adding the products to the store //If the product already existed in the store the product won't be added and an error is thrown try { using var context = new _2006StoreApplicationContext(GenericRepository <Products> .Options); var store = context.Stores .Include(s => s.Inventory) .First(s => s.StoreId == sid); store.Inventory.Add(new Inventory { Product = product, Amount = qty }); context.SaveChanges(); Console.WriteLine("Product was added to the store!"); } catch (Exception) { Console.WriteLine("Error occurred when trying to add product to store."); } } else { Console.WriteLine($"No products with ID: {pid}"); } } else { Console.WriteLine($"No stores with ID: {sid}"); } }
/// <summary> /// User interface that guides the user to place an order for the current customer. /// </summary> /// <param name="currentCustomer">The current customer placing the order</param> /// <param name="sc">The StoreController</param> /// <param name="pc">The ProductController</param> /// <param name="oc">The OrderController</param> private static void PlaceOrder(Customers currentCustomer, StoreController sc, ProductController pc, OrderController oc) { //Select the store you want to place an order to Console.WriteLine("Which store would you like to place an order to: "); sc.DisplayStores(); Console.WriteLine("Enter the store ID: "); string userIn = Console.ReadLine(); int sid; while (!int.TryParse(userIn, out sid)) { Console.WriteLine("Invalid input. Not a number."); Console.WriteLine("Enter the store ID: "); userIn = Console.ReadLine(); } //If the store id that was inputted exists list the products in that store if (sc.repository.GetAll().Any(s => s.StoreId == sid)) { var currentStore = sc.repository.GetById(sid); using var context = new _2006StoreApplicationContext(GenericRepository <Stores> .Options); var inventory = context.Inventory .Include(i => i.Product) .Where(i => i.StoreId == sid) .ToList(); //Keep track of the number of products in the order to calculate the final total of the order later Dictionary <Products, int> productsInOrder = new Dictionary <Products, int>(); //Keep asking the user to add more products to the order until they type 0 while (true) { Console.WriteLine("Select a product to add to order:"); foreach (var item in inventory) { Console.WriteLine($"Product: {item.Product.ProductName} Price: ${item.Product.Price} ID: {item.Product.ProductId} In Stock: {item.Amount}\n"); } Console.WriteLine("Enter product ID to add to order(or type 0 to quit):"); userIn = Console.ReadLine(); int pid; while (!int.TryParse(userIn, out pid)) { Console.WriteLine("Invalid input. Not a number."); Console.WriteLine("Enter product ID to add to order(or type 0 to quit):"); userIn = Console.ReadLine(); } //prevent the user from adding the same item to their order if (productsInOrder != null) { while (productsInOrder.Keys.Any(p => p.ProductId == pid)) { Console.WriteLine("Item was already added to order."); foreach (var item in inventory) { Console.WriteLine($"Product: {item.Product.ProductName} Price: ${item.Product.Price} ID: {item.Product.ProductId} In Stock: {item.Amount}\n"); } Console.WriteLine("Enter product ID to add to order(or type 0 to quit):"); userIn = Console.ReadLine(); while (!int.TryParse(userIn, out pid)) { Console.WriteLine("Invalid input. Not a number."); Console.WriteLine("Enter product ID to add to order(or type 0 to quit):"); userIn = Console.ReadLine(); } } } if (pid == 0) { break; } //Check to see if the product id the user enter matches any of the products available in the store if (inventory.Any(i => i.Product.ProductId == pid)) { //Get the product info from the id the user entered, then get the amount from the inventory to //check that it is >= 0 var p = pc.repository.GetById(pid); Console.WriteLine($"How many {p.ProductName}s do you want to add to the order:"); userIn = Console.ReadLine(); int qty; while (!int.TryParse(userIn, out qty)) { Console.WriteLine("Invalid input. Not a number."); Console.WriteLine($"How many {p.ProductName}s do you want to add to the order:"); userIn = Console.ReadLine(); } if (qty > 0) { Inventory inventoryLine = inventory.First(i => i.Product.ProductId == pid); if (inventoryLine.Amount == 0) { Console.WriteLine($"{p.ProductName} no longer in stock."); } else if (inventoryLine.Amount < qty) { Console.WriteLine("You can't order more products than are available."); } //If the product is available and in stock, add keep track of the product, decrement the inventory, //and update the inventory when selecting more products else { productsInOrder.Add(p, qty); inventoryLine.Amount -= qty; context.Update(inventoryLine); context.SaveChanges(); Console.WriteLine("Product added to order!"); } } else { Console.WriteLine("Invalid qty. Input a positive integer."); } } else { Console.WriteLine($"Product with ID: {pid} is not available in the current store."); } } //Check that the user actually selected products before actually creating the order in the database if (productsInOrder.Count == 0) { Console.WriteLine("No products were added to order."); } else { //calculate the total cost of the order and display it to the user decimal totalCostOfOrder = 0; foreach (var item in productsInOrder.Keys) { totalCostOfOrder += (item.Price * productsInOrder[item]); } Console.WriteLine("Total cost of your order: $" + totalCostOfOrder); //ask for a description to uniquely identify the order in order to find it later Console.WriteLine("Please provide a unique description for your order: "); string desc = Console.ReadLine(); if (desc == null || oc.repository.GetAll().Any(o => o.OrderDescription.Equals(desc))) { Console.WriteLine("Description already exists or no description was entered."); } else { //Save the order, then retrieve it again to create the orderline for it Orders newOrder = new Orders { CustomerId = currentCustomer.CustomerId, StoreId = currentStore.StoreId, OrderDescription = desc, TotalCost = totalCostOfOrder }; oc.repository.Add(newOrder); oc.repository.Save(); newOrder = oc.repository.GetAll().First(o => o.OrderDescription.Equals(desc)); //Link products that were in the recently created order to the orderId //This creates a new OrderLine that keeps track of what products belong to what order foreach (var item in productsInOrder.Keys) { var product = context.Products .Include(p => p.OrderLines) .First(p => p.ProductId == item.ProductId); product.OrderLines.Add(new OrderLines { Order = newOrder, Amount = productsInOrder[item] }); } context.SaveChanges(); } } } else { Console.WriteLine($"Store with ID: {sid} does not exist."); } }
//REQUIREMENTS: //place orders to store locations for customers //add a new customer //search customers by name //display details of an order //display all order history of a store location //display all order history of a customer static void Main(string[] args) { CustomerController cc = new CustomerController(); OrderController oc = new OrderController(); ProductController pc = new ProductController(); StoreController sc = new StoreController(); Customers currentCustomer = null; bool quit = false; Console.WriteLine("Welcome to the Store Application"); string homeMenu = "What would you like to do:\n" + "1: Register as a new customer or sign in\n" + "2: Create new store\n" + "3: Create new product\n" + "4: Add products to a store\n" + "5. Place an order\n" + "6: Search for customer by username\n" + "7: Display details of an order\n" + "8: Display order history of a customer\n" + "9: Display order history of a store\n" + "10: Delete store\n" + "11: Unregister Customer\n" + "0: Quit"; while (!quit) { Console.WriteLine(homeMenu); Console.WriteLine("Enter your selection(0-11):"); string userIn = Console.ReadLine(); int input; while (!int.TryParse(userIn, out input)) { Console.WriteLine("Invalid input. Not a number."); Console.WriteLine("Enter your selection(0-11):"); userIn = Console.ReadLine(); } switch (input) { case 1: //Register Customers currentCustomer = RegisterCustomer(cc); break; case 2: //Add Stores BuildStore(sc); break; case 3: //Add Products BuildProducts(pc); break; case 4: //Add Products to Stores AddProductToStore(pc, sc); break; case 5: //Place an order if (currentCustomer == null) { Console.WriteLine("You must register or sign in first."); } else { PlaceOrder(currentCustomer, sc, pc, oc); } break; case 6: //Search customers by username Console.WriteLine("Enter username to search:"); string username = Console.ReadLine(); cc.SearchCustomerByUsername(username); break; case 7: //Get order details for a particular order Console.WriteLine("Select the order you want details for:"); oc.DisplayOrders(); Console.WriteLine("Enter the Order ID to get details: "); userIn = Console.ReadLine(); int oid; while (!int.TryParse(userIn, out oid)) { Console.WriteLine("Invalid input. Not a number."); userIn = Console.ReadLine(); } oc.DisplayOrderDetails(oid); break; case 8: //Check current customer order history if (currentCustomer == null) { Console.WriteLine("You must register or sign in first."); } else { Console.WriteLine($"Order history for current customer {currentCustomer.UserName}:"); oc.DisplayOrderDetailsOfCustomer(currentCustomer.CustomerId); } break; case 9: //Check order history for a particular store Console.WriteLine("Select the store you want the order history for:"); sc.DisplayStores(); Console.WriteLine("Enter the store ID to see order history:"); userIn = Console.ReadLine(); int sid; while (!int.TryParse(userIn, out sid)) { Console.WriteLine("Invalid input. Not a number."); userIn = Console.ReadLine(); } if (sc.repository.GetAll().Any(s => s.StoreId == sid)) { Console.WriteLine($"Order history for {sc.repository.GetById(sid).StoreName}"); oc.DisplayOrderDetailsOfStore(sid); } else { Console.WriteLine($"Store with ID: {sid} does not exist."); } break; case 10: //Delete a particular store DeleteStore(sc); break; case 11: //Delete the current customer Console.WriteLine("Are you sure you want to unregister?(y/n)"); string choice = Console.ReadLine().ToLower(); while (!choice.Equals("y") && !choice.Equals("n")) { Console.WriteLine("Please type y or n."); choice = Console.ReadLine().ToLower(); } if (choice.Equals("y")) { UnregisterCustomer(cc, currentCustomer.CustomerId); currentCustomer = null; } else { Console.WriteLine($"{currentCustomer.UserName} was not unregistered."); } break; case 0: quit = true; break; default: Console.WriteLine("Invalid input. Please enter 1-11."); break; } } }