public void AddNewIngredient(string ingredientName, double amount, DateTime expiryDate) //metoda pozwalająca na dodanie { //nowych składników do bazy danych AbstractIngredient newIngredient = FactoryPicker.Instance.Pick(ingredientName).Create(amount, expiryDate); //pomaga określić rodzaj składnika i od razu do instancjonuje DataBase.AddIngredientToDatabase(newIngredient);//dodaje nowy składnik w postaci klasy do bazy danych }
public void AddNewIngredientToDatabase(OnlineDataBase dataBase, AbstractIngredient ingredient)//dodaje nowy składnik do bazy, //wykorzystując klasę newIngredient, //ale na podstawie klasy ingredient { //a nie poszczególnych składowych NewIngredient = new NewIngredientSaver(dataBase); NewIngredient.AddNewIngredient(ingredient.Name, ingredient.Amount, ingredient.ExpiryDate); }
/// <summary> /// Searches for the oldest ingredient of the passed type. /// </summary> /// <param name="neededIngredient"></param> /// <returns></returns> private AbstractIngredient FindOldestIngredient(AbstractIngredient neededIngredient) //wyszukuje składnika z najstarszą datą ważności { AbstractIngredient oldest = neededIngredient; //pole w którym będzie zapisywany najstarszy składnik foreach (AbstractIngredient AI in Fridge.Content) //iteruje przez wszyskie składniki w lodówce { if (AI.Name == neededIngredient.Name) //w polu zapisany zostaje pierwszy składnik o nazwie takiej jak poszukiwany { oldest = AI; break; } } for (int i = 1; i < Fridge.Content.Count; i++) //każda pozycja w lodówce jest sprawdzana pod kątem nazwy i jeżeli jej { //nazwa się zgadza, a data ważności jest starsza niż dotychczasowa, //to starszy składnik zastępuje dotychczasowy if (Fridge.Content[i].Name == neededIngredient.Name) { if (oldest.ExpiryDate > Fridge.Content[i].ExpiryDate) { oldest = Fridge.Content[i]; } } } return(oldest); }
public override void AddIngredientToDatabase(AbstractIngredient ingredient) //metoda dodająca składniki do bazy danych { try { MySqlConnection LocalConnection = (MySqlConnection)Connection; //downcasting, w celu dostosowania odpowiednich metod MySqlCommand LocalCommand = (MySqlCommand)Command; //downcasting, w celu dostosowania odpowiednich metod string CommandString = $"INSERT INTO FridgeContent (Name, Amount, ExpiryDate) " + //polecenie dodania rzędu w tabeli $"VALUES(@Name, @Amount, @ExpiryDate)"; using (LocalConnection = new MySqlConnection(ConnectionString)) { LocalConnection.Open(); // otwarcie połączenia using (LocalCommand = new MySqlCommand(CommandString, LocalConnection)) //przy otwartym połączeniu wypełniane { //są poszczególne kolumny LocalCommand.Parameters.AddWithValue("@Name", ShortName(ingredient.Name)); LocalCommand.Parameters.AddWithValue("@Amount", ingredient.Amount); LocalCommand.Parameters.AddWithValue("@ExpiryDate", ingredient.ExpiryDate); LocalCommand.ExecuteNonQuery(); } LocalConnection.Close();//zamyka połączenie } } catch (InvalidOperationException ex) { MessageBox.Show(ex.Message, "FreeSQL.AddIngredient"); } catch (MySqlException ex) { MessageBox.Show(ex.Message, "FreeSQL.AddIngredient"); } catch (Exception ex) { MessageBox.Show(ex.Message, "FreeSQL.AddIngredient"); } }
/// <summary> /// Adds the new ingredient for the recipe after clicking the addIngredient button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAddIngredientToRecipe_Click(object sender, RoutedEventArgs e) //dodaje nowy składnik na listę roboczą 'list' { lstIngredients.Items.Clear(); //czyści listę składników, przed wypełnieniem jej jej aktualną wersją AbstractIngredientFactory factory = FactoryPicker.Instance.Pick(cmbIngredientList.SelectedItem.ToString()); //wybiera odpowiednią fabrykę na podstawie wybranej nazwy AbstractIngredient ingredient = factory.Create(Convert.ToDouble(txtAmount.Text)); //tworzy nowy składnik z parametrami podanymi w formularzu przez użytkownika list.Add(ingredient); //dodaje nowy składnik na listę roboczą FillTheList(); //wypełnia listę składników na liście roboczej na nowo }
public override void DeleteIngredientFromDatabase(AbstractIngredient ingredient) { string amountWithaDot = CommaFormat(ingredient.Amount.ToString()); string amountwithComa = ingredient.Amount.ToString(); string CommandString = $"DELETE FROM FridgeContent " + $"WHERE Name = '{ShortName(ingredient.Name)}' " + $"AND Amount = '{amountWithaDot}' " + $"AND ExpiryDate = '{ingredient.ExpiryDate}' " + $"LIMIT 1"; using (MYSQLConnection = new MySqlConnection(ConnectionString)) { MYSQLConnection.Open(); MYSQLCommand = new MySqlCommand(CommandString, MYSQLConnection); MYSQLCommand.ExecuteNonQuery(); MYSQLConnection.Close(); } }
private void BtnDeleteIngredient_Click(object sender, RoutedEventArgs e) //metoda usuwająca wybrany składnik z bazy danych { //polega na porównianiu parametrów wybranego //obiektu z listy z obiektami w bazie danych try { string[] Ingredient = lstFridgeContent.SelectedItem.ToString().Split(' ', '\n'); //rozbija tekst z listy na //pojedyńcze informacje AbstractIngredient ingredient = FactoryPicker.Instance.Pick(Ingredient[1]). //dopasowuje dane do odpowiednich Create(Double.Parse(Ingredient[2]), //właściwości klasy ingredient Convert.ToDateTime(Ingredient[3])); Fridge.DeleteIngredientFromDataBase(Fridge.Filler.dataBasePull.DataBase, ingredient); //używa metody Delete klasy //fridge MessageBox.Show("Thrown out!", "Success!", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception) { MessageBox.Show("Deleting failed", "MainWindow.BtnDelete", MessageBoxButton.OK, MessageBoxImage.Error); } RefreshPage(); //odświeża widok }
public override void DeleteIngredientFromDatabase(AbstractIngredient ingredient) //metoda usuwająca wybrany składnik //z bazy danych { MySqlConnection LocalConnection = (MySqlConnection)Connection; //downcasting, w celu dostosowania odpowiednich metod MySqlCommand LocalCommand = (MySqlCommand)Command; //downcasting, w celu dostosowania odpowiednich metod string amountWithaDot = CommaFormat(ingredient.Amount.ToString()); //zmienia format doubla dostosowując go do standardu //bazy danych, wykorzystując lokalną metodę CommaFormat string CommandString = $"DELETE FROM FridgeContent " + //polecenie usunięcia składnika o podanych $"WHERE Name = '{ShortName(ingredient.Name)}' " + //parametrach $"AND Amount = '{amountWithaDot}' " + $"AND ExpiryDate = '{ingredient.ExpiryDate}' " + $"LIMIT 1"; using (LocalConnection = new MySqlConnection(ConnectionString)) { LocalConnection.Open(); //otwiera połączenie LocalCommand = new MySqlCommand(CommandString, LocalConnection); LocalCommand.ExecuteNonQuery(); //wykonuje polecenie LocalConnection.Close(); //zamyka połączenie } }
/// <summary> /// Pulls specific amount of selected ingredient from the fridge. /// </summary> /// <param name="neededIngredient"></param> private void PullIngredientFromFridge(AbstractIngredient neededIngredient) // pobiera z lodówki odpowiednią ilość //najstarszego produktu i wkłada go na miejsce { try { AbstractIngredient NeededIngredient = neededIngredient; //pole, którego wartość może być zmniejszana AbstractIngredient oldestIngredient = FindOldestIngredient(neededIngredient); //pole, w którym znajduje się składnik //uznawany za najstarszy AbstractIngredient putBack; //tu wyląduje niewykorzystana resztka, którą następnie lokuje się //spowrotem w bazie danych double amountToTake = neededIngredient.Amount; //pole na ilość danego składnika do wzięcia - zmniejsza się wraz // z wyjmowaniem kolejnych produktów danego typu if (amountToTake < oldestIngredient.Amount) { putBack = FactoryPicker.Instance.Pick(oldestIngredient.Name). //wypełnia pole ze składnikiem do odłożenia Create(oldestIngredient.Amount - amountToTake, oldestIngredient.ExpiryDate); Fridge.DeleteIngredientFromDataBase(Fridge.Window.DataBase, oldestIngredient); //usuwa z bazy wzięty składnik Fridge.AddNewIngredientToDatabase(Fridge.Window.DataBase, putBack); //dodaje w miejsce usuniętego //składnika składnik o zmniejszonej ilości } else if (amountToTake == oldestIngredient.Amount) //jeśli ilość najstarszego składnika pokrywa się //z jego wymaganą ilością, to jest usuwany w całości { Fridge.DeleteIngredientFromDataBase(Fridge.Window.DataBase, oldestIngredient); } else if (amountToTake > oldestIngredient.Amount) //jeśli jedno "opakowanie" to za mało, to metoda zostanie wykonana jeszcze raz { NeededIngredient.TakeAmount(oldestIngredient.Amount); //zmniejsza wartość amount pola NeededIngredient Fridge.DeleteIngredientFromDataBase(Fridge.Window.DataBase, oldestIngredient); //usuwa wykorzystany składnik z BD oldestIngredient = FindOldestIngredient(neededIngredient); // odnajduje kolejny najstarszy składnik PullIngredientFromFridge(NeededIngredient); //kontynuuje pobieranie z "lodówki" ze zmniejszonym wymaganiem } } catch (MySqlException ex) { MessageBox.Show(ex.Message, "FoodPuller.PullAllIngredients"); } catch (Exception ex) { MessageBox.Show(ex.Message, "FoodPuller.PullAllIngredients"); } }
public override void AddIngredientToDatabase(AbstractIngredient ingredient) { try { string CommandString = $"INSERT INTO FridgeContent (Name, Amount, ExpiryDate) " + $"VALUES(@Name, @Amount, @ExpiryDate)"; using (MYSQLConnection = new MySql.Data.MySqlClient.MySqlConnection(ConnectionString)) { MYSQLConnection.Open(); using (MYSQLCommand = new MySql.Data.MySqlClient.MySqlCommand(CommandString, MYSQLConnection)) { MYSQLCommand.Parameters.AddWithValue("@Name", ShortName(ingredient.Name)); MYSQLCommand.Parameters.AddWithValue("@Amount", ingredient.Amount); MYSQLCommand.Parameters.AddWithValue("@ExpiryDate", ingredient.ExpiryDate); MYSQLCommand.ExecuteNonQuery(); } MYSQLConnection.Close(); } } catch (InvalidOperationException ex) { MessageBox.Show(ex.Message, "FreeSQL.AddIngredient"); } catch (MySqlException ex) { MessageBox.Show(ex.Message, "FreeSQL.AddIngredient"); } catch (Exception ex) { MessageBox.Show(ex.Message, "FreeSQL.AddIngredient"); } }
public void DeleteIngredientFromDataBase(OnlineDataBase dataBase, AbstractIngredient ingredient) //usuwa wybrany składnik z { //bazy danych wykorzystując narzędzia dataBase.DeleteIngredientFromDatabase(ingredient); //należące bezpośrednio do klasy database Content.Remove(ingredient); //i usuwa je od razu z listy }
public void AddIngredient(AbstractIngredient ingredient) //umożliwia dodawanie składników na listę { Content.Add(ingredient); }
public abstract void DeleteIngredientFromDatabase(AbstractIngredient ingredient);
public abstract void AddIngredientToDatabase(AbstractIngredient ingredient);
/// <summary> /// Writes down all the content of passed ingredient and returns it as a string. /// </summary> /// <param name="ingredient"></param> /// <returns></returns> public string FromIngredientToString(AbstractIngredient ingredient) //metoda tworząca string gotowy do umieszczenia na { //listBoxie string output = ingredient.Name + "-" + ingredient.Amount.ToString(); //metoda rozdziela poszczególne składowe składnika return(output); }