Ejemplo n.º 1
0
        public bool Check(AbstractRecipe recipe) //porównuje ilości dostępnych składników z tymi,
                                                 //które są potrzebne w przepisie
        {
            List <AbstractIngredient> AvailibleIngredients = SumFridgeIngredients();
            int Count = recipe.ListOfIngredients.Count;//przechowuje ilość składników,

            //które jeszcze zostały do odnalezienia i porówniana

            foreach (AbstractIngredient recipeIngredient in recipe.ListOfIngredients)
            {
                for (int i = 0; i < AvailibleIngredients.Count; i++)
                {
                    if (AvailibleIngredients[i].Name == recipeIngredient.Name)
                    {
                        if (AvailibleIngredients[i].Amount < recipeIngredient.Amount) //jeśli okazuje się,
                                                                                      //że któregokolwiek składnika jest za mało,
                                                                                      //pętla jest przerywana i metoda zwraca false
                        {
                            return(false);
                        }
                        else
                        {
                            Count -= 1;//jeśli ilość się zgadza, odznacza się, że jeden ze składników został znaleziony
                        }
                    }
                    if (Count <= 0)
                    {
                        return(true);           //tylko w przypadku, kiedy wszystkie składniki zostały znalezione,
                    }
                    //metoda zwraca true
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
        public override void AddRecipeToDatabase(AbstractRecipe recipe) //zapisuje nowy przepis w bazie 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 Recipes (Name, Ingredients, Description) " + //polecenie dodania rzędu w tabeli
                                       $"VALUES(@Name, @Ingredients, @Description)";
                using (LocalConnection = new MySqlConnection(ConnectionString))
                {
                    LocalConnection.Open(); //otwiera połączenie
                    using (LocalCommand = new MySqlCommand(CommandString, LocalConnection))
                    {                       //wypełnia kolumny wartościami z przepisu przekazanego metodzie
                        LocalCommand.Parameters.AddWithValue("@Name", ShortName(recipe.Name));
                        LocalCommand.Parameters.AddWithValue("@Ingredients", ConvertToString(recipe));
                        LocalCommand.Parameters.AddWithValue("@Description", recipe.Description);
                        LocalCommand.ExecuteNonQuery();
                    }
                    LocalConnection.Close();//zamyka połączenie
                }
            }
            catch (InvalidOperationException ex) { MessageBox.Show(ex.Message); }
            catch (MySqlException) { MessageBox.Show($"Adding the new recipe failed. \n" +
                                                     $"Maybe you already have recipe called {recipe.Name}?"); }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
Ejemplo n.º 3
0
        public override void AddRecipeToDatabase(AbstractRecipe recipe) //zapisuje nowy przepis w bazie danych
        {
            try
            {
                string CommandString = $"INSERT INTO Recipes (Name, Ingredients, Description) " +
                                       $"VALUES(@Name, @Ingredients, @Description)";
                using (MYSQLConnection = new MySql.Data.MySqlClient.MySqlConnection(ConnectionString))
                {
                    MYSQLConnection.Open();
                    using (MYSQLCommand = new MySql.Data.MySqlClient.MySqlCommand(CommandString, MYSQLConnection))
                    {
                        MYSQLCommand.Parameters.AddWithValue("@Name", ShortName(recipe.Name));
                        MYSQLCommand.Parameters.AddWithValue("@Ingredients", ConvertToString(recipe));
                        MYSQLCommand.Parameters.AddWithValue("@Description", recipe.Description);
                        MYSQLCommand.ExecuteNonQuery();
                    }

                    MYSQLConnection.Close();
                }
            }
            catch (InvalidOperationException ex) { MessageBox.Show(ex.Message, "FreeSQL.AddRecipe"); }
            catch (MySqlException) { MessageBox.Show($"Adding the new recipe failed. \n" +
                                                     $"Maybe you already have recipe called {recipe.Name}?", "FreeSQL.AddRecipe"); }
            catch (Exception ex) { MessageBox.Show(ex.Message, "FreeSQL.AddRecipe"); }
        }
Ejemplo n.º 4
0
        public string ConvertToString(AbstractRecipe recipe) // lokalna metoda zwracająca listę składników w postaci stringa,
        {                                                    //żeby ułatwić jego zapisanie w bazie danych
            string output = "";                              //pole na docelowy string

            Converter = new RecipeConverter();               //zawiera narzędzia służące konwertowaniu typu Recipe na string i na odwrót
            for (int i = 0; i < recipe.ListOfIngredients.Count; i++)
            {
                output += Converter.FromIngredientToString(recipe.ListOfIngredients[i]);//dodaje kolejne nazwy i ilości do "output"
                if (i < recipe.ListOfIngredients.Count - 1)
                {
                    output += ";";//rozdziela kolejne składniki średnikiem
                }
            }
            return(output);
        }
Ejemplo n.º 5
0
        public string ConvertToString(AbstractRecipe recipe) // zwraca listę składników w postaci stringa
        {
            string output = "";

            Converter = new RecipeConverter();

            for (int i = 0; i < recipe.ListOfIngredients.Count; i++)
            {
                output += Converter.FromIngredientToString(recipe.ListOfIngredients[i]);
                if (i < recipe.ListOfIngredients.Count - 1)
                {
                    output += ";";
                }
            }
            return(output);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Pulls the ingredients from the list from the fridge.
 /// </summary>
 /// <param name="recipe"></param>
 public void PullAllForRecipe(AbstractRecipe recipe) // wyciąga z magazynu/lodówki wszyskie składniki z listy
 {
     if (Fridge.StateCheck.Check(recipe))            //przy użyciu metody Check klasy stateCheck sprawdza, czy w lodówce jest
                                                     //wystarczająco dużo składników na wybrany przepis, jeśli tak, to kontynuuje
     {
         try
         {
             foreach (AbstractIngredient ingredient in recipe.ListOfIngredients) //wyciąga każdy składnik po kolei korzystając
             {                                                                   //z lokalnej metody PullIngredientFromFridge
                 PullIngredientFromFridge(ingredient);
             }
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
     }
 }
Ejemplo n.º 7
0
 public bool IsThereEnough(AbstractRecipe recipe)                                //deleguje metodę porównującą zawartość
 {                                                                               //lodówki z listą z przepisu
     return(StateCheck.Check(recipe));
 }
Ejemplo n.º 8
0
 public abstract void AddRecipeToDatabase(AbstractRecipe recipe);