/*
 * Brian Billow
 * 10/23/03
 * Period 7
 * Purpose: create a guestbook for my AP Comp Sci website using java, Part B
 */

public class GuestbookPB
{
	public static void main(String[] args)
	{
				
		String lName = "", fName, Email, fileName = "Guestbook.txt", fileName2 = "Password.txt";
	    String Line = "", Reinput, Repeat, PW, RO = "", PWC = "", LineR;
	   	int TP = 0, C = 0, EN = 0, GL, RD = 0, i, RT, RF, PN = 1;
	   			
		System.out.println("Hello, and welcome to the guestbook of Brian Billow");
		EasyReader console = new EasyReader();     // Calls the EasyReader class
		EasyWriter outFile = new EasyWriter("Guestbook.txt", "app");    // Calls the EasyWriter class
		
		System.out.println("Please Enter your Last Name:");
		lName = console.readLine();                // Prompts user to input their last name
		
		if ( lName.equalsIgnoreCase( "admin" ) || lName.equalsIgnoreCase( "Lonewolf" ))    // Checks to see if Variable lName is equal to Admin or admin or ADMIN 
		{
			System.out.println("Please enter the password");
			PW = console.readLine();
			
			EasyReader inFile3 = new EasyReader(fileName2);    // tells the program to open the file "Password"
			
			PWC = inFile3.readLine();          // reads line 1 from that file	
			
			if (PW.equals(PWC))
			{
				do					
				{
					System.out.println("");
					System.out.println("Welcome Admin!");
					System.out.println("");
					System.out.println("Options:");
					System.out.println("1: Display Total Number of Posts");
					System.out.println("2: Display a range of entries");
					System.out.println("3: Clear all guestbook entries");
					System.out.println("4: View all entries");
					System.out.println("5. Change Password");
					System.out.println("6: Exit");
					System.out.println("");
					System.out.println("Please enter number of option you wish to use:");
					
					C = console.readInt();
					EasyReader console4 = new EasyReader();     // Calls the EasyReader class
					
					switch (C)
					{
						case (1):
						TP = 0;
						System.out.println("");
						
						EasyReader inFile = new EasyReader(fileName);    // tells the program to open the file "guestbook"
						
						while (!inFile.eof())                  // Checks to see if it is at the end of the file
						{
							Line = inFile.readLine();          // reads line 1 from that file	
							
							if (Line != null)                  // Checks to see if Line is null
							{
								TP = TP + 1;
	  						}
	  						else
	  						{
  								if (TP == 1)
  								{
  									System.out.println("There has been " + TP + " entry so far");
  									System.out.println("");
  								}
  								else
  								{
  									System.out.println("There have been " + TP + " entries so far");
  									System.out.println("");
  								}
  							}
  						}	
						System.out.println("Would you like to return to the options?");
						System.out.println("Y/N?");
						RO = console4.readLine();
						break;
					
						case (2):
						EasyReader console6 = new EasyReader();
									
						System.out.println("What do you want the range to be?");
						System.out.println("Start from what guestbook entry?");
						RF = console6.readInt();
						System.out.println("End with what guestbook entry?");
						RT = console6.readInt(); 
						
						EasyReader inFile4 = new EasyReader(fileName);
						
						while(PN < RF)
						{
							LineR = inFile4.readLine();
							PN = PN + 1;
						}
						for (i = RF; i >= RF && i <=RT; i++)
						{
							LineR = inFile4.readLine();
							System.out.println(LineR);
						}
						System.out.println("Would you like to return to the options?");
						System.out.println("Y/N?");
						RO = console4.readLine();
						break;
						
						case (3):
						EasyWriter outFile2 = new EasyWriter("Guestbook.txt", "write");    // Calls the EasyWriter class
						System.out.println("All of your guestbook entries have been cleared");
						System.out.println("");
						System.out.println("Would you like to return to the options?");
						System.out.println("Y/N?");
						RO = console4.readLine();
						break;
						
						case (4):
						EN = 0;
						System.out.println("");
										
						EasyReader inFile2 = new EasyReader(fileName);    // tells the program to open the file "guestbook"
		  	  		
						while (!inFile2.eof())                  // Checks to see if it is at the end of the file
						{
		  					EN = EN + 1;                       // adds 1 to Entry Number
		    				Line = inFile2.readLine();          // reads line 1 from that file
							
							if (Line != null)                  // Checks to see if Line is null
							{
								System.out.println("Entry " + EN);  // displays entry number
								System.out.println( Line );         // displays all info saved in the file
	  							System.out.println("");
	  						}
	  						else
	  						{
  								System.out.println("this is all of the info that has been saved so far");
  								System.out.println("");
  							}
  						}
						System.out.println("Would you like to return to the options?");
						System.out.println("Y/N?");
						RO = console4.readLine();
						break;
					
						case (5):
						
						do
						{
							System.out.println("");
							System.out.println("What would you like to change your password to?");
							PW = console4.readLine();
							System.out.println("New password: " + PW );
							System.out.println("Is this correct?");
							System.out.println("Y/N?");
							PWC = console4.readLine();
							if (PWC.equalsIgnoreCase("yes") || PWC.equalsIgnoreCase("y"))
							{
								EasyWriter outFile3 = new EasyWriter("Password.txt", "write");
								outFile3.println(PW);           // Stores new password into the password file
								outFile3.close();               // Closes the password file 
							}
							else
							{
							}
						}while ( !((PWC.equalsIgnoreCase("yes") || PWC.equalsIgnoreCase("y"))));
						System.out.println("Would you like to return to the options?");
						System.out.println("Y/N?");
						RO = console4.readLine();
						break;
											
						case (6):
						System.out.println("");
						System.out.println("Bye Bye");
						RO = "no";
						break;
						
						default:
						System.out.println("Your choice was invalid");
						System.out.println("");
						RO = "yes";
						break;
					}	
				}while (RO.equalsIgnoreCase("yes") || RO.equalsIgnoreCase("y"));
			}
			else
  			{
  				System.out.println("Your password was invalid");
  			}
  		}	
		else
		{
			do
			{
				EasyReader console2 = new EasyReader();     // Calls the EasyReader class
				
				if (RD == 1)
				{
					System.out.println("");
					System.out.println("Please enter your Last Name:");			
					lName = console2.readLine();
				}
				else
				{
					RD = 0;
				}
					
				System.out.println("Please Enter your First Name:");
				fName = console2.readLine();    // Prompts user to input their first name
				System.out.println("Please Enter your Email Address:");
				Email = console2.readLine();    // Prompts user to input their email address
				System.out.println("Please Enter your Grade Level:");
				GL = console2.readInt();        // Prompts user to input their grade level
				System.out.println("");
				
				System.out.println("Here is the information you have Entered:");       // re-displays all of the person info   
				System.out.println("");
				System.out.println("Last Name: " + lName );
				System.out.println("First Name: " + fName );
				System.out.println("Email Address: " + Email );
				System.out.println("Grade Level: " + GL );
				System.out.println("is all of this info correct?");
				System.out.println("Y/N?");
		
				EasyReader console3 = new EasyReader();     // Calls the EasyReader class
				
				Reinput = console3.readLine();              // Prompts user for either yes or no
					
				if (Reinput.equalsIgnoreCase("yes") || Reinput.equalsIgnoreCase("y"))
				{
					RD = 0;
					outFile.println("Last Name: " + lName + ", First Name: " + fName + ", Email Address: " + Email + ", Grade Level: " + GL);   // Stores the variable into Guestbook.txt
					outFile.close();               // Closes the guestbook file   
					System.out.println("");
					System.out.println("Thanks you for your time");
				}
				else
				{
					RD = 1;
				}			 	
			}while (RD == 1);
		}
	}
}
