1

Hello again stackoverflow, I have a question concerning List of Objects.

I have tried out writing some things, but can't find it though. How do I find an Object in a list of objects, when these objects are lists of its own?

this is what i have so far:

Recipe is a List of: {modification[], ingredients[], recipeBookName, recipeName}

public void removeFromBook(Recipe recipeName) {
    recipes = getRecipes();
    emptyRecipe = getEmptyPage(recipeBookName);

Now, I want to replace a Recipe which has a recipeName, by the emptyRecipe. I think it will be something like:

for(r in recipes) {
    if(r.recipeName == recipeName) {
        list.replace(Recipe, emptyRecipe)
    } else {

    }
}

any ideas? :)

here's my constructor for the Recipe class:

    public String[] modifications;
public String[] ingredients;
public String recipeName;
public String partOfRecipeBook;

public Recipe(String recipeName, String[] modifications, String[] ingredients, String recipeBookName){
    setRecipeModifications(modifications);
    setRecipeIngredients(ingredients);
    setRecipeName(recipeName);
    setRecipeBookName(recipeBookName);
}

StructureMap and Generics - How do I specify a default constructor?

I have a generic interface:

public interface IRepository<T> { ... }

I have an implementation like this:

public class Repository<T> {
    public Repository<T>() { ... }
}

StructureMap (v 2.6.3) is configured like this:

For(typeof(IRepository<>)).Use(typeof(Repository<>));

When I try and pull an IRepository<Something> out of StructureMap, I get a Repository<Something> as expected. Hooray!

Now I've added a second constructor, and the implementation looks like this:

public class Repository<T> {
    public Repository<T>() { ... }
    public Repository<T>(string database) { ... }
} 

When I try and get an IRepository<Something> now, I get an exception because it defaults to trying to use the new constructor with the parameter. Boo!

How can I change my StructureMap configuration so that it knows to use the parameter-less constructor?

4

2 に答える 2

2

文字列を等しいと比較する必要があることを除いて、あなたのアプローチはうまく見えます:

if(r.recipeName.equals(recipeName)) {

さらに簡単な方法は、レシピをマップに保存することです。

Map<String, Recipe> recipes = new HashMap<String, Recipe>();
recipes.put("Pizza with Lobster", new Recipe());

レシピを置き換えたい場合:

recipes.put("Pizza with Lobster", emptyRecipe);

古いレシピは置き換えられました。

于 2012-05-15T14:05:28.810 に答える
2

オブジェクト(一部は配列)を含むリストを使用することは、新しいオブジェクトレシピを定義してそれを使用するのが最善の方法ではありません。

public class Recipe {
    private List<Ingredient> ingredients;
    private List<Modification> modifications;
    private String bookName;
    private String book;
}

そうすれば、材料の交換がはるかに簡単になります。たとえば、レシピに次のような機能を与える

public void replaceIngredent(Ingredient oldIngredient, Ingredient newIngredient) {
    int index = ingredients.indexOf(oldIngredient);
    if (index != -1) {
        ingredients.remove(index);
        ingredients.add(index, newIngredient);
    }
}
于 2012-05-15T14:10:18.563 に答える