// joi/3/textfiles/TextFile.java
//
//
// Copyright 2003 Bill Campbell and Ethan Bolker

import java.util.Date;

/**
 * A TextFile mimics the sort of text file that one finds
 * on a computer's file system.  It has an owner,
 * a create date (when the file was created),
 * a modification date (when the file was last modified),
 * and String contents.
 *
 * @version 3
 */

public class TextFile 
{
    // Private Implementation

    private String owner;       // Who owns the file.
    private Date   createDate;  // When the file was created.
    private Date   modDate;     // When the file was last modified.
    private String contents;    // The text stored in the file.

    // Public Interface

    /**
     * Construct a new TextFile with given owner and
     * contents; set the creation and modification dates.
     *
     * @param owner the user who owns the file.
     * @param contents the file's initial contents.
     */

    public TextFile( String owner, String contents ) 
    {
        this.owner    = owner;
        this.contents = contents;
        createDate    = new Date(); // date and time now
        modDate       = createDate;     
    }

    /**
     * Replace the contents of the file.
     *
     * @param contents the new contents.
     */

    public void setContents( String contents ) 
    {
        this.contents = contents;
        modDate = new Date();
    }

    /**
     * The contents of a file.
     *
     * @return String contents of the file.
     */

    public String getContents() 
    {
        return contents;
    }

    /**
     * Append text to the end of the file.
     *
     * @param  text the text to be appended.
     */

    public void append( String text ) 
    {
        this.setContents( contents + text );
    }

    /**
     * Append a new line of text to the end of the file.
     *
     * @param  text the text to be appended.
     */

    public void appendLine( String text ) 
    {
        this.setContents(contents + '\n' + text);
    }

    /**
     * The size of a file.
     *
     * @return the integer size of the file
     *  (the number of characters in its String contents)
     */

    public int getSize() 
    {
        int charCount;
        charCount = contents.length();
        return charCount;
    }

    /**
     * The data and time of the file's creation.
     *
     * @return the file's creation date and time.
     */

    public String getCreateDate() 
    {
        return createDate.toString();
    }

    /**
     * The date and time of the file's last modification.
     *
     * @return the date and time of the file's last modification.
     */

    public String getModDate() 
    {
        return modDate.toString();
    }

    /**
     * The file's owner.
     *
     * @return the owner of the file.
     */

    public String getOwner() 
    {
        return owner;
    }

    /**
     * A definition of main(), used only for testing this class.
     *
     * Executing
     * <pre>
     *     %> java TextFile
     * </pre>
     * produces the output:
     * <pre>
     * TextFile myTextFile contains 13 characters.
     * Created by bill, Sat Dec 29 14:02:37 EST 2001
     * Hello, world.
     *
     * append new line "How are you today?"
     * Hello, world.
     * How are you today?
     * TextFile myTextFile contains 32 characters.
     * Modified Sat Dec 29 14:02:38 EST 2001
     * </pre>
     */

    public static void main( String[] args )
    {
        Terminal terminal = new Terminal();
        TextFile myTextFile 
            = new TextFile( "bill", "Hello, world." );
        terminal.println( "TextFile myTextFile contains " + 
                          myTextFile.getSize() + 
                          " characters.");
        terminal.println( "Created by " +
                          myTextFile.getOwner() + ", " +
                          myTextFile.getCreateDate() );
        terminal.println( myTextFile.getContents() );
        terminal.println();
        terminal.println( 
                 "append new line \"How are you today?\"" );
        myTextFile.appendLine( "How are you today?" );  
        terminal.println( myTextFile.getContents() );
        terminal.println( "TextFile myTextFile contains " + 
                          myTextFile.getSize() + 
                          " characters.");
        terminal.println( "Modified "  +
                          myTextFile.getModDate() );
    }
}
