/********************************************************************************************
*	Eddie Corrales		                   5/10/07	                                        *
*	Java CM16		                   Final Project                                        *
*	Prof. Bam		                   ECFinalpayroll                                       *
*				FINAL	                                                                    *
*		                                                                                    *
*********************************************************************************************/

/*******************Import Java Components**********************/


import java.io.*;import java.util.Scanner;import java.lang.*;import java.text.DecimalFormat;

public class ECFinalpayroll
{
    public static void main(String args[] ) throws IOException
    {

    /***** Declare variables ******/

    /* Strings */
    String HoursWorked = "temp";
    String testTempID = "temp";

    /* Integers */
    int tempID = -1;
	int correct = 6;
	int m = 0;

	/* Final Ints */
	final int length = 5; //Length of employee array

    ////****** Java Variables ******////
    DecimalFormat formatter = new DecimalFormat("#0.00");
    Scanner entry = new Scanner(System.in);

    /////////////////////**INTRO**////////////////////////////////////////

 	/*Create the array of employees */
    ECEmployeeClassFinal Emp[]=  new ECEmployeeClassFinal[ 500 ];

   //****Inputs for the data for analyzation****//
    Emp[0] = new ECEmployeeClassFinal("12345,3,3,Smith,John,true,9.00,true");
    Emp[1] = new ECEmployeeClassFinal("23456,2,2,Jackson,Micheal,true,10.00,true");
    Emp[2] = new ECEmployeeClassFinal("34567,1,1,Presley,Megan,false,11.00,false");
    Emp[3] = new ECEmployeeClassFinal("45678,2,0,Corrales,Eddie,false,12.00,true");
    Emp[4] = new ECEmployeeClassFinal("56789,3,4,Becker,Daniel,true,15.00,false");

	////****** SORT ******////
     for (int k=0;k < 5;k++)
     {
       for (int j=0;j < 4;j++)
        {
          if (Emp[j].getID() > Emp[j+1].getID())
          {
           swap(Emp,j,j+1);
          }
        }
     }

    ////****** INPUT******////

 int position = -1;
 for(int u = 0; u < length +1;u++)
 {
    do
    {
       do
       {
       	testTempID = "temp"; 				 	 			//////Reset ID string to prevent loop trap
        say("Enter a Employee's number or else type 'print' to display Results.");
        testTempID = entry.nextLine(); 		 				//////Take in ID as string
    	if(testTempID.equalsIgnoreCase("print"))			//////Test stringID to end loop
        reports(length, Emp);
        if (testID(testTempID)== false)		 				//////Checks for valid ID by function TesttempID
             say("No such employee");
       }while(testID(testTempID) == false);					//////Loops end when a valid number entered
     tempID = Integer.parseInt(testTempID);					//////Parse string ID to integer

   ////**** Employee Search ****////

    int first = 0;
    int last=length-1;
    int middle;
    boolean found = false;
    position = -1; 											//////Reset Positions
     while(!found && first <= last)
       {
        middle = (first + last) / 2;
            if (Emp[middle].getID() == tempID)
            {
              found = true;
              position = middle;
            }
            else if (Emp[middle].getID() > tempID)
             last = middle -1;
            else
             first = middle +1;
        }
     if(position == -1)
       say("No such employee exists."); 	   //No employee found
     else
      correct = position; 				   //Repeat till Correct until employee found
    }while (position == -1);


 	////******INPUT OF HOURS******////
    do
    {
     say("Please enter the amount of hours " + Emp[correct].getLname() + " worked.");
     HoursWorked = entry.nextLine();
    }while(checkHours(HoursWorked) == false); 	//Repeat till Correct of valid hours

	/* Set hours */
    Emp[correct].setHours(Double.parseDouble(HoursWorked));

 }
    printPay(Emp, length);

 }
public static boolean testForEnd(String z)
{
   boolean end = false;
    String id = z;
    if (id.equalsIgnoreCase("no"));
         end = true;
    return end;
}
public static void printPay(ECEmployeeClassFinal Emp[], int length)
{
    System.out.println("***** Paychecks Prints *****");
    for(int j=0; j < length; j++)
    Emp[j].printPaychecks();

}
        ////****** REPORTS ******////

public static void reports(int length, ECEmployeeClassFinal Emp[])
{
    String answer;
    double totalRate=0;
    double totalpay = 0;
    double hoursInAll = 0;
    double marriedfinalpay = 0;
    double totaltax = 0;
    int marriedemps = 0;
    int ficapeople = 0;
    int counterEmpWork = 0;
    DecimalFormat formatter = new DecimalFormat("#0.00");
    DecimalFormat formatter2 = new DecimalFormat("#0.0");
    Scanner entry = new Scanner(System.in);
   printPay(Emp, length);
   for(int j= 0; j < length; j++)
    {
        hoursInAll = hoursInAll + Emp[j].getHours();
        totalpay = totalpay + Emp[j].getGrosspay();
        totalRate = totalRate + Emp[j].getTotalRate();
        if (Emp[j].getMarried() == true && Emp[j].getHours() > 0)
        {
        marriedfinalpay = marriedfinalpay + Emp[j].getGrosspay();
        marriedemps++;
        ficapeople++;
        }
        totaltax = totaltax + Emp[j].getTax();
        if (Emp[j].getHours() > 0)
        	counterEmpWork++;
     }
    say("\n***** Payroll Report *****");
    say("The total payroll comes to $" + formatter.format(totalpay));
    say("The number of employees are " + length + ".");
    say("The number of employees whom worked this week is " + counterEmpWork + ".");
    say("The total number of hours worked is " + hoursInAll + ".");
    say("The average amount of hours worked is " + formatter2.format((hoursInAll / counterEmpWork)) + " per employee.");
    say("The average pay is $" + formatter.format((totalRate / length)) + " per hour.");
    say("The amount of people in Fica is" + ficapeople + "." );
    say("The number of married employees is " + marriedemps + ".");
    say("The average gross pay for married employees is $" + formatter.format((marriedfinalpay / marriedemps)) + " that worked.\n");
    say("Would you like to print an employee report? (y/n)");
     answer = entry.nextLine();
    if(answer.equalsIgnoreCase("yes"))
        displayEmp(Emp, length);
     else if(answer.equalsIgnoreCase("y"))
         displayEmp(Emp, length);
     else
     {
      say("Are you sure you want to exit before displaying the reports? (y/n)");
         answer = entry.nextLine();
     }
     if(answer.equalsIgnoreCase("no"))
        displayEmp(Emp, length);
     else if(answer.equalsIgnoreCase("n"))
         displayEmp(Emp, length);
     else
     {
      say("Goodbye!");
         System.exit(0);
     }
}

public static boolean checkHours(String y)
{
    String hours = y;
    boolean good = true;
    int index = 0;
    if(hours.length() >= 5)
        good = false;
    return good;
}
public static boolean testID(String x)
{
    String id = x;
    boolean good = true;
    int index = 0;
    if(id.length() != 5)
        good = false;
    while(good && index == 4)
    {
        if(!Character.isDigit(id.charAt(index)));
        good = false;
        index++;
    }
    return good;
}

public static void swap(ECEmployeeClassFinal Emp[], int j1, int j2)
{
	ECEmployeeClassFinal temp = new ECEmployeeClassFinal("00000,0,0,TEMP,EMP,false,00.01");
	temp = Emp[j1];
	Emp[j1] = Emp[j2];
	Emp[j2] = temp;

}
public static void say(String x)
{System.out.println(x);}

public static void displayEmp(ECEmployeeClassFinal Emp[], int length)
{
  String answer;
  Scanner entry = new Scanner(System.in);
 boolean loop = false;
 DecimalFormat formatter = new DecimalFormat("#0.00");
  do
  {
  say("What kind of employee report would you like?");
   say("Your options are:");
   say("1. Report by ID's descending");
   say("2. Report by ID's asscending");
   say("3. Report by pay rate descending");
   say("4. Report by pay rate assecnding");
   say("5. Report by hours descending");
   say("6. Report by hours assecnding");
   say("7. Special report, workers over 45 hours whom are married.");
   say("8. Everyone Gets a 10 percent raise");
   say("9. Exit");
   answer = entry.nextLine();

  ///////////////**************************** Case Reports ****************************////////////////

  if(answer.equalsIgnoreCase("1"))
   {
               //******by ID*******//
     for (int k=0;k < 5;k++)
     {
		 for (int j=0;j < 4;j++)
        {
			if (Emp[j].getID() > Emp[j+1].getID())
          {
			  swap(Emp,j,j+1);
          }
        }
     }
     for(int j=0; j < length; j++)
         Emp[j].printEmp();
     say("That is all. Thank you and Goodbye.");
System.exit(0);
   }
   else if(answer.equalsIgnoreCase("2"))
   {
                //******by ID*******//
     for (int k=0;k < 5;k++)
     {
       for (int j=0;j < 4;j++)
        {
          if (Emp[j].getID() < Emp[j+1].getID())
          {
           swap(Emp,j,j+1);
          }
        }
     }
     for(int j=0; j < length; j++)
         Emp[j].printEmp();
     say("That is all. Thank you and Goodbye.");
System.exit(0);
   }

   else if(answer.equalsIgnoreCase("3"))
{
             //******by pay*******//
     for (int k=0;k < 5;k++)
     {
       for (int j=0;j < 4;j++)
        {
          if (Emp[j].getTotalRate() < Emp[j+1].getTotalRate())
          {
           swap(Emp,j,j+1);
          }
        }
     }
     for(int j=0; j < length; j++)
         Emp[j].printEmp();
     say("That is all. Thank you and Goodbye.");
System.exit(0);
}

   else if(answer.equalsIgnoreCase("4"))

       {
              //******by pay*******//
     for (int k=0;k < 5;k++)
     {
       for (int j=0;j < 4;j++)
        {
          if (Emp[j].getTotalRate() > Emp[j+1].getTotalRate())
          {
           swap(Emp,j,j+1);
          }
        }
     }
     for(int j=0; j < length; j++)
         Emp[j].printEmp();
     say("That is all. Thank you and Goodbye.");
System.exit(0);
}

 else if(answer.equalsIgnoreCase("5"))
 {
              //******byHours*******//
     for (int k=0;k < 5;k++)
     {
       for (int j=0;j < 4;j++)
        {
          if (Emp[j].getHours() < Emp[j+1].getHours())
          {
           swap(Emp,j,j+1);
          }
        }
     }
     for(int j=0; j < length; j++)
         Emp[j].printEmp();
     say("That is all. Thank you and Goodbye.");
System.exit(0);
 }
   else if(answer.equalsIgnoreCase("6"))
 {
              //******byHours*******//
     for (int k=0;k < 5;k++)
     {
       for (int j=0;j < 4;j++)
        {
          if (Emp[j].getHours() > Emp[j+1].getHours())
          {
           swap(Emp,j,j+1);
          }
        }
     }
     for(int j=0; j < length; j++)
         Emp[j].printEmp();
     say("That is all. Thank you and Goodbye.");
	System.exit(0);
 }

   else if(answer.equalsIgnoreCase("7"))
   {
     //*************by pay for married over 45 hours*************//
   for(int j=0; j < length; j++)
   {
       if (Emp[j].getMarried() == true && Emp[j].getHours() > 45);
       Emp[j].printEmp();

   }
     say("That is all. Thank you and Goodbye.");
	System.exit(0);
   }
   else if(answer.equalsIgnoreCase("8"))
   {
	   String password;
	   say("Please enter the password: ");
	   password = entry.nextLine();
	   if (password.equalsIgnoreCase("password"));
	   {
	   	//************* Raise function *************//
	  	double pay =0;
	  	for(int j=0; j < length; j++)
	   		{
			say("Employee " + Emp[j].getID() + " gets $" + Emp[j].getTotalRate() + " now with a raise he recieves $" + formatter.format(Emp[j].raise()) + ".") ;
			}
	   loop = true;
		}
		if (!password.equalsIgnoreCase("password"));
		{
		say("Password is incorrect. Try again and enter a valid Password.");
		loop = true;
		}
	}
   else if(answer.equalsIgnoreCase("9"))
   System.exit(0);

   else
     {
         say("Invalid entry, Please choose a number 1 to 7.");
         loop = true;
     }
   }while(loop == true);
}

/////****** END *******//////


}


