//********************************* //Methods //********************************* /// <summary> /// Takes in the properties of a bevarage and adds it to the database /// </summary> /// <param name="id">string</param> /// <param name="name">string</param> /// <param name="pack">string</param> /// <param name="price">decimal</param> /// <param name="active">bool</param> public void AddNewItem(string id, string name, string pack, decimal price, bool active) { try { //Open the databse BeverageJMartinEntities beverageEntities = new BeverageJMartinEntities(); //Create a new beverage Beverage addBevarage = new Beverage(); //Assign values to addBeverage properties addBevarage.id = id; addBevarage.name = name; addBevarage.pack = pack; addBevarage.price = price; addBevarage.active = active; //Add the new beverage with it's assigned properties to beverageEntites beverageEntities.Beverages.Add(addBevarage); //Save the changes to the databes made in this instance of beverageEntitites. beverageEntities.SaveChanges(); } catch (Exception e) { UserInterface ui = new UserInterface(); ui.OutputAString(e.ToString() + " " + e.StackTrace); } }
/// <summary> /// Used to delete the list of bevarages found in the SearchByAndPossiblyDelete method /// </summary> /// <param name="queryBeverages"></param> /// <returns>bool</returns> public bool DeleteItem(List <Beverage> queryBeverages) { try { //Bool to hold if the item(s) was successfully deleted of not bool itemDeletedBool = false; //Count used to find out how many times that the database did not correctly delete the beverage int count = 0; //Open the database BeverageJMartinEntities beverageEntities = new BeverageJMartinEntities(); //Beverage to temporarily hold a beverage Beverage tempBeverage = new Beverage(); foreach (Beverage beverage in queryBeverages) { //hold the current beverage in the list than delete if from the list tempBeverage = beverageEntities.Beverages.Find(beverage.id); beverageEntities.Beverages.Remove(tempBeverage); //Check if the beverage just deleted is in the database tempBeverage = beverageEntities.Beverages.Find(beverage.id); //If the bevarage is still in the database add one to the count if (tempBeverage == null) { count++; } } //If the count is not less than one, than at least one of the items was not succesffuly deleted if (count < 1) { //All items successfully added so save the changes beverageEntities.SaveChanges(); itemDeletedBool = true; } else { //An item was not added correctly so do not save the changes and print out an error message. UserInterface ui = new UserInterface(); ui.ErrorInDeleting(); } return(itemDeletedBool); } catch (Exception e) { UserInterface ui = new UserInterface(); ui.OutputAString(e.ToString() + " " + e.StackTrace); return(false); } }
/// <summary> /// Updates a single wine /// </summary> /// <param name="id">string</param> public void UpdateWine(string id) { try { //Open database BeverageJMartinEntities beverageEntities = new BeverageJMartinEntities(); //Create an instance of the UserInterface UserInterface ui = new UserInterface(); //Create a beverage that will hold the one to be updated by the id passed in Beverage beverageToUpdate = beverageEntities.Beverages.Find(id); string inputString = " "; //Continue to loop until the user choose 5 to save and exit. while (inputString != "5") { //Output the beveragtToUpdate to the user ui.PrintLine(); ui.ColorLineNoEnter(FormatBeverageSting(beverageToUpdate)); ui.PrintLine(); //Ouput the Edit Menu ui.ColorLineNoEnter(ui.PrintEditMenu(id)); //Get the users choice of which menu item to do inputString = ui.InputCharReturnString(); //Loop until valid input from the user while (inputString != "1" && inputString != "2" && inputString != "3" && inputString != "4" && inputString != "5") { ui.ColorLineNoEnter(ui.WriteInvalidEntry()); ui.ColorLineNoEnter(FormatBeverageSting(beverageToUpdate)); ui.ColorLineNoEnter(ui.PrintEditMenu(id)); inputString = ui.InputCharReturnString(); } //Ouput a blank line ui.OutputAString(" "); //Get the information needed from the user to update the property of the //property type the user just chose. switch (inputString) { case "1": beverageToUpdate.name = ui.GetTheNameOfTheWine(); break; case "2": beverageToUpdate.pack = ui.GetTheWinePack(); break; case "3": beverageToUpdate.price = ui.GetThePrice(); break; case "4": beverageToUpdate.active = ui.GetIfWineIsActive(); break; } } //Save the changes to the database beverageEntities.SaveChanges(); } catch (Exception e) { UserInterface ui = new UserInterface(); ui.OutputAString(e.ToString() + " " + e.StackTrace); } }