I'm kinda confused , about if im storing the Name of the Recipe "TükőrTojás" and the desc of it , because even though, I pass the first test in the add test section, when it comes to , the delete section, the , i fail the first test, my array size changes to 2, I guess its because the name there is already defined ? Do i have to, somehow, store the already defined name into my Array list? If i, have to do that , how should i?
public class Recipes {
/*
Implement the Recipes class, which handle food recipes (name, description).
The class should have an add (add a new recipe),
*/
ArrayList<String> recipes = new ArrayList<>();
public void add(String name, String desc)
{ Collections.addAll(recipes,name,desc);
System.out.println(recipes.get(0));
System.out.println(recipes.get(1));
}
public void delete(String name)
{
recipes.remove(name);
}
}
@Test
public void testDelete() {
Recipes recipes = new Recipes();
String name = "Tükörtojás";
recipes.add(name, "1. Az olajat egy serpenyőben kellőképp felforrósítjuk és óvatosan beleütjük"
" a tojásokat.\r\n2. Keverés nélkül készre sütjük, míg a tojásfehérje megsül, de a sárgája"
" folyós marad.\r\n3. Hogy jobban átsüljön, a tojásfehérjét egy villa segítségével óvatosan"
" megmozgathatjuk.");
assertEquals(1, recipes.recipes.size()); // The test i fail
recipes.delete(name);
assertEquals(0, recipes.recipes.size());
}
```
CodePudding user response:
You need to create a JavaBean for Recipe to use it with your ArrayList, so instead of ArrayList<String>
you would probably have ArrayList<Recipe>
.
RecipeBean.java
For this example I will create a JavaBean named RecipeBean
which will contain the Strings
recipeName
and recipeDescription
public class RecipeBean {
private String recipeName = "", recipeDescription = "";
public RecipeBean() {
}
public String getRecipeName() {
return recipeName;
}
public void setRecipeName(String recipeName) {
this.recipeName = recipeName;
}
public String getRecipeDescription() {
return recipeDescription;
}
public void setRecipeDescription(String recipeDescription) {
this.recipeDescription = recipeDescription;
}
public void clear() {
this.recipeName = "";
this.recipeDescription = "";
}
}
The class above will act as the Recipe
object or item for a single Recipe.
Recipe.java
Then in your class called Recipe.class
I will declare the ArrayList
now with the RecipeBean
instead of String
for more control.
ArrayList<RecipeBean> recipes = new ArrayList();
import java.util.ArrayList;
public class Recipe {
/*
Implement the Recipes class, which handle food recipes (name, description). The class should have an add (add a new recipe)
*/
ArrayList<RecipeBean> recipes = new ArrayList<>();
/**
* Function to add recipe to recipes ArrayList
*
* @param recipeName - Recipe Name
* @param recipeDescription - Recipe Description
*/
public void addRecipe(final String recipeName, final String recipeDescription) {
System.out.println("Adding recipe : " recipeName);
RecipeBean recipeBean = new RecipeBean();
recipeBean.setRecipeName(recipeName); // Set recipeName to JavaBean
recipeBean.setRecipeDescription(recipeDescription); // Set recipeDescription to JavaBean
// Check if ArrayList is null
if (recipes != null) {
recipes.add(recipeBean); // Add java bean to ArrayList
}
}
/**
* Function to delete recipe to recipes ArrayList
*
* @param recipeName - Recipe Name
*/
public void deleteRecipe(final String recipeName) {
System.out.println("Deleting recipe : " recipeName);
// Check if ArrayList is null
if (recipes != null) {
// Loop through ArrayList and remove current object if name matches passed parameter
recipes.removeIf(recipe -> recipe.getRecipeName().equalsIgnoreCase(recipeName));
}
}
}