// Contestant Database - using private data members // modified readData() using String method .split() // readData uses Parametized Constructors // Illustrate use of ArrayList Class - see QuizShow Class // Extended Example - Expanded menu //needed in order to use the Scanner class import java.util.Scanner; //needed for File I/O import java.io.*; public class pgm07_02_02QuizShowArrayList2 { public static void main(String[] args) throws IOException { //create a QuizShow object QuizShow jeopardy = new QuizShow( "Jeopardy Game Show", //set showTitle new Name("Trebek", "Alex")); //set hostName //variable declarations char choice; //menu selection choice boolean not_done = true; //loop control flag //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 */ readData(jeopardy); //print the database printDatabase(outputFile,jeopardy); /* 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,jeopardy); break; case 'G': case 'g': findGender(keyboard,jeopardy); break; case 'H': case 'h': findHairColor(keyboard,jeopardy); break; case 'T': case 't': findTitle(keyboard,jeopardy); break; case 'S': case 's': findSalary(keyboard,jeopardy); break; case 'N': case 'n': newContestant(keyboard,jeopardy); break; case 'R': case 'r': reviseContestant(keyboard,jeopardy); break; case 'U': case 'u': updateContestant(keyboard,jeopardy); break; case 'E': case 'e': editContestant(keyboard,jeopardy); break; case 'I': case 'i': increaseContestantSalary(keyboard,jeopardy); break; case 'P': case 'p': printDatabase(outputFile,jeopardy); 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 void readData(QuizShow show) throws IOException { //local variable 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 //add the contestant to the database show.addContestant(myContestant); } //close the contestant input file cFile.close(); } /* Method printDatabase() */ public static void printDatabase(PrintWriter dbFile, QuizShow show) { //create local Name, JobInfo, and PersonalInfo objects Name myName = new Name(); JobInfo myJob = new JobInfo(); PersonalInfo myInfo = new PersonalInfo(); Contestant contestant = new Contestant(); //print show information dbFile.println("\t\tShow: " + show.getShowTitle()); dbFile.println("\t\tHost: " + show.getHostName().getFirst() + " " + show.getHostName().getLast()); dbFile.println(); //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 < show.getNumContestants(); count++) { contestant = show.getContestant(count); myName = contestant.getName(); dbFile.printf("%-10s", myName.getFirst()); dbFile.printf("%-10s", myName.getLast()); myInfo = contestant.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("\t N -- add new contestant"); System.out.println("\t R -- revise existing contestant info"); System.out.println("\t U -- update contestant info"); System.out.println("\t E -- edit existing contestant trait"); System.out.println("\t I -- increase existing contestant salary"); System.out.println("\t P -- print contestant database"); System.out.println("\n\n\tEnter your selection: "); } /* Method findAge() */ public static void findAge(Scanner keyboard, QuizShow show) { Contestant contestant; 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 < show.getNumContestants(); count++) { contestant = show.getContestant(count); //get next Contestant if (contestant.getPersonalInfo().getAge() == agewanted) { System.out.println(contestant.getName().getFirst() + " " + contestant.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 newContestant(Scanner keyboard, QuizShow show) { System.out.print("Enter last name: "); String lastName = keyboard.next(); System.out.print("Enter first name: "); String firstName = keyboard.next(); Name myName = new Name( lastName, //set last name component firstName); //set first name component //retrieve index of Contestant of interest int index = show.findContestantByName(myName); if(index != -1) { System.out.println(); System.out.println(firstName + " " + lastName + " already in the database"); } else { System.out.print("Enter gender: "); char gender = keyboard.next().charAt(0); System.out.print("Enter hair color: "); String hairColor = keyboard.next(); System.out.print("Enter age: "); int age = keyboard.nextInt(); System.out.print("Enter job title: "); String jobTitle = keyboard.next(); System.out.print("Enter salary: "); double salary = keyboard.nextDouble(); JobInfo myJob = new JobInfo( jobTitle, //set job title component salary); //set job salary component PersonalInfo myInfo = new PersonalInfo( gender, //set gender component hairColor, //set hair color component age, //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 //add the contestant to the database show.addContestant(myContestant); System.out.println(); System.out.println(firstName + " " + lastName + " added to the database"); } // give user a chance to look at output before printing menu pause(keyboard); } public static void reviseContestant(Scanner keyboard, QuizShow show) { System.out.print("Enter last name: "); String lastName = keyboard.next(); System.out.print("Enter first name: "); String firstName = keyboard.next(); Name myName = new Name( lastName, //set last name component firstName); //set first name component //retrieve index of Contestant of interest int index = show.findContestantByName(myName); if(index == -1) { System.out.println(); System.out.println(firstName + " " + lastName + " does not exist in the database"); } else { System.out.print("Enter gender: "); char gender = keyboard.next().charAt(0); System.out.print("Enter hair color: "); String hairColor = keyboard.next(); System.out.print("Enter age: "); int age = keyboard.nextInt(); System.out.print("Enter job title: "); String jobTitle = keyboard.next(); System.out.print("Enter salary: "); double salary = keyboard.nextDouble(); JobInfo myJob = new JobInfo( jobTitle, //set job title component salary); //set job salary component PersonalInfo myInfo = new PersonalInfo( gender, //set gender component hairColor, //set hair color component age, //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 //revise the contestant info in the database show.reviseContestantInfo(index, myContestant); System.out.println(); System.out.println("Revised info for contestant " + firstName + " " + lastName); } // give user a chance to look at output before printing menu pause(keyboard); } public static void updateContestant(Scanner keyboard, QuizShow show) { System.out.print("Enter last name: "); String lastName = keyboard.next(); System.out.print("Enter first name: "); String firstName = keyboard.next(); System.out.print("Enter gender: "); char gender = keyboard.next().charAt(0); System.out.print("Enter hair color: "); String hairColor = keyboard.next(); System.out.print("Enter age: "); int age = keyboard.nextInt(); System.out.print("Enter job title: "); String jobTitle = keyboard.next(); System.out.print("Enter salary: "); double salary = keyboard.nextDouble(); Name myName = new Name( lastName, //set last name component firstName); //set first name component JobInfo myJob = new JobInfo( jobTitle, //set job title component salary); //set job salary component PersonalInfo myInfo = new PersonalInfo( gender, //set gender component hairColor, //set hair color component age, //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 //update the contestant info in the database boolean success = show.updateContestantInfo(myContestant); System.out.println(); if (success) System.out.println("Updated info for contestant " + firstName + " " + lastName); else System.out.println(firstName + " " + lastName + " does not exist in the database"); // give user a chance to look at output before printing menu pause(keyboard); } public static void editContestant(Scanner keyboard, QuizShow show) { System.out.print("Enter last name: "); String lastName = keyboard.next(); System.out.print("Enter first name: "); String firstName = keyboard.next(); Name myName = new Name( lastName, //set last name component firstName); //set first name component //retrieve index of Contestant of interest int index = show.findContestantByName(myName); if(index == -1) { System.out.println(); System.out.println(firstName + " " + lastName + " does not exist in the database"); } else { //retrieve reference to Contestant of interest //Any changes to myContestant will effect the Contestant in the database Contestant myContestant = show.getContestant(index); //print Contestant Edit Menu printContestantEditMenu(); //read the selection char choice = keyboard.next().charAt(0); //process the selection switch(choice) { case 'A': case 'a': //update Contestant age info System.out.print("Enter age: "); int age = keyboard.nextInt(); myContestant.getPersonalInfo().setAge(age); break; case 'G': case 'g': //update Contestant gender info System.out.print("Enter gender: "); char gender = keyboard.next().charAt(0); myContestant.getPersonalInfo().setGender(gender); break; case 'H': case 'h': //update Contestant hair color info System.out.print("Enter hair color: "); String hairColor = keyboard.next(); myContestant.getPersonalInfo().setHairColor(hairColor); break; case 'T': case 't': //update Contestant job title info System.out.print("Enter job title: "); String jobTitle = keyboard.next(); myContestant.getPersonalInfo().getJobInfo().setTitle(jobTitle); break; case 'S': case 's': //update Contestant job salary info System.out.print("Enter salary: "); double salary = keyboard.nextDouble(); myContestant.getPersonalInfo().getJobInfo().setSalary(salary); break; default: System.out.println("\"" + choice + "\" is an invalid selection - aborting edit operation"); break; } //Note: Contestant info in the database is also updated by reference //Note: This is a Security Hole!!! System.out.println(); System.out.println("Updated info for contestant " + firstName + " " + lastName); } // give user a chance to look at output before printing menu pause(keyboard); } /* Method printMenu() */ public static void printContestantEditMenu() { System.out.println(); System.out.println("Which trait do you want to edit?"); System.out.println("\t****************************"); System.out.println("\t List of Choices "); System.out.println("\t****************************"); 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: "); } public static void increaseContestantSalary(Scanner keyboard, QuizShow show) { System.out.print("Enter last name: "); String lastName = keyboard.next(); System.out.print("Enter first name: "); String firstName = keyboard.next(); Name myName = new Name( lastName, //set last name component firstName); //set first name component //retrieve index of Contestant of interests int index = show.findContestantByName(myName); if(index == -1) { System.out.println(); System.out.println(firstName + " " + lastName + " does not exist in the database"); } else { //retrieve reference to Contestant of interest //Any changes to myContestant will effect the Contestant in the database Contestant myContestant = show.getContestant(index); //update Contestant job salary info System.out.print("Enter amount of raise: "); double raise = keyboard.nextDouble(); boolean success = myContestant.getPersonalInfo().getJobInfo().raiseSalary(raise); System.out.println(); if(success) { System.out.println("Salary increased for contestant " + firstName + " " + lastName); } else { System.out.println("\"" + raise + "\" is an invalid amount - operation aborted"); } } // give user a chance to look at output before printing menu pause(keyboard); } public static void findGender(Scanner keyboard, QuizShow show) { } public static void findHairColor(Scanner keyboard, QuizShow show) { } public static void findTitle(Scanner keyboard, QuizShow show) { } public static void findSalary(Scanner keyboard, QuizShow show) { } }