// 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_04QuizShow {

	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;
				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("\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 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)
	{
	}

}
