import java.io.*;
import java.util.*;

/************************************************
 * Lab 4 - Remember gets the date, finds a corresponding
 *    date on the calendar and prints a name
 * 
 * @author - Joshua M. Bryant
 * @version - V1 - Jan 22, 2007
 ************************************************/
 // Lab Time : Wednesday, 1:25 - Section 1
 // Lab 4 : Experimenting With File I/O
public class IO_bryan5jm_Remember
{
   private static Scanner        dateScanner;
   private static PrintWriter    screen;
   
   /**********************************************
    * main method that creates a calendar and prints
    * the calendar
    *
    * @param args The names of files
    **********************************************/
   public static void main(String[] args)
   {
      GregorianCalendar        today;
      String                   name;
		File                     dates[];       

      // instantiates today and screen
      today    = new GregorianCalendar();
      screen   = new PrintWriter(System.out);
      
		// instantiates dates
      dates = new File[args.length];
		
		for (int index = 0; index < args.length; index++)
		{
			   dates[index] = new File(args[index]);
		}
		
      // prints today's calendar date
      screen.printf(Locale.US, "\nToday's Date: %1$te %1$tB %1$tY\n", today);
      screen.flush();       
       
      // attempts to instantiate dateScanner, but catches exception
		//    if file not found
      try
		{
		   for (int index = 0; index < args.length; index++)
			{
		      dateScanner = new Scanner(dates[index]);
				   
				// calls processDates with today and dates[] as a parameter
            processDates(today, dates[index]);
		   }
		}
		catch (FileNotFoundException fnfe)
		{
		   screen.println("Exception: The file " + dates + " was not found");
		}
   }
    
   /**********************************************
	 * method that receives GregorianCalendar today,
    * 
    * @param today GregorianCalendar
	 * @param dates File with corresponding events
    **********************************************/
   private static void processDates(GregorianCalendar today, File dates)
   {
      int           day, month;              
      String        name;
       
      // Use the '\t' (tab) and '/' characters as delimiters
      dateScanner.useDelimiter("[\t/]");
      
		// extracts the date from the calendar
      while (dateScanner.hasNext())
      {
         month = dateScanner.nextInt();
         day   = dateScanner.nextInt();
         name  = dateScanner.nextLine();
         
			// prints items extracted from files
         if ((month == (today.get(Calendar.MONTH)+1)) && 
            (day   == today.get(Calendar.DAY_OF_MONTH)))
         {
            screen.printf("%s\n", name);
            screen.flush();
         }
      }
   }
}
