I have a GenericContainer class and a FIFOContainer class that extends the generic one. My problem appears when trying to use the takeout() method. It does not recognize that I hold values in my FIFOContainer ArrayList. I suspect this has something to do with how I defined the constructors but for the life of me cannot figure it out how to solve it.
A solution I thought of is defining a getter in the GenericContainer class and passing the value in the FIFOContainer class but I feel like this should not be needed.
public abstract class GenericContainer implements IBag {
private ArrayList<ISurprise> container;
public GenericContainer() {
this.container = new ArrayList<ISurprise>();
}
@Override
public void put(ISurprise newSurprise) {
this.container.add(newSurprise);
}
@Override
public void put(IBag bagOfSurprises) {
while (!bagOfSurprises.isEmpty()) {
System.out.println(bagOfSurprises.size());
this.container.add(bagOfSurprises.takeout());
}
}
@Override
public boolean isEmpty() {
if (this.container.size() > 0) {
return false;
}
return true;
}
@Override
public int size() {
if (isEmpty() == false) {
return this.container.size();
}
return -1;
}
}
public class FIFOContainer extends GenericContainer {
private ArrayList<ISurprise> FIFOcontainer;
public FIFOContainer() {
super();
this.FIFOcontainer = new ArrayList<ISurprise>();
}
public ISurprise takeout() {
if (isEmpty() == false) {
this.FIFOcontainer.remove(0);
ISurprise aux = this.FIFOcontainer.get(0);
return aux;
}
return null;
}
}