I am having the next code arhitecture:
- class candybox with arguments flavor and origin which is the parent class for classes: Lindt, Baravelli and ChocoAmor which will have as a plus arguments: height, width(depending of the box)
- class CandyBag which needs to contain an Arraylist that will represent a gift containing 1 candybox of type Lindt, 1 candybox of type Baravelli and 1 candyboxe of type ChocoAmor. Now my problem is that I do not know how to populate the arraylist from class candybag with objects from the 3 classes: Lindt, Baravelli and ChocoAmor.
I thought of this:
import java.util.*;
class CandyBag extends CandyBox{
ArrayList<CandyBox> candybag = new ArrayList<CandyBox>();
CandyBox Austria_cherry = new Lindt(20, 5.4, 19.2);
Candybox Italy_grape = new Baravelli(6.7,8.7);
Candybox France_coffee = new ChocAmor(5.5);
candybag.add(Austria_cherry);
candybag.add(Italy_grape);
candybag.add(France_coffee);
public void printArray(candybag){
for(String item: candybag){
System.out.println(item);
} }
And then in main class just to create a candybag object and call printArray method: CandyBag cadou = new CandyBag(); cadou.printArray(candybag); //i do not know exactly how to pass the arraylist from other class as parameter
Can you help me with this, how to print the arraylist with the 3 boxes?
I think you need to override the toString() method of each class to return the desired text. Also, the elements of
candybaglist are not string type.