在java中将ArrayList分配给列表

在java中将ArrayList分配给列表,java,list,arraylist,junit,Java,List,Arraylist,Junit,我正在实现可以考虑以下junit测试的代码: package it.unica.pr2.pizze.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import static org.junit.Assert.assertTrue; import org.junit.Test; import org.junit.Ignore; import org.junit.ru

我正在实现可以考虑以下junit测试的代码:

package it.unica.pr2.pizze.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.util.*; 
import it.unica.pr2.pizze.*; 

@RunWith(JUnit4.class)
public class TestPizza {

  @Test
    public void test1() {
      Ingrediente mozzarella = new Ingrediente("mozzarella",50);
      Ingrediente pomodoro = new Ingrediente("pomodoro",10);
      Ingrediente[] ingredienti = new Ingrediente[] {mozzarella, pomodoro}
      Pizza pizzaMargherita = new Pizza(ingredienti);
      assertTrue( pizzaMargherita.calorie() == 60 );
      List ingredientiMargherita = pizzaMargherita;
        assertTrue(ingredientiMargherita.size() ==2);
        assertTrue(ingredientiMargherita.get(0) == mozzarella);
        assertTrue(ingredientiMargherita.get(1) == pomodoro);
     }
这是我的班级:比萨饼

package it.unica.pr2.pizze;
import java.util.ArrayList;
import java.util.List;

public class Pizza  {


    private ArrayList<Ingrediente> ingredienti;


    public Pizza(Ingrediente[] ing) {

        this.ingredienti = new ArrayList<>();

        int i = 0;
        while (i < ing.length) {

            this.ingredienti.add(ing[i]);
            i++;
        }

    }

    public double calorie(){

        double sumaCalorie = 0;

        for(Ingrediente elem: this.ingredienti)
            sumaCalorie += elem.getCalorie();

        return sumaCalorie;

    }
}
package it.unica.pr2.pizze;
导入java.util.ArrayList;
导入java.util.List;
公共级比萨饼{
私人ArrayList Ingrediti;
公共比萨饼(Ingrediente[]ing){
this.ingredienti=新的ArrayList();
int i=0;
而(i
还有另一类:Ingredinte

package it.unica.pr2.pizze;


public class Ingrediente  {


    private String nomeIngrediente;
    private double calorie;



    public Ingrediente(String nomeIngrediente, double calorie) throws IngredienteNonValidoException {

        this.nomeIngrediente = nomeIngrediente;
        if (calorie < 0) throw new IngredienteNonValidoException();
        else
            this.calorie = calorie;
    }

    public void setNomeIng(String nomeIngrediente) {
        this.nomeIngrediente = nomeIngrediente;
    }

    public void setCalorie(double calorie) {

        this.calorie = calorie;
    }

    public String getNomeIng() {

        return this.nomeIngrediente;
    }

    public double getCalorie() {
        return this.calorie;
    }

}
package it.unica.pr2.pizze;
公共课{
私有字符串命名;
私人双卡;
public Ingrediente(字符串名为Rediente,双卡路里)抛出IngRedientNonAlidoException{
this.nomeIngrediente=nomeIngrediente;
如果(卡路里<0)抛出新的IngRedientNonAlidoException();
其他的
这个。卡路里=卡路里;
}
公共void集合命名(字符串命名){
this.nomeIngrediente=nomeIngrediente;
}
公共热量(双倍热量){
这个。卡路里=卡路里;
}
公共字符串getnoming(){
返回此.nomeIngrediente;
}
公共双卡路里(){
归还这个。卡路里;
}
}
运行测试后,我出现以下错误:

错误:不兼容类型:比萨饼无法转换为列表

List ingredientimargreta=pizzamagreta


因此,我不知道如何仅使用运算符=,将ArrayList转换为列表,我无法修改junit测试代码。

如果无法修改赋值,则必须执行以下操作:

public class Pizza implements List {
...
}
或者类似的东西,比如

public class Pizza extends AbstractList {
...
}

在单元测试的下一行,您正在将
比萨饼
类型分配给
列表

List ingredientiMargherita = pizzaMargherita;
将其更改为:

List<Ingrediente> ingredientiMargherita = pizzaMargherita.getIngredienti();

我在没有编辑junit测试代码的情况下找到了一个解决方案,我不得不修改Pizza.java,如下所示:

package it.unica.pr2.pizze;
import java.util.ArrayList;
import java.util.*;

public class Pizza extends ArrayList   {

    public Pizza(Ingrediente[] ing) {

        for(Ingrediente elem: ing) {

            this.add(elem);
        }

    }

    public int calorie() {
        int calorie = 0;
        for(Object i : this) {
            calorie += ((Ingrediente)i).getCalorie();
        }
        return calorie;
    }


} 

现在测试已正确通过。

我99%确定单元测试代码中有错误。如果这是一项作业,请回到你的教授那里,询问他们是否真的是指“List INGREDIENTIARGERITA=PIZAMARGERITA;”,而不是“List INGREDIENTIARGERITA=PIZAMARGERITA.getIngrediente();”。否则唯一的解决方案就是markspace的。是的,测试代码相当可疑。我会证实这没有错。需要像Pizza这样的值类来实现List有点奇怪。要么是这样,要么就是OP试图重新编写以运行程序。在单元测试中没有错误,我找到的解决方案如下。这也可行,但当OP说他/她无法修改赋值时,我假设这样修改测试代码是不可能的。如果可能,这可能是更好的解决方案。从设计角度来看,这是一个糟糕的解决方案。但这是唯一能起作用的+是的,我知道,有点难看。我认为你关于测试用例可能是一个拼写错误的评论可能值得OP检查。如果你这样做的话,没有任何批评的意图。只是想确保人们不会在不必要的地方尝试这样做。
package it.unica.pr2.pizze;
import java.util.ArrayList;
import java.util.*;

public class Pizza extends ArrayList   {

    public Pizza(Ingrediente[] ing) {

        for(Ingrediente elem: ing) {

            this.add(elem);
        }

    }

    public int calorie() {
        int calorie = 0;
        for(Object i : this) {
            calorie += ((Ingrediente)i).getCalorie();
        }
        return calorie;
    }


}