/*********************************************************************
 * Author: B. Alex Bridges                                           *
 * Login ID: brid0129                                                *
 * Class: CPSC-102, Summer 1998                                      *
 * Project: Programming Assignment 1                                 *
 * Description: This program is a simple database query system for a *
 *              video store.                                         *
 * Contents: Methods for program actions--search and list.           *
 *********************************************************************/

/* IMPORTS */
import java.io.*;


class Database
{
	/* CONSTANTS */
	final static boolean b_debug = true;											// CONTROLS EXTRA DEBUG OUTPUT

	/*************************************************************************
	 * Method: search                                                        *
	 * Purpose: Searches video database for matching title.                  *
	 * Input: --PARAMATERS--                                                 *
	 *        => 'str_database' = The filename of the video database.        *
 	 *        => 'str_search'   = The title for which to search.             *
	 * Output: --EXIT VALUE--                                                *
	 *         => FATAL EXCEPTION = -1                                       *
	 *************************************************************************/
	public static void search(String str_database, String str_search)
	{
		/* LOCAL VARIABLES */
		Videos Videos_movie = new Videos();	// VIDEO OBJECT FOR MOVIE
		boolean b_found_movie = false;		// BOOLEAN FOR MOVIE CARRIED
		BufferedReader br_file_in;			// BUFFERED READER FOR INPUT FILE
		String str_curr_title = "";			// STRING FOR CURRENT MOVIE TITLE

		try
		{
			/* FILE INPUT STREAM SETUP FOR DATABASE */
			br_file_in = new BufferedReader( new FileReader(str_database) );

			System.out.println(" Searching for '"+str_search+"' . . . \n");

			/* SEARCH AND LIST IF FOUND */
			while( br_file_in.ready() && b_found_movie == false )
			{
				Videos_movie.read_movie(br_file_in);

				str_curr_title = Videos_movie.get_title();

				if( str_curr_title.compareTo(str_search) == 0 )
				{
					b_found_movie = true;
					Videos_movie.print_movie();
					System.out.println(" Stopped Search.\n");
				} // if
			} // while

			/* NOTIFY IF TITLE NOT FOUND */
			if(b_found_movie == false)
				System.out.println(" The title '"+str_search+"' was not found.\n");
		} // try
		/* EXCEPTION HANDLING */
		catch(FileNotFoundException exception)
		{
			System.out.println(" FATAL EXCEPTION: Database input file '"+str_database+"' not found.");
			System.exit(-1);
		} // catch
		catch(IOException exception)
		{
			System.out.println(" FATAL EXCEPTION: File input problem.");
			System.exit(-1);
		} // catch
	} // method search

	/*************************************************************************
	 * Method: list                                                          *
	 * Purpose: Lists the contents of the video database.                    *
	 * Input: --PARAMATERS--                                                 *
	 *        => 'str_database' = The filename of the video database.        *
	 * Output: --EXIT VALUE--                                                *
	 *         => FATAL EXCEPTION = -1                                       *
	 *************************************************************************/
	public static void list(String str_database)
	{
		/* LOCAL VARIABLES */
		Videos Videos_movie = new Videos();	// VIDEO OBJECT FOR MOVIE
		BufferedReader br_file_in;			// BUFFERED READER FOR INPUT FILE

		try
		{
			/* FILE INPUT STREAM SETUP FOR DATABASE */
			br_file_in = new BufferedReader( new FileReader(str_database) );

			System.out.println(" Database:\n");

			/* SEARCHING FOR TITLE AND LISTING IF FOUND */
			while( br_file_in.ready() )
			{
				Videos_movie.read_movie(br_file_in);

				Videos_movie.print_movie();
			} // while

			System.out.println(" End of Database.\n");
		} // try
		/* EXCEPTION HANDLING */
		catch(FileNotFoundException exception)
		{
			System.out.println(" FATAL EXCEPTION: Database input file '"+str_database+"' not found.");
			System.exit(-1);
		} // catch
		catch(IOException exception)
		{
			System.out.println(" FATAL EXCEPTION: File input problem.");
			System.exit(-1);
		} // catch
	} // method list
} // class Database
