Assign ArrayList to list in java

I am implementing some code that the following junit test can account for:

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

      

and here is my class: Pizza

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;

    }
}

      

and another class: Ingrediente

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

}

      

After running the test, I got the following error:

error: incompatible types: Pizza cannot be converted to List

List ingredientiMargherita = pizzaMargherita;

So i dont know how to convert ArrayList to List only using = operator, i cant change junit test code.

+3


source to share


3 answers


I found a solution without editing junit test code, I had to change Pizza.java like this:

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


} 

      



the test is now transmitted correctly.

+1


source


If you cannot change the assignment, you must do this:

public class Pizza implements List {
...
}

      



Or something similar, for example

public class Pizza extends AbstractList {
...
}

      

+3


source


In the line below unit test, you are assigning the Pizza

type List

:

List ingredientiMargherita = pizzaMargherita;

      

Change it to something like:

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

      

And add getIngredienti()

to revert List

to the class Pizza

:

public ArrayList<Ingrediente> getIngredienti(){
     return ingredienti;
}

      

+1


source







All Articles