// Contestant Database - using private data members // modified readData() using String method .split() // readData uses Parametized Constructors //needed in order to use the Scanner class import java.util.Scanner; //needed in order to use the StringTokenizer class import java.util.StringTokenizer; //import java.util.*; //could use the wild card //needed for File I/O import java.io.*; public class pgm06_04_02ContestantDatabase2 { public static void main(String[] args) throws IOException { //constant definitions final int NUMCONS = 50; //maximum number of contestants //variable declarations int num; //number of contestants char choice; //menu selection choice boolean not_done = true; //loop control flag //declare the contestant database Contestant[] contestant = new Contestant[NUMCONS]; //Create a Scanner object for the keyboard Scanner keyboard = new Scanner(System.in); //create an output file object //PrintWriter outputFile = new PrintWriter("C:/BC/CISC3115/pgms/Chapter_06/prj06_02AllClassPrivate/myoutput.txt"); //PrintWriter outputFile = new PrintWriter("myoutput.txt"); PrintWriter outputFile = new PrintWriter(System.out); /* first part */ /* fill the database */ num = readData(contestant); //print the database printDatabase(outputFile,contestant,num); /* second part */ /* call functions to read and process requests */ do { //print the menu printMenu(); //prompt to make a selection System.out.print("enter selection: "); //read the selection choice = keyboard.next().charAt(0); //process the selection switch(choice) { case 'Q': case 'q': not_done = false; break; case 'A': case 'a': findAge(keyboard,contestant,num); break; case 'G': case 'g': findGender(keyboard,contestant,num); break; case 'H': case 'h': findHairColor(keyboard,contestant,num); break; case 'T': case 't': findTitle(keyboard,contestant,num); break; case 'S': case 's': findSalary(keyboard,contestant,num); break; default: System.out.println("Incorrect value; try again"); break; } } while (not_done); //print to console to show program completion System.out.println("The program is terminating"); //close the keyboard keyboard.close(); //close the output file outputFile.close(); } /* Method readData() - using String method .split() */ public static int readData(Contestant[] contestant) throws IOException { //local variable int count = 0; String line; //open the contestant input file //File myFile = new File("C:/BC/CISC3115/pgms/Chapter_06/prj06_02AllClassPrivate/myinput.txt"); File myFile = new File("myinput.txt"); //Create a Scanner object to read the input file Scanner cFile = new Scanner(myFile); //Scanner cFile = new Scanner(System.in); while (cFile.hasNext()) { //read next line of data line = cFile.nextLine(); String[] tokens = line.split(" "); //tokenize a String using method split() //create Name, JobInfo, and PersonalInfo objects // and set their components using constructors Name myName = new Name( tokens[0], //set last name component tokens[1]); //set first name component JobInfo myJob = new JobInfo( tokens[5], //set job title component Double.parseDouble(tokens[6])); //set job salary component PersonalInfo myInfo = new PersonalInfo( tokens[2].charAt(0), //set gender component tokens[3], //set hair color component Integer.parseInt(tokens[4]), //set age component myJob); //set JobInfo component //create the Contestant object and set its components using a constructor Contestant myContestant = new Contestant( myName, //set Name component myInfo); //set PersonalInfo component //set the array element contestant[count] = myContestant; count++; //increment the contestant count } //close the contestant input file cFile.close(); return count; } /* Method printDatabase() */ public static void printDatabase(PrintWriter dbFile, Contestant[] contestant, int num) { //create local Name, JobInfo, and PersonalInfo objects Name myName = new Name(); JobInfo myJob = new JobInfo(); PersonalInfo myInfo = new PersonalInfo(); //print table title dbFile.println("\t\tContestants in the Database"); dbFile.println(); //print table column headings dbFile.printf("%-20s%-7s%-11s%-4s%-10s%-10s", "Name","Gender","Hair Color","Age","Title","Salary"); dbFile.println(); for (int count = 0; count < num; count++) { myName = contestant[count].getName(); dbFile.printf("%-10s", myName.getFirst()); dbFile.printf("%-10s", myName.getLast()); myInfo = contestant[count].getPersonalInfo(); dbFile.printf("%-7c", myInfo.getGender()); dbFile.printf("%-11s", myInfo.getHairColor()); dbFile.printf("%-4d", myInfo.getAge()); myJob = myInfo.getJobInfo(); dbFile.printf("%-10s", myJob.getTitle()); dbFile.printf("$%9.2f", myJob.getSalary()); dbFile.println(); } dbFile.flush(); //flush the output file buffer } /* Method printMenu() */ public static void printMenu() { System.out.println("\n"); System.out.println("To obtain a list of contestants with a given"); System.out.println("trait, select a trait from the list and type in"); System.out.println("the character corresponding to that trait."); System.out.println("To quit, select Q."); System.out.println("\t****************************"); System.out.println("\t List of Choices "); System.out.println("\t****************************"); System.out.println("\t Q -- quit"); System.out.println("\t A -- age"); System.out.println("\t G -- gender"); System.out.println("\t H -- hair color"); System.out.println("\t T -- title"); System.out.println("\t S -- salary"); System.out.println("\n\n\tEnter your selection: "); } /* Method findAge() */ public static void findAge(Scanner keyboard, Contestant[] contestant, int num) { int agewanted,found=0; System.out.println(); System.out.print("Enter the age you want: "); agewanted = keyboard.nextInt(); System.out.println(); System.out.println("Contestants whose age is " + agewanted); for (int count = 0; count < num; count++) if (contestant[count].getPersonalInfo().getAge() == agewanted) { System.out.println(contestant[count].getName().getFirst() + " " + contestant[count].getName().getLast()); found++; } if (found == 0) System.out.println("No contestants of this age"); else System.out.println(found + " contestants found"); // give user a chance to look at output before printing menu pause(keyboard); } /* Method pause() */ public static void pause(Scanner keyboard) { String tempstr; System.out.println(); System.out.print("press ENTER to continue"); tempstr = keyboard.nextLine(); //flush previous ENTER tempstr = keyboard.nextLine(); //wait for ENTER } public static void findGender(Scanner keyboard, Contestant[] contestant, int num) { } public static void findHairColor(Scanner keyboard, Contestant[] contestant, int num) { } public static void findTitle(Scanner keyboard, Contestant[] contestant, int num) { } public static void findSalary(Scanner keyboard, Contestant[] contestant, int num) { } }