Implemeted base classes; Refactored animals; Finished main.

This commit is contained in:
Jurn Wubben 2025-07-29 13:05:45 +02:00
parent 145a49ce7a
commit bd21c2ad30
18 changed files with 221 additions and 184 deletions

View file

@ -2,37 +2,65 @@ package com.ing.zoo;
import java.util.Scanner;
import com.ing.zoo.animals.*;
import com.ing.zoo.base.*;
public class Zoo {
public static void main(String[] args)
{
String[] commands = new String[4];
commands[0] = "hello";
commands[1] = "give leaves";
commands[2] = "give meat";
commands[3] = "perform trick";
Lion henk = new Lion();
henk.name = "henk";
Hippo elsa = new Hippo();
elsa.name = "elsa";
Pig dora = new Pig();
dora.name = "dora";
Tiger wally = new Tiger();
wally.name = "wally";
Zebra marty = new Zebra();
marty.name = "marty";
Animal[] animals = {
new Lion("henk"),
new Hippo("elsa"),
new Pig("dora"),
new Tiger("wally"),
new Zebra("marty")
};
Scanner scanner = new Scanner(System.in);
System.out.print("Voer uw command in: ");
System.out.print("Enter your command: ");
String input = scanner.nextLine();
if(input.equals(commands[0] + " henk"))
{
henk.sayHello();
String input = scanner.nextLine().trim();
String[] splittedInput = input.split("\\s+");
scanner.close(); // NOTE: You don't do this in the template for some reason; It's a resource leak.
if (splittedInput.length == 0) {
System.out.println("Please provide an command.");
return;
}
else
{
System.out.println("Unknown command: " + input);
if (splittedInput[0].equals("hello")) {
for (Animal animal : animals) {
if (splittedInput.length > 1 && animal.name.equals(splittedInput[1]))
animal.sayHello();
else if (splittedInput.length == 1) animal.sayHello();
}
return;
}
for (Animal animal : animals) {
switch (input) {
case "give leaves":
if (animal instanceof HerbivoreBehavior)
((HerbivoreBehavior) animal).eatLeaves();
break;
case "give meat":
if (animal instanceof CarnivoreBehavior)
((CarnivoreBehavior) animal).eatMeat();
break;
case "perform trick":
animal.performTrick();
break;
default:
System.err.println("Invalid command: " + input);
return;
};
}
return;
}
}