import java.io.*;

public class ContactUpdater
{
	// Create a single shared BufferedReader for keyboard input
	private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );
		
	public static void main (String [] args) throws IOException
	{
		System.out.print( "Enter the filename: " );   // Prompt the user for a file name
		String fileName = stdin.readLine();           // get a file name from the user
		
		Contact [] oldContacts;		//array that stores Contacts read from the file
		Contact [] newContacts;		//array that stores my new Contacts
		int numContactsInFile = 0;
		int numContactsToAdd = 0;
		
		File file = new File( fileName ); // create a File object
		
		if ( file.exists() )	// check that the file exists
    	{						// before trying to create a
								// BufferedReader

			// Create a BufferedReader from the file
			BufferedReader inFile = new BufferedReader( new FileReader( file ) );
			
			// Compare the results of calling the readLine method to null
			// to determine if you are at the end of the file.
			String line = inFile.readLine();
			while ( line != null )
			{
				line = inFile.readLine();	// read another line from the file
				numContactsInFile++;		// increment my line counter
			}
			
			numContactsInFile = numContactsInFile / 3; // for every three lines of input, there is one contact
			
			// Close the buffered reader input stream attached to the file
			inFile.close();
			
			inFile = new BufferedReader( new FileReader( file ) );	//reopen the file for reading
			oldContacts = new Contact[numContactsInFile];
			
			//read the file and create new Contacts from the file
			for (int i = 0; i < numContactsInFile; i++)
			{
				String firstname = inFile.readLine();
				String lastname = inFile.readLine();
				String phone = inFile.readLine();
				
				oldContacts[i] = new Contact(firstname, lastname, phone);
			}
			
			//prompt the user for the number of contacts they want to add
			System.out.println("How many contacts would you like to add?");
			numContactsToAdd = Integer.parseInt(stdin.readLine());
			
			//initialize the array
			newContacts = new Contact[numContactsToAdd];
			
			//create new Contacts and put them in the array using information
			//from the user
			for (int i = 0; i < numContactsToAdd; i++)
			{
				System.out.print("Please enter a firstname: ");
				String firstname = stdin.readLine();
				System.out.print("Please enter a lastname: ");
				String lastname = stdin.readLine();
				System.out.print("Please enter a phone: ");
				String phone = stdin.readLine();
				
				newContacts[i] = new Contact(firstname, lastname, phone);
			}
			
			//print out the old Contacts to the screen
			for (int i = 0; i < oldContacts.length; i++)
			{
				System.out.println(oldContacts[i]);
			}
			
			//print out the new Contacts to the screen	
			for (int i = 0; i < newContacts.length; i++)
			{
				System.out.println(newContacts[i]);
			}
			

			FileWriter fw = new FileWriter( fileName, false );
		
			// Create a PrintWriter that automatically flushes data
			// to the output file whenever the println method is used.
			PrintWriter pw = new PrintWriter( fw, true );

			//output the array of old Contacts to file
			for (int i = 0; i < oldContacts.length; i++)
			{
				pw.println(oldContacts[i].getFirstname());
				pw.println(oldContacts[i].getLastname());
				pw.println(oldContacts[i].getPhone());
			}
			
			//output the array of new Contacts to file
			for (int i = 0; i < newContacts.length; i++)
			{
				pw.println(newContacts[i].getFirstname());
				pw.println(newContacts[i].getLastname());
				pw.println(newContacts[i].getPhone());
			}
			
			pw.close();
		}
	}
}