From b986fab515901b2a247162c614ece41eea1b0211 Mon Sep 17 00:00:00 2001 From: Selebrator Date: Mon, 8 Apr 2019 00:22:21 +0200 Subject: [PATCH] Initial commit for #1 --- src/main/java/_1/_1/Animal.java | 125 +++++++++++++++++++++++++++++++ src/main/java/_1/_1/BioTest.java | 32 ++++++++ src/main/java/_1/_1/Plant.java | 27 +++++++ 3 files changed, 184 insertions(+) create mode 100644 src/main/java/_1/_1/Animal.java create mode 100644 src/main/java/_1/_1/BioTest.java create mode 100644 src/main/java/_1/_1/Plant.java diff --git a/src/main/java/_1/_1/Animal.java b/src/main/java/_1/_1/Animal.java new file mode 100644 index 0000000..16ddd6d --- /dev/null +++ b/src/main/java/_1/_1/Animal.java @@ -0,0 +1,125 @@ +package _1._1; + +import java.util.*; +import java.util.stream.Collectors; + +public class Animal { + private String name; + private Plant[] plantFoods; //null if empty + private Animal[] animalFoods; //null if empty + + public Animal(String name) { + this.name = name; + } + + /** + * Gives the animal the ability to eat a new plant + */ + public void addFood(Plant food) { + if(this.plantFoods == null) { + this.plantFoods = new Plant[10]; + } + for(int i = 0; i < this.plantFoods.length; i++) { + if(this.plantFoods[i] == null) { + this.plantFoods[i] = food; + } + } + } + + /** + * Gives the animal the ability to eat a new animal + */ + public void addFood(Animal food) { + if(this.animalFoods == null) { + this.animalFoods = new Animal[10]; + } + for(int i = 0; i < this.animalFoods.length; i++) { + if(this.animalFoods[i] == null) { + this.animalFoods[i] = food; + } + } + } + + //collects all non-null entries from an array in a set + private static Set nonNullEntries(T[] array) { + if(array == null) { + return new HashSet<>(); + } + return Arrays.stream(array) + .filter(Objects::nonNull) + .collect(Collectors.toSet()); + } + + /** + * Provides read-only access to the plants this animal eats. + */ + public Collection getPlantFoods() { + return nonNullEntries(this.plantFoods); + } + + /** + * Provides read-only access to the animals this animal eats. + */ + public Collection getAnimalFoods() { + return nonNullEntries(this.animalFoods); + } + + /* + * Es gibt keine setter für die Nahrung, weil ich dann keine Annahmen mehr + * über die beiden Arrays machen könnte, ohne dem Anwender alle implementation details + * zu verraten und mich darauf zu verlassen das sie eingehalten werden. + * + * Das ist zwar zu diesem Zeitpunkt kein Problem, würde es aber erschweren + * neue Funktionen hinzuzufügen. + * + * Lieber würde ich eine andere Datenstruktur wie Listen oder Sets verwenden, + * aber leider verbietet die Aufgabenstellung dies. + */ + + /** + * @return true if this animal can eat at least one plant. + */ + /* + * Könnte private sein, aber ich habe mich dagegen entschieden weil ich diese Methoden + * für ähnlich praktisch wie isCarnivore(), isOmnivore() und isHerbivore() halte. + */ + public boolean eatsPlants() { + return this.plantFoods != null; + } + + /** + * @return true if this animal can eat at least one animal. + */ + public boolean eatsAnimals() { + return this.animalFoods != null; + } + + /** + * @return true if this animal can only eat animals but no plants. + */ + public boolean isCarnivore() { + return this.eatsAnimals() && !this.eatsPlants(); + } + + /** + * @return true if this animal can eat plants and animals + */ + public boolean isOmnivore() { + return this.eatsAnimals() && this.eatsPlants(); + } + + /** + * @return true if this animal can only eat plants but no animals. + */ + public boolean isHerbivore() { + return !this.eatsAnimals() && this.eatsPlants(); + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/main/java/_1/_1/BioTest.java b/src/main/java/_1/_1/BioTest.java new file mode 100644 index 0000000..1e180dd --- /dev/null +++ b/src/main/java/_1/_1/BioTest.java @@ -0,0 +1,32 @@ +package _1._1; + +public class BioTest { + public static void main(String[] args) { + Plant grass = new Plant("Grass", "is green"); + Plant berry = new Plant("Berry", "is red"); + Animal zebra = new Animal("Zebra"); + zebra.addFood(grass); + Animal lion = new Animal("Lion"); + lion.addFood(zebra); + Animal bear = new Animal("Bear"); + bear.addFood(berry); + Animal fish = new Animal("Fish"); + bear.addFood(fish); + + System.out.println(getFoodClassDescription(zebra)); + System.out.println(getFoodClassDescription(lion)); + System.out.println(getFoodClassDescription(bear)); + } + + private static String getFoodClassDescription(Animal animal) { + if(animal.isHerbivore()) { + return animal.getName() + " is a herbivore"; + } else if(animal.isCarnivore()) { + return animal.getName() + " is a carnivore"; + } else if(animal.isOmnivore()) { + return animal.getName() + " is an omnivore"; + } else { + return animal.getName() + " is water fasting"; + } + } +} diff --git a/src/main/java/_1/_1/Plant.java b/src/main/java/_1/_1/Plant.java new file mode 100644 index 0000000..1da1c51 --- /dev/null +++ b/src/main/java/_1/_1/Plant.java @@ -0,0 +1,27 @@ +package _1._1; + +public class Plant { + private String name; + private String description; + + public Plant(String name, String description) { + this.name = name; + this.description = description; + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return this.description; + } + + public void setDescription(String description) { + this.description = description; + } +}