/*********************************************************************
 * Author: B. Alex Bridges                                           *
 * Login ID: brid0129                                                *
 * Class: CPSC-102, Summer 1998                                      *
 * Project: Programming Assignment 3                                 *
 * 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 = false;			// CONTROLS EXTRA DEBUG OUTPUT

	/* GLOBAL VARIABLES */
	public static Videos Videos_in = new Videos();	// LINKED LIST VIDEO OBJECT FOR IN STOCK MOVIES
	public static Videos Videos_out = new Videos();	// LINKED LIST VIDEO OBJECT FOR CHECKED OUT MOVIES

	/*************************************************************************
	 * Method: read_db                                                       *
	 * Purpose: Reads the video database, storing contents into the          *
	 *          appropriate linked-list.                                     *
	 * Input: --PARAMATERS--                                                 *
	 *        => 'str_database' = The filename of the video database.        *
	 * Output: --EXIT VALUE--                                                *
	 *         => FATAL EXCEPTION = -1                                       *
	 *************************************************************************/
	public static void read_db(String str_database)
	{
		/* LOCAL VARIABLES */
		BufferedReader br_file_in;	// BUFFERED READER FOR INPUT FILE
		Videos Videos_current;		// VIDEO OBJECT FOR CURRENT MOVIE
		int int_pos;				// POSITION AT WHICH TO INSERT MOVIE

		try
		{
			/* FILE INPUT STREAM SETUP FOR DATABASE */
			br_file_in = new BufferedReader( new FileReader(str_database) );

			System.out.println(" Reading the video database '"+str_database+"' . . . ");

			/* LIST BASED ON SELECTED AVAILABILITY */
			while( br_file_in.ready() )
			{
				Videos_current = new Videos();
				Videos_current.get_movie(br_file_in);

				if( Videos_current.str_available.equals("true") )
				{
					int_pos = Videos.insert_positon(Videos_in, Videos_current);
					Videos_in = Videos.insert_movie(Videos_in, int_pos, Videos_current);
				} // if
				else if( Videos_current.str_available.equals("false") )
				{
					int_pos = Videos.insert_positon(Videos_out, Videos_current);
					Videos_out = Videos.insert_movie(Videos_out, int_pos, Videos_current);
				} // else
			} // while

			System.out.println(" Read completed.\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 read_db

	/*************************************************************************
	 * Method: availability                                                  *
	 * Purpose: Modifies the availability of a given movie.                  *
	 * Input: --PARAMATERS--                                                 *
 	 *        => 'str_given'      = The title for which to modify.           *
	 *        => 'b_availability' = The expected availability.               *
	 * Output: none                                                          *
	 *************************************************************************/
	public static void availability(String str_given, boolean b_availability)
	{
		/* LOCAL VARIABLES */
		Videos Videos_in_result;	// VIDEOS OBJECT FOR RESULT MOVIE FROM IN STOCK LINKED LIST
		Videos Videos_out_result;	// VIDEOS OBJECT FOR RESULT MOVIE FROM CHECKED OUT LINKED LIST
		boolean b_in;				// BOOLEAN FOR EXISTENCE OF MOVIE AS IN STOCK VIDEO
		boolean b_out;				// BOOLEAN FOR EXISTENCE OF MOVIE AS CHECKED OUT VIDEO
		int int_pos;				// POSITION AT WHICH TO INSERT MOVIE

		/* VERIFY EXISTENCE */
		System.out.println(" Verifying existence of '"+str_given+"' . . . ");

		// => IN STOCK LINKED LIST
		Videos_in_result = Videos.find_movie(Videos_in, str_given);

		if(Videos_in_result != null)
			b_in = true;
		else
			b_in = false;

		// => CHECKED OUT LINKED LIST
		Videos_out_result = Videos.find_movie(Videos_out, str_given);

		if(Videos_out_result != null)
			b_out = true;
		else
			b_out = false;

		// => RESULTS
		if(!b_in && !b_out)
		{
			System.out.println(" ERROR: The title '"+str_given+"' is NOT carried.\n"+
			                   " End of verifying existence.\n");
			return;
		} // if
		else
		{
			System.out.println(" The title '"+str_given+"' is carried.\n"+
			                   " End of verifying existence.\n");
		} // else

		/* VERIFY STATUS */
		System.out.println(" Verifying status of '"+str_given+"' . . . ");

		// => RETURNING VIDEO, BUT ALREADY IN STOCK
		if(b_availability && b_in)
		{
			System.out.println(" ERROR: The title '"+str_given+"' is already in stock.\n"+
			                   " End of verifying status.\n");
			return;
		} // if
		// => CHECKING OUT VIDEO, BUT ALREADY CHECKED OUT
		else if (!b_availability && b_out)
		{
			System.out.println(" ERROR: The title '"+str_given+"' is already checked out.\n"+
			                   " End of verifying status.\n");
			return;
		} // if
		// => RETURNING VIDEO WHICH IS CHECKED OUT
		else if(b_availability && !b_in && b_out)
		{
			System.out.println(" The title '"+str_given+"' can be returned.\n"+
			                   " End of verifying status.\n");
		} // if
		// => CHECKING OUT VIDEO WHICH IS IN STOCK
		else if(!b_availability && b_in && !b_out)
		{
			System.out.println(" The title '"+str_given+"' can be checked out.\n"+
			                   " End of verifying status.\n");
		} // if
		else
			System.out.println(" ERROR: This case is NOT currently handled.\n"+
			                   "  -> Availibility = '"+b_availability+"'\n"+
   			                   "  -> In Stock     = '"+b_in+"'\n"+
   			                   "  -> Checked Out  = '"+b_out+"'\n"+
							   "\n"+
			                   " End of verifying status.\n");

		/* CHECK OUTS */
		if(!b_availability)
		{
			System.out.println(" Checking out the title '"+str_given+"' . . . ");

			// => DELETE FROM IN STOCK LINKED LIST
			Videos_in = Videos.delete_movie(Videos_in, str_given);

			// => ADJUST RESULT'S AVAILABILITY ACCORDINGLY
			Videos_in_result.str_available = "false";

			// => INSERT INTO CHECKED OUT LINKED LIST
			Videos_in_result.Videos_next = null; // DROP REMAINING LINKED LIST
			int_pos = Videos.insert_positon(Videos_out, Videos_in_result);
			Videos_out = Videos.insert_movie(Videos_out, int_pos, Videos_in_result);

			System.out.println(" Done checking out.\n");

			return;
		} // if
		/* RETURNS */
		else
		{
			System.out.println(" Returning the title '"+str_given+"' . . . ");

			// => DELETE FROM CHECKED OUT LINKED LIST
			Videos_out = Videos.delete_movie(Videos_out, str_given);

			// => ADJUST RESULT'S AVAILABILITY ACCORDINGLY
			Videos_out_result.str_available = "true";

			// => INSERT INTO IN STOCK LINKED LIST
			Videos_out_result.Videos_next = null; // DROP REMAINING LINKED LIST
			int_pos = Videos.insert_positon(Videos_in, Videos_out_result);
			Videos_in = Videos.insert_movie(Videos_in, int_pos, Videos_out_result);

			System.out.println(" Done returning.\n");

			return;
		} // else
	} // method availability

	/*************************************************************************
	 * Method: search                                                        *
	 * Purpose: Searches the linked lists for matching title.                *
	 * Input: --PARAMATERS--                                                 *
 	 *        => 'str_given'   = The title for which to search.              *
	 * Output: --EXIT VALUE--                                                *
	 *         => FATAL EXCEPTION = -1                                       *
	 *************************************************************************/
	public static void search(String str_given)
	{
		/* LOCAL VARIABLES */
		Videos Videos_result;	// VIDEOS OBJECT FOR RESULT MOVIE

		System.out.println(" Searching for '"+str_given+"' . . . ");

		/* IN STOCK LINKED LIST */
		Videos_result = Videos.find_movie(Videos_in, str_given);
		if(Videos_result != null)
		{
			System.out.println(" The title '"+str_given+"' was found to be in stock.\n");
			Videos_result.print_movie();
		} // if
		else
			System.out.println(" The title '"+str_given+"' was NOT found to be in stock.");

		/* CHECKED OUT LINKED LIST */
		Videos_result = Videos.find_movie(Videos_out, str_given);
		if(Videos_result != null)
		{
			System.out.println(" The title '"+str_given+"' was found to be checked out.\n");
			Videos_result.print_movie();
		} // if
		else
			System.out.println(" The title '"+str_given+"' was NOT found to be checked out.");

		System.out.println(" End of search.\n");
	} // method search

	/*************************************************************************
	 * Method: list                                                          *
	 * Purpose: Lists the contents of the selected linked list of Videos.    *
	 * Input: --PARAMATERS--                                                 *
	 *        => 'b_availability' = true ->Linked List for In Stock Movies   *
	 *                              false->Linked List for Checked Out Movies*
	 * Output: none                                                          *
	 *************************************************************************/
	public static void list(boolean b_availability)
	{
		System.out.println(" List:\n");

		/* LIST BASED ON SELECTED AVAILABILITY */
		if(b_availability)
			Videos.print_movie(Videos_in);
		else
			Videos.print_movie(Videos_out);

		System.out.println(" End of list.\n");
	} // method list
} // class Database
