66 lines
1.9 KiB
Java
66 lines
1.9 KiB
Java
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)
|
|
{
|
|
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("Enter your command: ");
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|