/// <summary> /// Alter quantity of a given location's associated product by given amount. /// </summary> /// <param name="location"></param> /// <param name="product"></param> /// <param name="amount"></param> public void AddProductToLocation(Location location, Product product, int amount) { LocationLine locationLine = locationLines.Where(x => x.LocationId == location.LocationId && x.ProductId == product.ProductId).FirstOrDefault(); locationLine.Quantity += amount; DbContext.SaveChanges(); }
/// <summary> /// Allow user to select a product for purchase. Manipulates a location line passed by reference. /// </summary> /// <param name="location"></param> /// <param name="locationLine"></param> /// <returns>Product</returns> public static Product BrowseProducts(Location location, ref LocationLine locationLine) { List <LocationLine> locationLineList = context.GetLocationLines(location); int productSelected; Product product; do { Console.WriteLine("Which item would you like to purchase?"); // get userinput productSelected = context.ConvertStringToInt(Console.ReadLine()); // validate user input if (productSelected > 0 || productSelected < locationLineList.Count) { // cycle location list foreach (var loc in locationLineList) { // find a matching product if (loc.ProductId == locationLineList[productSelected - 1].ProductId) { // get the product product = context.GetProductFromId(loc.ProductId); locationLine = loc; //return the product return(product); } } } else { Console.WriteLine("Well, at least you tried. Try again."); } } while (true); }
static void Main(string[] args) { //context.Populate(); Console.WriteLine("Welcome to Risky Business!"); // START LOGIN/QUIT SECTION int logInOrQuitInt; do { logInOrQuitInt = MainMenu(); if (logInOrQuitInt == 2) { Quit(); } //log in or create a new customer. unique fName and lName means create a new customer, other wise, grab the existing customer string[] userNamesArray = GetUserNames(); Customer cust = new Customer(); if (userNamesArray.Length == 1) //if the user unputted just one name { cust = context.CreateCustomer(fName: userNamesArray[0]); } else if (userNamesArray.Length > 1) //if the user unputted 2 names { cust = context.CreateCustomer(userNamesArray[0], userNamesArray[1]); } else { throw new ArgumentNullException("User input for name was invalid."); } // if the userNamesArray is empty. // END LOGIN/QUIT SECTION // START LOCATION SELECTION SECTION Location location; int choice; do //location loop starts here. { // menu Console.WriteLine("1. Shop 2. Find A Friend 3. View Your Orders"); //get user input choice = context.ConvertStringToInt(Console.ReadLine()); if (choice == 1) { break; } else if (choice == 2) { SearchACustomer(); } else if (choice == 3) { ViewOrders(cust); } else { Console.WriteLine("Bruh what the heck. 1 or 2."); } } while (true); /* SHOPPING */ // START LOCATION SELECTION SECTION do { // list all locations ListAvailableLocations(); // set the users location choice location = SetLocation(); // END LOCATION SELECTION SECTION Console.WriteLine($"Would you like to shop or view orders at {location.Address} in {location.City}?\n" + "\t1. Shop 2. View Orders \n\t(Or -1 if you'd like to go back)"); choice = context.ConvertStringToInt(Console.ReadLine()); if (choice == 2) { ListOrdersAtLocation(location); } else if (choice == 1) { // START PRODUCT SELECTION SECTION Product product; LocationLine locationLine = new LocationLine(); do // store selected, display products { Console.WriteLine("Here's what we have to offer!"); // show products at that location (aka our inventory) DisplayProducts(location); // get the product they want from that location product = BrowseProducts(location, ref locationLine); // END PRODUCT SELECTION SECTION int amount; // START CHOOSE NUMBER OF PRODUCT TO ADD SECTION do { // list current item they're looking at Console.WriteLine("How many would you like to add to your cart? \n\t(Or -1 if you'd like to go back.)"); // get user response amount = context.ConvertStringToInt(Console.ReadLine()); if (amount == -1) { break; } // check if inventory has enough to satisfy request else if (amount <= context.QuantityOfProduct(product, locationLine)) { // temporarily subtract from location context.SubtractProductFromLocation(location, product, choice); break; } else { Console.WriteLine("Sorry, we don't have enough in stock. Try again."); } } while (true); // END CHOOSE NUMBER OF PRODUCT TO ADD SECTION // START CHECKOUT SECTION Console.WriteLine("Would you like to checkout?(1.Yes 2.No)" + "\n(WARNING! Selecting no will restart the process.)"); // get user response choice = context.ConvertStringToInt(Console.ReadLine()); if (choice == 2) { context.AddProductToLocation(location, product, amount); } // START ORDER SECTION // amount check so we don't add orders with no total else if (amount > 0 && choice == 1) { context.HandleOrder(cust, location, product, amount); } // END ORDER SECTION // END CHECKOUT SECTION } while (choice != 2); } else if (choice == -1) { break; } else { Console.WriteLine("well at least you tried."); } } while (true); } while (true); }
/// <summary> /// Returns the quantity of a product on a specified location line. /// </summary> /// <param name="p"></param> /// <param name="l"></param> /// <returns>int</returns> public int QuantityOfProduct(Product p, LocationLine l) { return(locationLines.Where(x => x.ProductId == p.ProductId && x.LocationLineId == l.LocationLineId).FirstOrDefault().Quantity); }
/// <summary> /// Returns the product of a specified location line. /// </summary> /// <param name="locationLine"></param> /// <returns>Product</returns> public Product GetSpecificProduct(LocationLine locationLine) { return(products.Where(x => x.ProductId == locationLine.ProductId).FirstOrDefault()); }