/* 
John Oxford & Ryan Thompson & Joseph Lovins 
CS1301-A Programming Principles I
Dr. Youming Li (instructor)
Due 4/27/2006 (April 27, 2006)
Program created 4/24/2006 (April 24, 2006)
Filename is Tests.java

	Compiling and running the program:

-First, extract the program (Tests.java & Student.java) to the C:\ directory
-Now to run the program you must enter the command prompt
-If your directory is not pointed to C:\, then change to it by typing "cd\"
-To compile the program you must input "javac Tests.java" & "javac Student.java"
-After the compilation is completed, run the program by typing "java Tests"

	Purpose and uses of the program:

This program lets the user enact sever options on a set of randomly assigned test
scores for 10 students. The student names are provided by the user via the
JOptionPane method when the program is first started. The user has four options that 
he or she can input into a dialog box. The first option is the ability to print out
the entire list of scores for each test of each student. The second option is the 
ability to sort the grades for a specific test in ascending order with reference
to which student scored what. The third option is the ability to sort the student
names in alphabetical order. The fourth allows the user to search a particular quiz for
a particular score that he or she both decides. Finally, the user can only correctly exit
the program by inputting the exit command inside the dialog box.
*/
import javax.swing.*;

public class Tests
{

  public static void main (String[] args)
  {
    Student[] srecords = new Student[10];//create an array of objects (Student)
     for(int i=0; i<10; i++)
     srecords[i] = new Student();
     
    Student[] holder = new Student[10];//create a copy of srecords
     for(int i=0; i<srecords.length; i++)
     holder[i] = srecords[i];
      

	for(int i=0; i<srecords.length; i++)//print out the scores via instancing
  	srecords[i].printout();
  	String choice="";//stores the user's input
  	boolean input = false;//this will allow for the loop to catch exceptions
  	 	
	do 
	{
		
	input = false;//assigns a false value every iteration		
	
	choice = JOptionPane.showInputDialog("Enter 'P' to printout the student scores"+
	"\nEnter 'A' to sort a test"+
	"\nEnter 'B' to sort the student names"+
	"\nEnter 'S' to search a quiz for a certain score"+
	"\nEnter 'E' to exit the program");
	
	if (choice.equals("P") | (choice.equals("p")))
	{
	for(int i=0; i<srecords.length; i++)//call the printout function
	  srecords[i].printout();
	input = true;//assign input as true
	}
	
	else if (choice.equals("A") | (choice.equals("a")))
	{
	  sorting(srecords, holder);//sort a test
	  input = true;
	}
	
	else if (choice.equals("B") | (choice.equals("b")))
	{
	  sort_names(srecords, holder);//sort the names
	  input = true;
	}  	
	else if (choice.equals("S") | (choice.equals("s")))
	{
	  search_key(srecords, holder);//search a test for a certain score
	  input = true;
	}
	
	else if (choice.equals("E") | (choice.equals("e")))
	 {
	  for(int i=0; i<srecords.length; i++)//reprint the scores
	   	srecords[i].printout();
	   	
	  System.exit(0);//exit the loop (program)
	  input = true;
	 }
	 
	  else if (input != true)//this is where the exceptions are catched
	  {
	    JOptionPane.showMessageDialog(null, "Your input isn't legal, please try again.");
	    input = true;//then the boolean is assigned true to allow the loop to continue
	  }
	  
	} while(input = true);//loop continues while input is equal to true
	
  } 
	 
  public static void sorting(Student[] x, Student[] y)//sorting method
  {
  	String temp="";
  	int choice;
  	
  	temp = JOptionPane.showInputDialog("Which quiz would you like sorted?");
  	choice = Integer.parseInt(temp);
  	for(int j=x.length; j>=2; j--)
		for(int k=0; k<j-1; k++)
			if(x[k].quiz[choice-1] > x[k+1].quiz[choice-1])
				swap(x, k, k+1);//swap the position of the scores
				
	System.out.println("------------------------------------------------");	
	//reprint the newly sorted scores		
	for(int b=0; b<x.length; b++)
		System.out.println(x[b].name +": "+ x[b].quiz[choice-1]+"\n");
		
	System.out.println("------------------------------------------------");	
		
	recycle(x, y);//recycles the arrays
  }
  public static void swap(Student[] name, int pos1, int pos2)//swap method
  {
	Student temp;

    temp = name[pos1];
    name[pos1] = name[pos2];
    name[pos2] = temp;
  }
  
  public static void sort_names(Student[] a, Student[] b)//name sorting method
  { 
    //names are printed before they are sorted
  	System.out.println("------------------------------------------------"+
	"\nBefore sorting,");
	for(int i=0; i<a.length; i++)
	System.out.println("\n"+a[i].name);

     for(int j=a.length; j>=2; j--)
      for(int k=0; k<j-1; k++)
	   if(a[k].name.compareTo(a[k+1].name) > 0)
			swap(a, k, k+1);//names are swaped

    System.out.println("--------------------"+
	"\nAfter sorting,");
	for(int i=0; i<a.length; i++)
	System.out.println("\n"+a[i].name);
	System.out.println("------------------------------------------------");
	//the sorted names are printed	
	recycle(a, b);//recycle the arrays
  }
  
  public static void search_key(Student[] b, Student[] c)//specific quiz score search method
  {
  	String temp = JOptionPane.showInputDialog("Which quiz would you like to search?");
  	String score = JOptionPane.showInputDialog("What is the score you would like to find?");
  	
  	int quiz = Integer.parseInt(temp);
  	int key = Integer.parseInt(score);
  	
  	linear_search(b, quiz, key);//calls the linear searching method
  	recycle(b, c);//recycle the arrays
  	
  }
  
  public static void linear_search(Student[] x, int y, int key)//score search method
  {
	for(int j=0; j<x.length; j++) 
	{
		if(key==x[j].quiz[y-1])
			System.out.println("Student "+x[j].name+" has the key at quiz "+(y));
	}//the student's name and quiz are printed if they have the key provided
  }

  
  public static void recycle(Student[] original, Student[] junk)//recycle method
  {
     for(int i=0; i<junk.length; i++)
       original[i] = junk[i];
  }
  // The recycle method allows the array to not keep permanate changes, so it looks
  // the same as it did to begin with. 
  
 }