/*********************************************************************
 * Authors: B. Alex Bridges and Miriam Becker                        *
 * File: database.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 database.                  *
 *********************************************************************/

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


/* CONSTANTS */
// NONE

/* GLOBAL VARIABLES */
Main* MainDBVar = new Main();       // MAIN VARIABLE
int bAccounts = MainDBVar->bfalse;  // EXISTENCE OF ACCOUNTS


/* METHODS */
/*************************************************************************
 * Method: DBOpenAccount                                                 *
 * Purpose: Requests the necessary information and creates the account.  *
 * Input: --PARAMETERS--                                                 *
 *        => none                                                        *
 * Output: --RETURNS--                                                   *
 *        => none                                                        *
 *************************************************************************/
void Database::DBOpenAccount(void)
{
  /* LOCAL VARIABLES */
  char charAName[30]; // SUPPLIED NAME ON ACCOUNT
  float fpAmount = 0; // SUPPLIED OPENING BALANCE

  /* SPECIAL CASE: FIRST RUN */
  if(bAccounts == MainDBVar->bfalse)
  {
    bAccounts = MainDBVar->btrue;
    iDBLastIndex = 0;
  } // if
  /* OTHER RUNS */
  else
    iDBLastIndex++;

  /* GET NAME */
  puts("Please enter your first and last name: ");
  fflush(stdin);
  gets(charAName);
  charAName[strlen(charAName)] = '\0';
  puts("");

  if(MainDBVar->bDebug)
    printf(" The name given was '%s'.\n\n",charAName);

  /* GET OPENING BALANCE */
  puts("Please enter the amount of your intial deposit:");
  scanf("%f",&fpAmount);
  puts("");

  if(MainDBVar->bDebug)
    printf(" The opening balance was '%f'.\n\n",fpAmount);

  /* CREATE ACCOUNT */
  AccountArr[iDBLastIndex] = new Account(charAName, fpAmount, iDBLastIndex);

  /* NOTIFY USER */
  printf("Congratulations %s on opening a new checking account.\n",charAName);
  printf("Your account currently has $%f.\n",AccountArr[iDBLastIndex]->fpBalance);
  printf("Your assigned A.M. bank account number is '%d'\n",AccountArr[iDBLastIndex]->iAccountNo);
  printf("Please save this number for future reference.\n\n");

  MainDBVar->PressContinueKey();
} // method DBOpenAccount

/*************************************************************************
 * Method: DBCloseAccount                                                *
 * Purpose: Requests the necessary information and closes the account.   *
 * Input: --PARAMETERS--                                                 *
 *        => none                                                        *
 * Output: --RETURNS--                                                   *
 *        => none                                                        *
 *************************************************************************/
void Database::DBCloseAccount(void)
{
  /* LOCAL VARIABLES */
  int i;  // ARRAY INDEX

  /* GET AND LOOKUP ACCOUNT NUMBER */
  i = DBFindAccount();

  /* REPORT RESULTS */
  if(i == -1)
  {
    puts("Sorry, but the account number was not found and therefore could not be closed.\n");
  } // if
  else if (i == -2)
  {
    puts("Sorry, but no accounts are avaible at this time.\n");
  } // else if
  else
  {
    printf("The account number for %s was found and is now being closed.\n\n",AccountArr[i]->charAName);

    if(MainDBVar->bDebug)
      printf(" The current index is '%d'.\n\n",i);

    strcpy(AccountArr[i]->charAName,"null");
    AccountArr[i]->fpBalance = 0;
    AccountArr[i]->iAccountNo = -1;

    if(MainDBVar->bDebug)
    {
      printf(" Resulting Account:\n");
      printf(" - Name = '%s'\n",AccountArr[i]->charAName);
      printf(" - Balance = '%f'\n",AccountArr[i]->fpBalance);
      printf(" - Account Number = '%d'\n\n",AccountArr[i]->iAccountNo);
    } // if
  } // else
} // method DBCloseAccount

/*************************************************************************
 * Method: DBFindAccount                                                 *
 * Purpose: Requests the necessary information to find an account.       *
 * Input: --PARAMETERS--                                                 *
 *        => none                                                        *
 * Output: --RETURNS--                                                   *
 *        => The index of the account number or                          *
 *           -1 if it could not be found or                              *
 *           -2 if no accounts are available.                            *
 *************************************************************************/
int Database::DBFindAccount(void)
{
  /* LOCAL VARIABLES */
  int iGivenNumber = 0; // SUPPLIED ACCOUNT NUMBER

  /* ACCOUNTS AVAILABLE? */
  if(bAccounts == MainDBVar->bfalse)
    return(-2);

  /* GET ACCOUNT NUMBER */
  puts("Please enter the number of the account:");
  scanf("%d",&iGivenNumber);
  puts("");

  if(MainDBVar->bDebug)
    printf(" The account number given was '%d'.\n\n",iGivenNumber);

  /* LOOKUP ACCOUNT */
  return ( DBFindAccount(iGivenNumber) );
} // method DBFindAccount

/*************************************************************************
 * Method: DBFindAccount                                                 *
 * Purpose: Searches the database for a matching account number.         *
 * Input: --PARAMETERS--                                                 *
 *        => iFindNumber = The account number which is to be found.      *
 * Output: --RETURNS--                                                   *
 *        => The index of the account number or                          *
 *           -1 if it could not be found.                                *
 *************************************************************************/
int Database::DBFindAccount(int iFindNumber)
{
  /* LOCAL VARIABLES */
  int i;      // LOOP COUNT
  int bMatch; // MATCH FOR ACCOUNT

  /* LOOKUP ACCOUNT */
  // => DEFAULT VALUES
  i = 0;
  bMatch = MainDBVar->bfalse;

  // => LOOP THROUGH ACCOUNTS UNTIL MATCH OR END
  printf("Searching for account number '%d' . . . ",iFindNumber);
  while( (bMatch == MainDBVar->bfalse) && (i <= iDBLastIndex) )
  {
    if(MainDBVar->bDebug == MainDBVar->btrue)
      printf("'%d',",i);
    
    if(AccountArr[i]->iAccountNo == iFindNumber)
      bMatch = MainDBVar->btrue;
    else
    {
      i++;
    } // else
  } // while
  //bMatch == MainDBVar->bfalse && i <= iDBLastIndex
  printf("COMPLETED!\n\n");

  // => RETURN RESULTS
  if(bMatch == MainDBVar->btrue)
    return(i);
  else
    return(-1);

} // method DBFindAccount

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


/* DESTRUCTOR */
Database::~Database()
{
  delete[] *AccountArr;
} // destructor Database

