/** Let's make a zoo -- the other files in this program define various * animals, and here we can create and play with them. * Because a zoo has several animals, and we don't know how many in advance, * we use an ArrayList instead of an array of animals. An ArrayList has all * the functionality and is more flexible than an array: it allows you to * grow or shrink whenever needed. */ import java.util.*; // where the ArrayList class is defined import java.io.*; public class Driver { public static void main(String [] args) throws IOException { ArrayList zoo = new ArrayList(); BufferedReader kbd = new BufferedReader(new InputStreamReader(System.in)); // This loop will interactively get input concerning // what the user wants to do with the animals. while (true) { System.out.println("\nCurrently we have " + zoo.size() + " animals."); System.out.print("(p)rint zoo, (a)dd animal, (d)elete, "); System.out.println("(e)xercise, (f)eed, (w)eigh, (q)uit program"); System.out.print("Enter your selection: "); char input = kbd.readLine().charAt(0); // A series of if/else statements will handle the individual commands. // It would be more efficient to read both the command and the // respective animal at the same time, rather than have separate prompts, // but it would be more difficult for the user to remember exactly // how to enter the commands. This implementation makes it easy to run. if (input == 'a') { System.out.print("(b)ird, (p)enguin, (f)ish, (r)eptile, (s)nake? "); char type = kbd.readLine().charAt(0); switch(type) { case 'b' : zoo.add(new Bird()); break; case 'f' : zoo.add(new Fish()); break; case 'p' : zoo.add(new Penguin()); break; case 'r' : zoo.add(new Reptile()); break; case 's' : zoo.add(new Snake()); break; default: System.out.println("Invalid animal type, sorry."); } } else if (input == 'd') { System.out.print("Enter number of animal to delete: "); int num = Integer.parseInt(kbd.readLine()); zoo.remove(num); } else if (input == 'p') { for (int i = 0; i < zoo.size(); ++i) System.out.println("#" + i + " --> " + zoo.get(i)); } // Take a close look at the calls to exercise() and feed() below. // It turns out that the ArrayList get() function returns simply // an Object, and we need to cast it to Animal, because exercise() // and feed() belong to the Animal class. This cast is not a problem, // because the ArrayList consists of animal objects (or specialized // versions of animals, which also have the ability to exercise and eat). else if (input == 'e') { System.out.print("Which animal do you want to exercise? "); int num = Integer.parseInt(kbd.readLine()); ((Animal)(zoo.get(num))).exercise(); } else if (input == 'f') { System.out.print("Which animal do you want to feed? "); int num = Integer.parseInt(kbd.readLine()); ((Animal)(zoo.get(num))).feed(); } else if (input == 'w') { System.out.print("Which animal do you want to weigh? "); int num = Integer.parseInt(kbd.readLine()); System.out.println(zoo.get(num) + " weighs " + ((Animal)(zoo.get(num))).getWeight()); } else if (input == 'q') break; else System.out.println("Invalid option. Try again."); } } }