import java.io.*;
import java.util.*;

class Memo implements Serializable
{
    private int day, month, year, hour, min;
    private String title, text;
    private transient int id; //should not save as may need new
    private static transient int id_index; //should not be saved as reindex

    public Memo()
    {
	Calendar cal = new GregorianCalendar();
	Date currentTime = new Date();
	cal.setTime( currentTime );
	
	day = cal.get(Calendar.DAY_OF_MONTH);
	month = cal.get(Calendar.MONTH);
	year = cal.get(Calendar.YEAR);
	hour = cal.get(Calendar.HOUR);
	min = cal.get(Calendar.MINUTE);
	id = id_index++; //get new id and incriment index
	title = "Untitled";
	text = null;
    }
    public Memo( int D, int Mo, int Y, int H, int Mi, String Ti, String Tx )
    {
	day = D;
	month = Mo;
	year = Y;
	hour = H;
	min = Mi;
	title = Ti;
	text = Tx;
	id = id_index++;
    }

    public Memo( Memo m )
    {
	this( m.day, m.month, m.year, m.hour, m.min, m.title, m.text );
    }


    public void diagnostic ()
    {
	System.out.print( "Start of memo id: " + id + "\n" );
	System.out.print( "created on : " );
	System.out.print( day + "\\" + month + "\\" + year );
	System.out.print( "---" + hour + ":" + min + "\n" );
	System.out.print( "Title: " + title + "\n" );
	System.out.print( "Text: " + text + "\n" );
    }
    void set_title ( String new_title )
    {
	title = new_title;
    }

    String get_title ( )
    {
	return title;
    }

    void set_text(String new_text)
    {
	text = new_text;
    }

    String get_text ( )
    {
	return text;
    }

    void print ()
    {
	System.out.println("Title: " + title );
	System.out.println("Notes: " + text );
    }

}//end of class Memo

class PIM_IO
{
    private ObjectInputStream i_stream;
    private ObjectOutputStream o_stream;
    private boolean reader;
    
    PIM_IO ()
    //default constructor created out of habbit
    {}

    public void openForRead ( String filename )
    //open a file for reading 
    {
	reader = true;
	try {
	    i_stream = new ObjectInputStream ( new FileInputStream( filename) );
	}catch ( IOException e ) {
	    System.out.println( e.getMessage() );
	}
    }

    public void openForRead()
    {
	String filename=null;
	BufferedReader console = new BufferedReader( new InputStreamReader( System.in) );
	try {
		System.out.print( "Enter the name of the file to read: ");
		filename = console.readLine().trim();
		this.openForRead( filename );
	}catch (IOException e ) {
	    System.out.println( e.getMessage() );
	}
    }

    public void openForWrite( String filename )
    {
	reader = false;
	try {
	    o_stream = new ObjectOutputStream ( new FileOutputStream( filename) );
	} catch (IOException e ) {
	    System.out.println( e.getMessage() );
	}
    }
    
    public void openForWrite()
    {
	String filename = null;
	BufferedReader console = new BufferedReader( new InputStreamReader( System.in) );
	try {
	    System.out.print( "Enter the name of the file to write: ");
	    filename = console.readLine().trim();
	    this.openForWrite( filename );
	} catch (IOException e ) {
	    System.out.println( e.getMessage() );
	}
    }

    public Memo get_memo ( )
    {
	try {
	    return (Memo)i_stream.readObject();
	} catch (EOFException e) {
	    return null;
	} catch (IOException e) {
	    System.out.println(e.getMessage() );
	} catch (ClassNotFoundException e ) {
	    System.out.println( e.getMessage() );
	}
	return null;
    }

    public void put_memo( Memo m )
    {
	try {
	    o_stream.writeObject( m );
	} catch (IOException e) {
	    System.out.println(e.getMessage() );
	}
    }

    public void close()
    {
	try {
	    if ( reader )
		i_stream.close();
	    else {
		o_stream.flush();
		o_stream.close();
	    }
	}catch ( IOException e ) {
	    System.out.println( e.getMessage() );
	    e.printStackTrace();
	}
    }
} //end of PIM_IO


public class Object_IO
{
    public static void main( String []args )
    {
	int size, count;
	size=5;
	count =0;
	Memo temp;
	Memo[] memoList = new Memo[size];
	Memo[] memoList2 = new Memo[size*2];

	for ( int i=0; i<size; i++ ) {
	    memoList[i] = new Memo();
	    memoList2[i] = new Memo();
	    memoList[i].set_text("list1, text for memo: " + i );
	    memoList[i].set_title("list1, title for memo: " + i );
	    memoList2[i].set_text("list2, text for memo: " +i );
	    memoList2[i].set_title("list2, title for memo: " +i );
	    memoList[i].diagnostic();
	}

	PIM_IO outfile = new PIM_IO ();
	outfile.openForWrite(); //open for witting

	for ( int i=0; i< size; i++ )
	    outfile.put_memo( memoList[i] ); //write the objects 1 at a time

	outfile.close(); //done writting file so close
	
	PIM_IO infile = new PIM_IO ();
	infile.openForRead();//open to read
	
	count = size;
	temp = infile.get_memo();
	while( temp!= null ) {
	    //read from file memo list 1 and put in list 2
	    memoList2[count] = new Memo ( temp );
	    temp = infile.get_memo();
	    count++;
	}
	
	System.out.println("List 2 printout (2nd half is list one from file)" );
	for ( int i=0; i < count; i++ ) {
	    //print out list 2;
	    memoList2[i].diagnostic();
	}
	infile.close();
	
    } 
}











