实现Recipes类,该类处理食物配方(名称、描述).该类应该有一个add(添加一个新配方)



我有点困惑,我是否存储了配方名称"TükõrTojás"和它的desc,因为即使我通过了添加测试部分的第一个测试,当涉及到删除部分时,我第一个测试失败了,我的数组大小变为2,我想这是因为那里的名称已经定义了吗?我是否必须以某种方式将已经定义的名称存储到我的数组列表中?如果我必须那样做,我该怎么做?

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.rn2. 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.rn3. 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());
}
```

您需要为Recipe创建一个JavaBean,以便将其与ArrayList一起使用,因此您可能会使用ArrayList<String>而不是ArrayList<Recipe>

RecipeBean.java

对于这个例子,我将创建一个名为RecipeBean的JavaBean,它将包含StringsrecipeNamerecipeDescription

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 = "";
}
}

上面的类将充当单个Recipe的Recipe对象或项。

配方.java

然后,在名为Recipe.class的类中,我将用RecipeBean而不是String来声明ArrayList,以获得更多控制。

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));
}
}
}

相关内容

  • 没有找到相关文章

最新更新