我有點困惑,如果我存盤配方名稱“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());
}
```
uj5u.com熱心網友回復:
您需要為 Recipe 創建一個 JavaBean 以將其與 ArrayList 一起使用,因此ArrayList<String>您可能會使用ArrayList<Recipe>.
食譜豆.java
對于這個例子,我將創建一個名為的 JavaBean RecipeBean,它將包含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 = "";
}
}
上面的類將充當Recipe單個配方的物件或專案。
食譜.java
然后在您的班級中,Recipe.class我將使用而不是宣告ArrayList現在以獲得更多控制權。RecipeBeanString
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));
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/447542.html
上一篇:類內的Ursina更新功能
