/*********************************************************************
 * Authors: B. Alex Bridges and Miriam Becker                        *
 * File: menus.cpp                                                   *
 * Class: CPSC-451, Summer 1999                                      *
 * Project: Project 1 - Account Maintenance                          *
 * Description: This program will simulate the ATM machine at a      *
 *              local bank.                                          *
 * Contents: Methods for dealing with the menus.                     *
 *********************************************************************/

/* INCLUDES */
// => PRE-DEFINED
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// => USER-DEFINED
#include "menus.h"
#include "main.h"
#include "database.h"


/* CONSTANTS */
// NONE

/* GLOBAL VARIABLES */
Main* MainMenusVar = new Main();  // MAIN VARIABLE
Database* DBVar = new Database(); // DATABASE VARIABLE

/* METHODS */
/*************************************************************************
 * Method: PrintMenus                                                    *
 * Purpose: Controls the menus and corresponding actions.                *
 * Input: --PARAMETERS--                                                 *
 *        => none                                                        *
 * Output: --RETURNS--                                                   *
 *        => none                                                        *
 *************************************************************************/
void Menus::PrintMenus(void)
{
  /* LOCAL VARIABLES */
  Main* MainMenusVar = new Main();  // MAIN VARIABLE
  int bValid;                       // VALIDITY OF USER INPUT
  char charAGiven[30];              // CHARACTERS READ
  int bContinue;                    // VALIDITY OF ACCOUNT NUMBER
  int i;                            // ARRAY INDEX

  /* MAIN MENU OPTIONS */

  do {
    // => DEFAULT VALUES
    start:bValid = MainMenusVar->bfalse;
    charAGiven[0] = '\0';

    MainMenusVar->ClearScreen();
    // => DISPLAYING MENU OPTIONS
    puts("Please choose one of the following commands:");
    puts("o -- open new account");
    puts("t -- transactions on existing account");
    puts("c -- close an account\n");
    printf("Enter a Letter - [o,t,c]? ");

    // => READING OF USER INPUT
    fflush(stdin);
    gets(charAGiven);
    puts("");
    if(MainMenusVar->bDebug)
      printf(" '%c' was the chosen command.\n\n",charAGiven[0]);

    // => RESULTING ACTIONS BASED ON USER INPUT
    if( strcmp(charAGiven,"o") == 0)
    {
      bValid = MainMenusVar->btrue;
      MainMenusVar->ClearScreen();
      DBVar->DBOpenAccount();
    } // if
    // ==> TRANSACTIONS
    else if( strcmp(charAGiven,"t") == 0)
    {
      bValid = MainMenusVar->btrue;
      MainMenusVar->ClearScreen();

      /* TRANSACTION MENU OPTIONS */
      // => DEFAULT VALUES
      bValid = MainMenusVar->bfalse;
      charAGiven[0] = '\0';

      /* TRANSACTIONS ON ACCOUNT */
      // => DEFAULT VALUES PART 1
      bContinue = MainMenusVar->bfalse;
      do {
        // => DEFAULT VALUES PART 2
        charAGiven[0] = '\0';

        MainMenusVar->ClearScreen();
        
        while (bContinue == MainMenusVar->bfalse)
        {
          // => GET ACCOUNT NUMBER AND LOOK IT UP
          i = DBVar->DBFindAccount();

          if(i == -1)
          {
            puts("Sorry, but the account number was not found and therefore could not be referenced.\n");
            MainMenusVar->PressContinueKey();
            MainMenusVar->ClearScreen();
          } // if
          else if (i == -2)
          {
            puts("Sorry, but no accounts are avaible at this time.\n");
            MainMenusVar->PressContinueKey();
            MainMenusVar->ClearScreen();
            goto start;
          } // else if
          else
          {
            bContinue = MainMenusVar->btrue;
            printf("The account number for %s was found and is now being referenced.\n\n",DBVar->AccountArr[i]->charAName);

            if(MainMenusVar->bDebug)
              printf(" The current index is '%d'.\n\n",i);
          } // else
        } // while

        // => DISPLAYING MENU OPTIONS
        puts("Please choose one of the following commands:");
        puts("d -- deposit");
        puts("w -- withdraw");
        puts("i -- inquire balance\n");
        puts("g -- go back go the previous menu\n");
        printf("Enter a Letter - [d,w,i,g]? ");

        // => READING OF USER INPUT
        fflush(stdin);
        gets(charAGiven);
        puts("");
        if(MainMenusVar->bDebug)
          printf(" '%c' was the chosen command.\n\n",charAGiven[0]);

        // => RESULTING ACTIONS BASED ON USER INPUT
        if( strcmp(charAGiven,"d") == 0 )
        {
          bValid = MainMenusVar->btrue;
          MainMenusVar->ClearScreen();
          DBVar->AccountArr[i]->DepWith(MainMenusVar->btrue);
          MainMenusVar->PressContinueKey();
        } // if
        else if( strcmp(charAGiven,"w") == 0 )
        {
          bValid = MainMenusVar->btrue;
          MainMenusVar->ClearScreen();
          DBVar->AccountArr[i]->DepWith(MainMenusVar->bfalse);
          MainMenusVar->PressContinueKey();
        } // else if
        else if( strcmp(charAGiven,"i") == 0 )
        {
          bValid = MainMenusVar->btrue;
          MainMenusVar->ClearScreen();
          DBVar->AccountArr[i]->Inquiry();
          MainMenusVar->PressContinueKey();
        } // else if
        else if( strcmp(charAGiven,"g") == 0 )
        {
          bValid = MainMenusVar->btrue;
        } // else if
        else
          puts(" ERROR: Invalid menu option. Please try again.\n");
      } while (strcmp(charAGiven,"g") != 0 );

    } // else if
    // ==> CLOSE ACCOUNT
    else if( strcmp(charAGiven,"c") == 0)
    {
      bValid = MainMenusVar->btrue;
      MainMenusVar->ClearScreen();
      DBVar->DBCloseAccount();
      MainMenusVar->PressContinueKey();
    } // else if
    // ==> INVALID COMMAND
    else
      puts(" ERROR: Invalid menu option. Please try again.\n");
  } while(bValid == MainMenusVar->bfalse);
} // method printMenus

/* CONSTRUCTORS */
/*************************************************************************
 * Constructor: Menus                                                    *
 * Purpose: Creates object which allows access to its methods and        *
 *          variables.                                                   *
 * Input: --PARAMETERS--                                                 *
 *        => none                                                        *
 *************************************************************************/
Menus::Menus()
{
} // constructor Menus


/* DESTRUCTOR */
Menus::~Menus()
{
} // destructor Menus
