实施处理食物食谱(名称、描述)的 Recipes class。 class 应该有一个添加(添加一个新的配方),
Implement the Recipes class, which handle food recipes (name, description). The class should have an add (add a new recipe),
我有点困惑,关于我是否存储了食谱名称“TükőrTojás”及其描述,因为即使我通过了添加测试部分的第一个测试,当涉及到时,删除部分,我第一次测试失败,我的数组大小更改为 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.\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());
}
```
您需要为 Recipe 创建一个 JavaBean 以将其与您的 ArrayList 一起使用,因此您可能 ArrayList<Recipe>
.
而不是 ArrayList<String>
RecipeBean.java
对于这个例子,我将创建一个名为 RecipeBean
的 JavaBean,它将包含 Strings
recipeName
和 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 = "";
}
}
上面的 class 将充当单个食谱的 Recipe
对象或项目。
Recipe.java
然后在你的 class 调用 Recipe.class
中我将声明 ArrayList
现在用 RecipeBean
而不是 String
以获得更多控制。
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));
}
}
}
我有点困惑,关于我是否存储了食谱名称“TükőrTojás”及其描述,因为即使我通过了添加测试部分的第一个测试,当涉及到时,删除部分,我第一次测试失败,我的数组大小更改为 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.\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());
}
```
您需要为 Recipe 创建一个 JavaBean 以将其与您的 ArrayList 一起使用,因此您可能 ArrayList<Recipe>
.
ArrayList<String>
RecipeBean.java
对于这个例子,我将创建一个名为 RecipeBean
的 JavaBean,它将包含 Strings
recipeName
和 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 = "";
}
}
上面的 class 将充当单个食谱的 Recipe
对象或项目。
Recipe.java
然后在你的 class 调用 Recipe.class
中我将声明 ArrayList
现在用 RecipeBean
而不是 String
以获得更多控制。
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));
}
}
}