// Contestant Database - using private data members
//	readData() using a String method .split()

//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_02_03AllClassPrivateSplit {

	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
		ContestantPrv[] contestant = new ContestantPrv[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(ContestantPrv[] 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())
		{
			//create NamePrv, JobInfoPrv, PersonalInfoPrv, and ContestantPrv objects
			NamePrv myName = new NamePrv();
			JobInfoPrv myJob = new JobInfoPrv();
			PersonalInfoPrv myInfo = new PersonalInfoPrv();
			ContestantPrv myContestant = new ContestantPrv();

			//read next line of data 
			line = cFile.nextLine();
			String[] tokens = line.split(" ");	//tokenize a String using method split()
	        
			//extract the data from the line read
			myName.setLast(tokens[0]);							//extract last name
			myName.setFirst(tokens[1]);  						//extract first name
			myInfo.setGender(tokens[2].charAt(0));				//extract gender
			myInfo.setHairColor(tokens[3]);						//extract hair color
			myInfo.setAge(Integer.parseInt(tokens[4]));			//extract age
			myJob.setTitle(tokens[5]);							//extract job title 
			myJob.setSalary(Double.parseDouble(tokens[6]));		//extract salary
			
			//set Contestant object components
			myInfo.setJobInfoPrv(myJob);						//set JobInfo component
			myContestant.setNamePrv(myName);					//set Name component
			myContestant.setPersonalInfoPrv(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, ContestantPrv[] contestant, int num)
	{
		//create local NamePrv, JobInfoPrv, and PersonalInfoPrv objects
		NamePrv myName = new NamePrv();
		JobInfoPrv myJob = new JobInfoPrv();
		PersonalInfoPrv myInfo = new PersonalInfoPrv();
    	
		//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].getNamePrv();
			dbFile.printf("%-10s", myName.getFirst());
			dbFile.printf("%-10s", myName.getLast());
			myInfo = contestant[count].getPersonalInfoPrv();
			dbFile.printf("%-7c", myInfo.getGender());
			dbFile.printf("%-11s", myInfo.getHairColor());
			dbFile.printf("%-4d", myInfo.getAge());
			myJob = myInfo.getJobInfoPrv();
			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, ContestantPrv[] 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].getPersonalInfoPrv().getAge() == agewanted)
			{
				System.out.println(contestant[count].getNamePrv().getFirst() + " "
						+ contestant[count].getNamePrv().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, ContestantPrv[] contestant, int num)
	{
	}

	public static void findHairColor(Scanner keyboard, ContestantPrv[] contestant, int num)
	{
	}

	public static void findTitle(Scanner keyboard, ContestantPrv[] contestant, int num)
	{
	}

	public static void findSalary(Scanner keyboard, ContestantPrv[] contestant, int num)
	{
	}

}
