/** * This database program stores and makes queries about animal names. * @author */ import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.Arrays; import java.util.ArrayList; import java.awt.event.*; public class DataBaseApp { private TheDataBase db; public Scanner scanner = new Scanner (System.in); /** The ascii file where the names are stored. */ String filename = "animals.txt"; public DataBaseApp() { db = FileIO.readFile(filename); // read the ascii file and load into database //System.out.println("\nThe Database contains:\n" + db); query(); } public String[] choices = { "Print all animals.", "Print all types.", "Add an animal.", "Print the animals of a given type", "Print the type of a given animal", "Print the entire database.", "quit"}; public void query() { int c = 0; while ( c!= 6 ) { // not quitting c = getChoice(); if (c==0) { // print all animals System.out.print("\n"+db.printAnimalNames()); } else if (c==1) { // print all animals System.out.print("\n"+db.printTypeNames()); } else if (c==2) { // add animal addAnimal(); } else if (c==3) { // Print the animals of a given type printAnimalsOfType(); } else if (c==4) { // Print the animals of a given type printTypeOfAnimal(); } else if (c==5) { // Print the entire database System.out.println("\nThe Database contains:\n" + db); } else if (c==6) { // quit System.out.print("\nGood-bye"); } } } /* Print all animals with a given type. */ public void printAnimalsOfType() { System.out.print("\n"+db.printTypeNames()); System.out.println("Enter the index of the animal type you want"); int c = Integer.parseInt( scanner.nextLine() ); Type t = db.getType(c); System.out.println(t.toString()); } /* Print the type for a given animal */ public void printTypeOfAnimal() { System.out.print("\n"+db.printAnimalNames()); System.out.println("Enter the index of the animal you are interested in."); int c = Integer.parseInt( scanner.nextLine() ); Animal a = db.getAnimal(c); System.out.println("\nThe animal " + a.getName() + " is of type " + a.getType().getName()); } /* Get the index of the user's query * @return the index */ public int getChoice() { int c; System.out.print("\nThis is the Animal Database. \nBelow are the possible actions.\n"); for (int i = 0; i < choices.length;i++) { System.out.println(" " + i + ": " + choices[i]); } System.out.print("Please enter your choice: "); c = Integer.parseInt( scanner.nextLine() ); System.out.print("You entered: " + choices[c]); return c; } /** Add a new animal */ public void addAnimal() { System.out.print("\nPlease enter the animal name you wish to add: "); String name = scanner.nextLine().trim().toLowerCase(); System.out.print("\nPlease enter the type of this animal:"); String tname = scanner.nextLine().trim().toLowerCase(); System.out.print("\nAdding " +name); Animal a = db.addAnimal(new Animal(name)); Type t = db.addType(new Type(tname)); a.setType(t); t.addAnimal(a); } // This is where the program starts public static void main(String[] args) { DataBaseApp app = new DataBaseApp(); } }