Commit b9dbe141 authored by jithushan.umaipalan's avatar jithushan.umaipalan

Initial Commit

parents
1. Use an ArrayList to store a list of all animals in the zoo ✔
2. Display the details of an individual animal ✔
3. Display the details of each animal in an ArrayList ✔
4. Generate an ArrayList of all animals of a particular type ✔
5. Output “Food required” for all animals in an ArrayList (might be all animals, by be a list from 4) ✔
6. Output all animals from a specific environment. ✔
\ No newline at end of file
import java.util.Collection;
import java.util.Set;
import models.*;
import models.Animal.AnimalType;
import models.Animal.Habitat;
import models.animals.*;
public class ZooModule {
public static void main(String[] args) {
// create some animals
Animal lion = new Lion("Lion", 3, "meat", Habitat.FOREST, AnimalType.MAMMALS);
Animal bird = new Bird("Bird", 1, "seeds", Habitat.ARTIC, AnimalType.BIRDS);
Animal fish = new Fish("Fish", 1, "bread", Habitat.ARTIC, AnimalType.FISH);
Animal reptile = new Crocodile("Reptile", 1, "meat", Habitat.ARTIC, AnimalType.REPTILES);
// set animals habitat and type with setter methods
lion.setHabitat(Habitat.FOREST);
lion.setAnimalType(AnimalType.MAMMALS);
// create a collection of animals
Collection<Animal> animals = Set.of(lion, bird, fish, reptile);
Zoo zoo = new Zoo(animals);
// print all animals
zoo.displayAnimals();
// display one animal
zoo.displayAnimal(lion);
// print the food requirements for each animal
zoo.outputFoodRequirements();
// print the animals in the forest
zoo.displayAnimalsByHabitat(Habitat.FOREST);
// print the animals of the type
Collection<Animal> mams = zoo.outputAnimalsByType(AnimalType.MAMMALS);
zoo.displayAnimals(mams);
}
}
package models;
public abstract class Animal {
public enum AnimalType {
MAMMALS, BIRDS, FISH, REPTILES
}
public enum Habitat {
DESSERT, ARTIC, JUNGLE, FOREST
}
private String name;
private String consumable;
private int age;
private AnimalType animalType;
private Habitat habitat;
public Animal(String name, int age, String consumable, Habitat habitat, AnimalType animalType) {
this.name = name;
this.age = age;
this.consumable = consumable;
this.habitat = habitat;
this.animalType = animalType;
}
// setters
public void setHabitat(Habitat habitat) {
this.habitat = habitat;
}
public void setAnimalType(AnimalType animalType) {
this.animalType = animalType;
}
// getters
public String getName() {
return name;
}
public AnimalType getType() {
return animalType;
}
public int getAge() {
return age;
}
public Habitat getHabitat() {
return habitat;
}
public String getConsumable() {
return consumable;
}
}
package models;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import models.Animal.AnimalType;
import models.Animal.Habitat;
public class Zoo {
private Collection<Animal> animals;
public Zoo(Collection<Animal> animals) {
this.animals = animals;
}
public Collection<Animal> getAnimals() {
return animals;
}
// print a specific animal
public void displayAnimal(Animal animal) {
System.out.println(
animal.getName() + " " + animal.getAge() + " " + animal.getHabitat() + " " + animal.getType());
}
// print all animals in the zoo
public void displayAnimals() {
for (Animal animal : this.getAnimals()) {
System.out.println("Name : " + animal.getName());
System.out.println("Age : " + animal.getAge());
System.out.println("Consumable : " + animal.getConsumable());
System.out.println("Habitat : " + animal.getHabitat());
System.out.println("Type : " + animal.getType());
System.out.println("\n");
}
}
// Overloaded method to print all animals in the zoo
public void displayAnimals(Collection<Animal> animals) {
for (Animal animal : animals) {
System.out.println("Name : " + animal.getName());
System.out.println("Age : " + animal.getAge());
System.out.println("Consumable : " + animal.getConsumable());
System.out.println("Habitat : " + animal.getHabitat());
System.out.println("Type : " + animal.getType());
System.out.println("\n");
}
}
// output the food requirements for each animal
public void outputFoodRequirements() {
for (Animal animal : this.getAnimals()) {
System.out.println(animal.getName() + " needs " + animal.getConsumable() + " to live.");
}
}
// print the animals in the zoo from a specific habitat
public void displayAnimalsByHabitat(Habitat habitat) {
System.out.println("Animals in the " + habitat + " habitat:");
for (Animal animal : this.getAnimals()) {
if (animal.getHabitat() == habitat) {
System.out.println(animal.getName());
}
}
}
// generete a collection of animals by their type
public Set<Animal> outputAnimalsByType(AnimalType type) {
// create a collection of animals
Set<Animal> animalT = new HashSet<>();
// loop through the animals and add the animals of the type to the collection
for (Animal animal : this.getAnimals()) {
if (animal.getType() == type) {
animalT.add(animal);
}
}
return animalT;
}
}
package models.animals;
import models.Animal;
public class Bird extends Animal {
public Bird(String name, int age, String consumable, Habitat habitat, AnimalType animalType) {
super(name, age, consumable, habitat, animalType);
}
}
\ No newline at end of file
package models.animals;
import models.Animal;
public class Crocodile extends Animal {
public Crocodile(String name, int age, String consumable, Habitat habitat, AnimalType animalType) {
super(name, age, consumable, habitat, animalType);
}
}
\ No newline at end of file
package models.animals;
import models.Animal;
public class Fish extends Animal {
public Fish(String name, int age, String consumable, Habitat habitat, AnimalType animalType) {
super(name, age, consumable, habitat, animalType);
}
}
\ No newline at end of file
package models.animals;
import models.Animal;
public class Lion extends Animal {
public Lion(String name, int age, String consumable, Habitat habitat, AnimalType animalType) {
super(name, age, consumable, habitat, animalType);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment