/************************************************** Component: Transaction Module: Withdraw **************************************************/ /* include statements */ #include #include #include #include #include "constants.h" #include "database.h" #include "screen_output.h" #include "getch.h" #include "withdraw.h" #include "main_.h" /* functions */ int tr_Withdraw(char *cardNumber) { int exit_condition = 0; char ch; long int CashInABM; char temp[2] = " "; int i; long int withdraw_amount_100x = 0; double withdraw_amount = 0.0; int number_of_accounts; char accountType[MAX_ACCOUNTS+1][MAX_ACCOUNT_TYPE_LENGTH+1]; char accountNumber[MAX_ACCOUNTS+1][MAX_ACCOUNT_NUMBER_LENGTH+1]; char msg[MESSAGE_BUFFER_LENGTH]; char source_account_number[MAX_ACCOUNT_NUMBER_LENGTH+1]; // for receipts double availableBalanceR; double totalBalanceR; db_GetNumAccounts(cardNumber, &number_of_accounts); if (DEBUG_ON == 1) { printf ("Pre-Number of accounts = [%d]\n", number_of_accounts); printf ("Press any key to continue...\n"); getch(); } if (number_of_accounts <= 0) { io_DrawTopLine(); printf ("No accounts attached to this card\n"); printf ("Press any key to continue...\n"); io_DrawBottomLine(); getch(); return 1; } else if (number_of_accounts > MAX_ACCOUNTS) { number_of_accounts = MAX_ACCOUNTS; } for (i=0;i= 1 && atoi(temp) <= number_of_accounts) { strcpy(source_account_number, accountNumber[atoi(temp)-1]); withdraw_amount_100x = tr_GetWithdrawAmount(cardNumber, source_account_number); if (withdraw_amount_100x == -1) { // user cancelled operation exit_condition = 1; } withdraw_amount = (double)withdraw_amount_100x / 100; if (withdraw_amount > 0) { // HANDLE THE WITHDRAW switch(db_WithdrawMoney(cardNumber, accountNumber[atoi(temp)-1], withdraw_amount)) { case 0: // remove money from abm CashInABM = m_ReadABMCashLevel(); m_WriteABMCashLevel((long int)(CashInABM-withdraw_amount)); // PRINT RECEIPT io_DrawTopLine(); printf ("Withdraw Receipt:\n"); printf ("Withdrew $%10.2lf\n", withdraw_amount); printf ("from account: %s\n", source_account_number); db_GetAccountBalance(cardNumber, source_account_number, &availableBalanceR, &totalBalanceR); printf ("Ending Balances for account: %s\n", source_account_number); printf ("Total Balance: $%10.2lf\n", totalBalanceR); printf ("Available Balance: $%10.2lf\n", availableBalanceR); io_DrawBottomLine(); printf ("Press any key to continue...\n"); getch(); // END RECEIPT break; default: printf("Error withdrawing - aborting transaction.\n"); } } } } else if (ch == 'c') exit_condition = 1; } while (exit_condition != 1); return 0; } long int tr_GetWithdrawAmount(char* cardNumber, char* source_account_number) { int exit_condition = 0; int W_position = 0; int warn_invalid_W = 0; int warn_no_zero_W = 0; int warn_20s = 0; int warn_balance_too_low = 0; int correct_value_entered = 0; char account_W[MDD+1]; // amount to deposit into the account char ch; char print1[MDD+1], print2[3]; // hold the split up dollar value int i,j; double amountWithdrawnToday; double availableBalance; double totalBalance; account_W[W_position] = '\0'; do { io_DrawTopLine(); printf ("Enter amount you want to withdraw:\n\n"); printf ("e - enter\n"); printf ("b - backspace\n"); printf ("c - cancel\n"); if (warn_20s == 1) { printf ("Withdraw amount must a multiple of $20\n"); warn_20s = 0; } if (warn_invalid_W == 1) { printf ("Maximum daily withdraw allowed: $%10.2lf\n", (double)MAX_WITHDRAW); printf ("Daily withdraw limit remaining: $%10.2lf\n", (double)MAX_WITHDRAW - amountWithdrawnToday); warn_invalid_W = 0; } if (warn_no_zero_W == 1) { printf ("Cannot complete. Your withdraw amount must be > 0\n"); warn_no_zero_W = 0; } if (warn_balance_too_low == 1) { printf ("Source account has inadequate funds.\n"); warn_balance_too_low = 0; } io_DrawMidLine(); /* Split up string "ab" into two strings "a" and "b" b is of length 2, a is variable. Display as cash. */ j = 0; for (i=0;i<=(int)strlen(account_W);i++) { if ((int)strlen(account_W)-i > 2) print1[i] = account_W[i]; else { print1[i] = '\0'; print2[j] = account_W[i]; j++; } } //print2[j-1] = '\0'; printf ("$%10s.%2s\n", print1, print2); io_DrawBottomLine(); ch = getch(); if (isdigit(ch)) { account_W[W_position] = ch; W_position++; } else if (ch == 'c') { exit_condition = 1; } else if (ch == 'e') { if(db_GetAmountWithdrawnToday(cardNumber, &amountWithdrawnToday)!=0) { io_DrawTopLine(); printf ("Error retrieving withdraw limit remaining\n"); printf ("Press any key to continue...\n"); io_DrawBottomLine(); getch(); return -1; } if(db_GetAccountBalance(cardNumber, source_account_number, &availableBalance, &totalBalance)!=0) { io_DrawTopLine(); printf ("Error retrieving account Balance\n"); printf ("Press any key to continue...\n"); io_DrawBottomLine(); getch(); return -1; } if (DEBUG_ON == 1) { //printf ("Withdraw limit exceeded, transaction not executed\n"); printf ("amountWithdrawnToday = [$%lf]\n", amountWithdrawnToday); printf ("Press any key to continue\n"); getch(); } // Check if this withdraw is a valid $ amount if (((double)MAX_WITHDRAW - amountWithdrawnToday)*100 < (double)atoi(account_W)) { /* over daily withdraw limit */ /* withdraw amount not valid */ W_position = 0; warn_invalid_W = 1; } else if (atoi(account_W) <= 0) { W_position = 0; warn_no_zero_W = 1; } else if (atoi(account_W)%2000 != 0) { /* test if multiple of $20 */ W_position = 0; warn_20s = 1; } else if ((double)atoi(account_W) > availableBalance*100) { W_position = 0; warn_balance_too_low = 1; } else { /* withdraw allowed */ correct_value_entered = 1; exit_condition = 1; } if (DEBUG_ON == 1) { printf ("amountWithdrawnToday = [$%lf]\n", amountWithdrawnToday); printf ("Press any key to continue\n"); getch(); } } else if (ch == 'b') { if (W_position >= 1) { W_position--; } } if (W_position > MDD) { W_position = MDD; } account_W[W_position] = '\0'; } while (exit_condition != 1); if (correct_value_entered == 1) { return atoi(account_W); } else { return -1; } }