I am doing an exercise, which needs the DogSchool to implement the PetSchool. I intend to make a array list of the animals that registered in the pet school, and the dog school need to distinguish dogs from other animals. The characterisation of dogs is their shouting "Wau! Wau!". I have corrected. But still it can't distinguish dogs from cats.
Tier = Animal
This is the code of interface.
import java.util.ArrayList;
public interface PetSchool {
boolean add(Tier tier);
boolean addAll(ArrayList<Tier> tiere);
boolean remove(Tier tier);
ArrayList<Tier> getTiere();
}
This is the code of Implementation.
Please tell me what's wrong with it.
import java.util.ArrayList;
public class DogSchool implements PetSchool {
public ArrayList<Tier> tiere= new ArrayList<Tier>();
@Override
public boolean add(Tier t){
if(t.gibLaut().equalsIgnoreCase("Wau! Wau!")){
return tiere.add(t);
}
else {
return false;
} }
@ Override
public boolean addAll(ArrayList<Tier> tiere){
return this.tiere.addAll(tiere);
}
@Override
public boolean remove(Tier t){
if(!t.gibLaut().equalsIgnoreCase("Wau! Wau!")){
return tiere.remove(t);
}
else{
return false;
}
}
@Override
public ArrayList<Tier> getTiere() {
return new ArrayList<Tier>(this.tiere);
}
}
Well, the problem happens in the demoTest:
import java.util.ArrayList;
public class TierDemo {
public static void main(String[] args) {
System.out.println("Test Teil 2:");
DogSchool schule = new DogSchool();
schule.add(new Hund());
schule.add(new Katze());
schule.add(new Hund());
schule.add(new Katze());
schule.addAll(tiere);
for (Tier t : schule.getTiere()) {
System.out.println(t.gibLaut());
}
}
After compiling, it shows:
Test Teil 2:
Wau! Wau!
Wau! Wau!
Miau!
Wau! Wau!
which is better, but still it can't tell distinguish dogs from cats.