/// <summary> /// This method prompts for a location ID then prints the current inventory at this location /// </summary> public void PrintInventory() { var dbLocations = db.Locations.ToList(); Console.WriteLine("List of Locations:\n"); foreach (var obj in dbLocations) { Console.WriteLine(obj.ToString()); } try { newLocation = db.Locations.FirstOrDefault(x => x.ID == InputPrompts.IDPrompt("Location")); } catch (Exception e) { Console.WriteLine($"Error retrieving Location from the database:\n{e}"); } if (newLocation != null) { try { InventoryList = db.Inventory.Include("Product").Include("Location").Where(x => x.Location.ID == newLocation.ID).ToList(); } catch (Exception e) { Console.WriteLine($"Error retrieving inventory list from database:\n{e}"); } if (InventoryList.Count > 0) { Console.WriteLine("Current Inventory Items:\n"); foreach (Inventory inv in InventoryList) { inv.PrintInfo(); } } else { Console.WriteLine("Currently no items have been added to this inventory."); } } }
/// <summary> /// The method to increase the inventory of an item /// Prompts for location and product id /// </summary> /// <remarks> /// If there is no inventory item present, one is created /// </remarks> /// <returns> /// An <c>Inventory</c> object containing the new Inventory status /// </returns> public Inventory AddToInventory() { int idTemp = 0; int quantityTemp = 0; newInventory = new Inventory(); var dbLocations = db.Locations.ToList(); //Print out a list of locations to select from Console.WriteLine("List of Locations:\n"); foreach (var obj in dbLocations) { Console.WriteLine(obj.ToString()); } var dbLocation = new Location(); //Loop to check if Location ID is present while (true) { idTemp = InputPrompts.IDPrompt("Location"); dbLocation = db.Locations.FirstOrDefault(x => x.ID == idTemp); if (dbLocation != null) { break; } Console.WriteLine("Location ID not found. Please try again."); } //List out the products available at the location given var dbentry = db.Products.ToList(); Console.WriteLine("List of products:\n"); foreach (var obj in dbentry) { Console.WriteLine(obj.ToString()); } var dbProduct = new Product(); while (true) { idTemp = InputPrompts.IDPrompt("Product"); //Check to see if the product exists dbProduct = db.Products.FirstOrDefault(x => x.ID == idTemp); if (dbProduct != null) { break; } Console.WriteLine("Product ID not found. Please try again."); } try { quantityTemp = InputPrompts.QuantityPrompt(); } catch (Exception e) { Console.WriteLine($"Error with quantity prompt:\n{e}"); } //Check to see if Product Exists in inventory. //If it does not, create an object and add it. //If it does, add the quantity to the existing object var dbInventory = db.Inventory.Include("Product").Include("Location").FirstOrDefault(x => x.Product == dbProduct && x.Location == dbLocation); if (dbInventory != null) { dbInventory.Quantity += quantityTemp; newInventory = dbInventory; } else { newInventory.Product = dbProduct; newInventory.Location = dbLocation; newInventory.Quantity = quantityTemp; db.Add(newInventory); } db.SaveChanges(); return(newInventory); }