EN VI

How does a construction Collection<Interface> work in Java?

2024-03-15 20:30:09
How does a construction Collection work in Java?

I had encountered a problem. There are a class and an interface:

Class:

public class Food implements NutritionalElement {
private String name;
private double calories;
private double proteins;
private double carbs;
private double fat;
private HashMap<String, ArrayList<Double>> rawMaterialsList = new HashMap<String, ArrayList<Double>>();

public void defineRawMaterial(String name,
                                  double calories,
                                  double proteins,
                                  double carbs,
                                  double fat){
    this.name = name;
    this.calories = calories;
    this.proteins = proteins;
    this.carbs = carbs;
    this.fat = fat;
    ArrayList<Double> nutrision = new ArrayList<>();
    nutrision.add(this.calories);
    nutrision.add(this.proteins);
    nutrision.add(this.carbs);
    nutrision.add(this.fat);
    rawMaterialsList.put(this.name, nutrision);
}

/**
 * Retrieves the collection of all defined raw materials
 * @return collection of raw materials through the {@link NutritionalElement} interface 
 * problem is here      
 */
public Collection<NutritionalElement> rawMaterials(){

    
    return res;
}

And an interface:

package diet;



public interface NutritionalElement {
public String getName();

public double getCalories();


public double getProteins();


public double getCarbs();


public double getFat();


public boolean per100g();

}

And I am completely lost, I have never met something like that. I would ask to help me understand how I shpold complete the method in the class. Thank you.

EDIT: I don't understand how to implement this method:

public Collection<NutritionalElement> rawMaterials(){}

I should return a collection of interfaces or what. By the way, this is university project and the method should be implemented in the same manner as it was provided by the professor (I cannot change return type at all). Sorry for bad explanation at the beginning.

Solution:

Going by common sense, your structure is incorrect. The nutritional elements are carbs, fats, proteins, probably other stuff I am not aware of. It seems you should have classes representing each of those elements.

class Carbohydrates implements NutritionalElement {
  //implement the methods
}

Create similar classes for the other elements.

Then Food should look like:

class Food {

  private List<NutritionalElement> elements;
  
  //constructors

  //other methods, if necesarry

  public Collection<NutritionalElement> rawMaterials(){  
    return this.elements;
  }
}

The food is not a NutritionalElement, but it has many NutritionalElements.

Tbh it seems the interface also needs refactoring, because carbohydrates having methods to get fats and proteins seems strange.

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login