#include #include #include #include "linklist.h" #include "database.h" #include "databaseInit.h" // Following is the private variable that points to the head of the linked // list database. This variable is static so that it is visible only // to functions within this file. Only the database component should // be able to access the database list directly. The calls to functions // in this component reference the customers and account by account number, // and with no knowledge of the internal workings of the database. static customerList databaseHeadPointer = NULL; /*-------------------------------------------------------------- * db_InitDatabase() * * Description: Master function called when program begins that * creates and fills the database with init data from file. * * Module: db - Database simulator for ABM software * * Arguments: * char* filename: filename that contains database init file * * Return values: * 0 - Successful * 1 - Couldn't open input file * 2 - Other error *--------------------------------------------------------------*/ // Master database init function char db_InitDatabase (char *filename) { // The actual initialization code is in databaseInit.c, but // other modules will only know about functions in this file, // so hide the real function through this call. return(dbinit_InitDatabase(filename, &databaseHeadPointer)); } /*-------------------------------------------------------------- * db_ValidateCardNumberAndPIN() * * Description: Checks that a card number exists in the * database, and that the PIN number matches the PIN * number passed in. * * Module: db - Database simulator for ABM software * * Arguments: * char* cardNumber: Pointer to string containing card number * char* PIN: Pointer to string contining PIN number * * Return values: * 0 - Successful * 1 - Invalid card number * 2 - Card number exists, but invalid PIN * 3 - Card locked out (disabled) * 4 - Other error *--------------------------------------------------------------*/ char db_ValidateCardNumberAndPIN(char* cardNumber, char* PIN) { customerList customer = NULL; char tempPIN[PIN_NUMBER_LENGTH+1]; customer = (customerList)linklist_FindCustomer(databaseHeadPointer, cardNumber); // If couldn't find the customer's entry in the database if (customer == NULL) return(1); // Invalid card // If internal error while getting PIN number from database if (linklist_GetPIN(customer, tempPIN) == 1) return(4); // If PIN number invalid if (strcmp(tempPIN, PIN)) { linklist_IncrementCustomerFailedPinAttempts(customer); return(2); } // If account locked out if(linklist_IsAccountLocked(customer) == 0) return(0); else if (linklist_IsAccountLocked(customer) == 1) return(3); else return(4); } // End db_ValidateCardNumberAndPIN() /*-------------------------------------------------------------- * db_GetNumAccounts() * * Description: Determines how many accounts a user has * associated with their account (card number). * * Module: db - Database simulator for ABM software * * Arguments: * char* cardNumber: Pointer to string containing card number * int* numberAccounts: Pointer to integer where number of * accounts will be stored by this function. * * Return values: * 0 - Successful * 1 - Couldn't find card number * 2 - Card locked out (disabled) * 3 - Other error *--------------------------------------------------------------*/ char db_GetNumAccounts(char* cardNumber, int* numberAccounts) { customerList customer = NULL; customer = (customerList)linklist_FindCustomer(databaseHeadPointer, cardNumber); // If couldn't find the customer's entry in the database if (customer == NULL) return(1); // Invalid card // Check whether account is locked if (linklist_IsAccountLocked(customer) == 1) return(2); *numberAccounts = linklist_FindNumberAccounts(customer); return(0); } // End db_GetNumAccounts() /*-------------------------------------------------------------- * db_GetAccountBalance() * * Description: Obtains the available and total (including * holds) balances for the specified account. * * Module: db - Database simulator for ABM software * * Arguments: * char* cardNumber: Pointer to string containing card number * char* accountNumber: Pointer to string with account * number of interest. * double* availableBalance: Pointer to double where available * balance will be stored by this function. * double* totalBalance: Pointer to double where total * balance will be stored by this function. * * Return values: * 0 - Successful * 1 - Couldn't find card number * 2 - Couldn't find account number * 3 - Card locked out (disabled) * 4 - Other error *--------------------------------------------------------------*/ char db_GetAccountBalance(char* cardNumber, char* accountNumber, double* availableBalance, double* totalBalance) { customerList customer = NULL; customer = (customerList)linklist_FindCustomer(databaseHeadPointer, cardNumber); // If couldn't find the customer's entry in the database if (customer == NULL) return(1); // Invalid card // Check whether card is locked if (linklist_IsAccountLocked(customer) == 1) return(3); if(linklist_GetAvailableBalance(customer, accountNumber, availableBalance) != 0) return(4); if(linklist_GetTotalBalance(customer, accountNumber, totalBalance) != 0) return (4); return(0); } // End db_GetAccountBalance() /*-------------------------------------------------------------- * db_DepositCash() * * Description: Sets available balance within an account. cd .. ; cd v18 * * Module: db - Database simulator for ABM software * * Arguments: * char* cardNumber: Pointer to string containing card number * char* accountNumber: Pointer to string with account * number of interest. * double deposit: Amount to deposit into account * * Return values: * 0 - Successful * 1 - Couldn't find card number * 2 - Couldn't find account number * 3 - Card locked out (disabled) * 4 - Other error *--------------------------------------------------------------*/ char db_DepositCash(char* cardNumber, char* accountNumber, double deposit) { customerList customer = NULL; customer = (customerList)linklist_FindCustomer(databaseHeadPointer, cardNumber); // If couldn't find the customer's entry in the database if (customer == NULL) return(1); // Invalid card // Check whether card is locked if (linklist_IsAccountLocked(customer) == 1) return(3); if(linklist_AddCash(customer, accountNumber, deposit) != 0) return(4); return(0); } // End db_DepositCash() /*-------------------------------------------------------------- * db_DepositCheque() * * Description: Adds cheque to held cheque queue within the account. * * Module: db - Database simulator for ABM software * * Arguments: * char* cardNumber: Pointer to string containing card number * char* accountNumber: Pointer to string with account * number of interest. * double deposit: Amount to deposit into account * * Return values: * 0 - Successful * 1 - Couldn't find card number * 2 - Couldn't find account number * 3 - Card locked out (disabled) * 4 - Other error *--------------------------------------------------------------*/ char db_DepositCheque(char* cardNumber, char* accountNumber, double deposit) { customerList customer = NULL; customer = (customerList)linklist_FindCustomer(databaseHeadPointer, cardNumber); // If couldn't find the customer's entry in the database if (customer == NULL) return(1); // Invalid card // Check whether card is locked if (linklist_IsAccountLocked(customer) == 1) return(3); if(linklist_AddHeldCheque(databaseHeadPointer, cardNumber, accountNumber, deposit) != 0) return(4); return(0); } // End db_DepositCheque() /*-------------------------------------------------------------- * db_GetAccountInfo() * * Description: Returns type and number of account associated * with a card number. * * Module: db - Database simulator for ABM software * * Arguments: * char* cardNumber: Pointer to string containing card number * number of interest. * int accountIndexIntoList: Zero-based arbitrary index of account * char* accountType: Pointer to string where the account * type will be stored by this function. * char* accountNumber: Pointer to string where account * number will be stored by this function. * * Return values: * 0 - Successful * 1 - Couldn't find card number * 2 - Account index invalid * 3 - Card locked out (disabled) * 4 - Invalid account index * 5 - Other error *--------------------------------------------------------------*/ char db_GetAccountInfo(char* cardNumber, int accountIndexIntoList, char* accountType, char* accountNumber) { customerList customer = NULL; int numAccounts; customer = (customerList)linklist_FindCustomer(databaseHeadPointer, cardNumber); // If couldn't find the customer's entry in the database if (customer == NULL) return(1); // Invalid card // Check whether account is locked if (linklist_IsAccountLocked(customer) == 1) return(2); if(db_GetNumAccounts(cardNumber, &numAccounts) != 0) return(5); if ((accountIndexIntoList >= numAccounts) || (accountIndexIntoList < 0)) return(4); linklist_GetAccountInfo(customer, accountIndexIntoList, accountType, accountNumber); return(0); } // End db_GetAccountInfo() /*-------------------------------------------------------------- * db_GetAmountWithdrawnToday() * * Description: Checks how much money has been withdrawn by * the customer during the current day. * * Module: db - Database simulator for ABM software * * Arguments: * char* cardNumber: Pointer to string containing card number * number of interest. * double* amountWithdrawnToday: Pointer to a double where * the amount withdrawn during the current day * is stored. * * Return values: * 0 - Successful * 1 - Couldn't find card number * 2 - Other error *--------------------------------------------------------------*/ char db_GetAmountWithdrawnToday(char* cardNumber, double* amountWithdrawnToday) { customerList customer = NULL; customer = (customerList)linklist_FindCustomer(databaseHeadPointer, cardNumber); // If couldn't find the customer's entry in the database if (customer == NULL) return(1); // Invalid card // Check whether account is locked if (linklist_IsAccountLocked(customer) == 1) return(2); if(linklist_GetCashWithdrawn(customer, amountWithdrawnToday) != 0) return(2); return(0); } // End db_GetAmountWithdrawnToday() /*-------------------------------------------------------------- * db_WithdrawMoney() * * Description: Withdraws money from the specified account, and * increments the sum of total money * withdrawn during the given day. * * Module: db - Database simulator for ABM software * * Arguments: * char* cardNumber: Pointer to string containing card number * char* accountNumber: Pointer to string with account * number of interest. * double withdrawal: Amount to deposit into account * * Return values: * 0 - Successful * 1 - Couldn't find card number * 2 - Couldn't find account number * 3 - Card locked out (disabled) * 4 - Withdrawal amount negative * 5 - Other error *--------------------------------------------------------------*/ char db_WithdrawMoney(char* cardNumber, char* accountNumber, double withdrawal) { customerList customer = NULL; customer = (customerList)linklist_FindCustomer(databaseHeadPointer, cardNumber); // If couldn't find the customer's entry in the database if (customer == NULL) return(1); // Invalid card // Check whether card is locked if (linklist_IsAccountLocked(customer) == 1) return(3); if (withdrawal < 0) return(4); if(linklist_AddCash(customer, accountNumber, (-1*withdrawal)) != 0) return(5); linklist_AddToCashWithdrawn(customer, withdrawal); return(0); } // End db_WithdrawMoney() /*-------------------------------------------------------------- * db_NewDayRollup() * * Description: Updates the database values for the beginning of * a new day. * * Module: db - Database simulator for ABM software * * Arguments: * * Return values: * 0 - Successful * 1 - Other error *--------------------------------------------------------------*/ char db_NewDayRollup(void) { return(linklist_NewDayRollup(databaseHeadPointer)); } // End db_NewDayRollup()