diff --git a/Java Classes & Interfaces/Interfaces/Animals/src/Animal.java b/Java Classes & Interfaces/Interfaces/Animals/src/Animal.java new file mode 100644 index 0000000..ae9a50c --- /dev/null +++ b/Java Classes & Interfaces/Interfaces/Animals/src/Animal.java @@ -0,0 +1,9 @@ +package src; + +public interface Animal { + + String animalName(); + String animalSound(); + double animalPrice(); + +} diff --git a/Java Classes & Interfaces/Interfaces/Animals/src/AnimalHelper.java b/Java Classes & Interfaces/Interfaces/Animals/src/AnimalHelper.java new file mode 100644 index 0000000..299e23e --- /dev/null +++ b/Java Classes & Interfaces/Interfaces/Animals/src/AnimalHelper.java @@ -0,0 +1,38 @@ +package src; + +public class AnimalHelper { + + private final Animal[] animals; + private double averagePrice; + + public AnimalHelper(Animal[] animals) { + this.animals = animals; + } + + public void displayInformation() { + /* Display the animal information */ + + int total = 0; + double sum = 0.0; + + for (Animal animal : animals) { + StringBuilder stringBuilder = new StringBuilder(); + + stringBuilder.append("\n\nName: " + animal.animalName()); + stringBuilder.append("\nSound: " + animal.animalSound()); + stringBuilder.append("\nPrice: £" + animal.animalPrice()); + + System.out.println(stringBuilder.toString()); + + total++; + sum += animal.animalPrice(); + } + averagePrice = sum / total; + } + + public void displayAveragePrice() { + /* Display the average price of all the animals */ + + System.out.printf("%nThe Average Price For All The Animals Are: £%.2f", averagePrice); + } +} diff --git a/Java Classes & Interfaces/Interfaces/Animals/src/AnimalMain.java b/Java Classes & Interfaces/Interfaces/Animals/src/AnimalMain.java new file mode 100644 index 0000000..a2e158b --- /dev/null +++ b/Java Classes & Interfaces/Interfaces/Animals/src/AnimalMain.java @@ -0,0 +1,17 @@ +package src; + +public class AnimalMain { + public static void main(String[] args) { + displayAnimals(); + } + + private static void displayAnimals() { + + // Creating an array of animal objects + AnimalHelper helper = new AnimalHelper(new Animal[] { new Cow(), new Cat(), new Dog(), new Horse() }); + + // Displaying information about the animals + helper.displayInformation(); + helper.displayAveragePrice(); + } +} diff --git a/Java Classes & Interfaces/Interfaces/Animals/src/Cat.java b/Java Classes & Interfaces/Interfaces/Animals/src/Cat.java new file mode 100644 index 0000000..427dda4 --- /dev/null +++ b/Java Classes & Interfaces/Interfaces/Animals/src/Cat.java @@ -0,0 +1,20 @@ +package src; + +public class Cat implements Animal { + + @Override + public String animalName() { + return "Cat"; + } + + @Override + public String animalSound() { + return "Meoooww"; + } + + @Override + public double animalPrice() { + return 29.99; + } + +} diff --git a/Java Classes & Interfaces/Interfaces/Animals/src/Cow.java b/Java Classes & Interfaces/Interfaces/Animals/src/Cow.java new file mode 100644 index 0000000..bc36e69 --- /dev/null +++ b/Java Classes & Interfaces/Interfaces/Animals/src/Cow.java @@ -0,0 +1,20 @@ +package src; + +public class Cow implements Animal { + + @Override + public String animalName() { + return "Cow"; + } + + @Override + public String animalSound() { + return "Moooo"; + } + + @Override + public double animalPrice() { + return 10.99; + } + +} diff --git a/Java Classes & Interfaces/Interfaces/Animals/src/Dog.java b/Java Classes & Interfaces/Interfaces/Animals/src/Dog.java new file mode 100644 index 0000000..3a37d58 --- /dev/null +++ b/Java Classes & Interfaces/Interfaces/Animals/src/Dog.java @@ -0,0 +1,20 @@ +package src; + +public class Dog implements Animal { + + @Override + public String animalName() { + return "Dog"; + } + + @Override + public String animalSound() { + return "Woof Woof"; + } + + @Override + public double animalPrice() { + return 15.99; + } + +} diff --git a/Java Classes & Interfaces/Interfaces/Animals/src/Horse.java b/Java Classes & Interfaces/Interfaces/Animals/src/Horse.java new file mode 100644 index 0000000..548140e --- /dev/null +++ b/Java Classes & Interfaces/Interfaces/Animals/src/Horse.java @@ -0,0 +1,20 @@ +package src; + +public class Horse implements Animal { + + @Override + public String animalName() { + return "Horse"; + } + + @Override + public String animalSound() { + return "Neighh"; + } + + @Override + public double animalPrice() { + return 59.99; + } + +} \ No newline at end of file