HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Animals in the Zoo

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
animalsthezoo

Problem

I am creating a zoo. In my zoo I want to have 3 types of animal: Mammal, Bird and Fish. I want to keep them in cages.

My implementation of animals is (classes for Mammal and Fish looks almost the same as Bird, only TypeEnum is different):

public class Bird implements Animal {
        private AnimalDetails animalDetails;
        private final BirdTypeEnum birdTypeEnum;

        public Bird(AnimalDetails animalDetails, BirdTypeEnum birdTypeEnum) {
            this.setAnimalDetails(animalDetails);
            this.birdTypeEnum = birdTypeEnum;
        }

        public String move() {
            return birdTypeEnum.move();
            // return birdTypeEnum.toString() + " fly";
        }

        public String getVoice() {
            return birdTypeEnum.getVoice();
        }
    // getters and setters toString method
}


The Animal Details is the simply class with name, sex, size, age. My interface has the possibility of using polymorphism:

public interface Animal
{
    public AnimalDetails getAnimalDetails();

    public String getVoice();

    public String move();

    public AnimalType getAnimalType();
}


The BirdTypeEnum looks like this (the same for Mammal and Fish):

```
public enum BirdTypeEnum implements AnimalType {
Eagle, Sparrow, Pigeon;

public String move() {
switch (this) {
case Eagle:
return Eagle.toString() + " fly";
case Pigeon:
return Pigeon.toString() + " fly";
case Sparrow:
return Sparrow.toString() + " fly";
default:
return this.toString() + " fly";
}
}

public String getVoice() {
switch (this) {
case Eagle:
return Eagle.toString() + " gives voice";
case Pigeon:
return Pigeon.toString() + " gives voice";
case Sparrow:
return Sparrow.toString() + " gives voice";
default:
return this.toString() + " gives voice";

Solution

There are two issues I can see that stand out. The first is that you are using the wrong level of detail for the birdTypeEnum field. The input as BirdTypeEnum is right, but the field should be AnimalType. You have:

public class Bird implements Animal {
    private AnimalDetails animalDetails;
    private final BirdTypeEnum birdTypeEnum;

    public Bird(AnimalDetails animalDetails, BirdTypeEnum birdTypeEnum) {
        this.setAnimalDetails(animalDetails);
        this.birdTypeEnum = birdTypeEnum;
    }

    .....

}


but the code should probably be:

public class Bird implements Animal {
    private AnimalDetails animalDetails;
    private final AnimalType birdType;

    public Bird(AnimalDetails animalDetails, BirdTypeEnum birdTypeEnum) {
        this.setAnimalDetails(animalDetails);
        this.birdType = birdTypeEnum;
    }

    .....

}


This leads to the possibility of actually including the AnimalType in with the AnimalDetails, but I am not sure you can go that far.

The reason this all works is because the enum implements the type methods, and you only use the interface methods, not the enum concepts.

It is good to use the enum to contain the BirdType details, but once you have the enum, you should use the interface methods to access them, not the enum definition

Now, about that enum, you have:

public enum BirdTypeEnum implements AnimalType {
    Eagle, Sparrow, Pigeon;

    public String move() {
        switch (this) {
        case Eagle:
            return Eagle.toString() + " fly";
        case Pigeon:
            return Pigeon.toString() + " fly";
        case Sparrow:
            return Sparrow.toString() + " fly";
        default:
            return this.toString() + " fly";
        }
    }

    public String getVoice() {
        switch (this) {
        case Eagle:
            return Eagle.toString() + " gives voice";
        case Pigeon:
            return Pigeon.toString() + " gives voice";
        case Sparrow:
            return Sparrow.toString() + " gives voice";
        default:
            return this.toString() + " gives voice";
        }
    }
}


But, this could be simplified by using some enum constructors.... like:

public enum BirdTypeEnum implements AnimalType {
    Eagle("soars", "cries"),
    Sparrow("flits", "chirps"),
    Pigeon("flaps", "coos");

    private final String move, voice;

    BirdTypeEnum(String move, String voice) {
        this.move = move;
        this.voice = voice;
    }

    public String move() {
        return this + " " + move;
    }

    public String getVoice() {
        return this + " " + voice;
    }
}

Code Snippets

public class Bird implements Animal {
    private AnimalDetails animalDetails;
    private final BirdTypeEnum birdTypeEnum;

    public Bird(AnimalDetails animalDetails, BirdTypeEnum birdTypeEnum) {
        this.setAnimalDetails(animalDetails);
        this.birdTypeEnum = birdTypeEnum;
    }

    .....

}
public class Bird implements Animal {
    private AnimalDetails animalDetails;
    private final AnimalType birdType;

    public Bird(AnimalDetails animalDetails, BirdTypeEnum birdTypeEnum) {
        this.setAnimalDetails(animalDetails);
        this.birdType = birdTypeEnum;
    }

    .....

}
public enum BirdTypeEnum implements AnimalType {
    Eagle, Sparrow, Pigeon;

    public String move() {
        switch (this) {
        case Eagle:
            return Eagle.toString() + " fly";
        case Pigeon:
            return Pigeon.toString() + " fly";
        case Sparrow:
            return Sparrow.toString() + " fly";
        default:
            return this.toString() + " fly";
        }
    }

    public String getVoice() {
        switch (this) {
        case Eagle:
            return Eagle.toString() + " gives voice";
        case Pigeon:
            return Pigeon.toString() + " gives voice";
        case Sparrow:
            return Sparrow.toString() + " gives voice";
        default:
            return this.toString() + " gives voice";
        }
    }
}
public enum BirdTypeEnum implements AnimalType {
    Eagle("soars", "cries"),
    Sparrow("flits", "chirps"),
    Pigeon("flaps", "coos");

    private final String move, voice;

    BirdTypeEnum(String move, String voice) {
        this.move = move;
        this.voice = voice;
    }

    public String move() {
        return this + " " + move;
    }

    public String getVoice() {
        return this + " " + voice;
    }
}

Context

StackExchange Code Review Q#70917, answer score: 4

Revisions (0)

No revisions yet.