// Function Prototypes
int read_accts(int acctnum_array[], double balance_array[], int max_accts);
void print_accts(int acctnum_array[], double balance_array[], int num_accts,
ofstream &dbfile);
void menu();
int findacct(int acctnum_array[], int num_accts, int requested_account);
void balance(int acctnum_array[], double balance_array[], int num_accts,
ofstream &outfile, ifstream &cin);
void deposit(int acctnum_array[], double balance_array[], int num_accts,
ofstream &outfile, ifstream &cin);
void withdrawal(int acctnum_array[], double balance_array[], int num_accts,
ofstream &outfile, ifstream &cin);
int new_acct(int acctnum_array[], double balance_array[], int num_accts,
ofstream &outfile, ifstream &cin);
int delete_acct(int acctnum_array[], double balance_array[], int num_accts,
ofstream &outfile, ifstream &cin);
void pause(void);
int main()
{
int acctnum_array[MAX_NUM]; //array of account numbers
double balance_array[MAX_NUM]; //array of balances
int num_accts; //number of accounts
char choice; //menu item selected
bool not_done = true; //loop control flag
// open input test cases file
//ifstream cin("c:\\bc\\cisc3110\\pgms\\chapter_00\\prj00_01BankAccounts\\mytestcases.txt"); //un-comment for file input of test cases
ifstream cin("con"); //un-comment for debugging
//ifstream cin("/bc/cisc3110/pgms/chapter_00/prj00_01BankAccounts/mytestcases.txt"); //for Mac users
//ifstream cin("/dev/stdin"); //for Mac users
// open output file
//ofstream outfile("c:\\bc\\cisc3110\\pgms\\chapter_00\\prj00_01BankAccounts\\myoutput.txt"); //comment-out for debugging
ofstream outfile("con"); //un-comment for debugging
//ofstream outfile("/bc/cisc3110/pgms/chapter_00/prj00_01BankAccounts/myoutput.txt"); //for Mac users
//ofstream outfile("/dev/stdout"); //for Mac users
outfile.setf(ios::fixed,ios::floatfield);
outfile.precision(2); //set decimal precision
/* first part */
/* fill and print initial database */
num_accts = read_accts(acctnum_array,balance_array,MAX_NUM);
print_accts(acctnum_array,balance_array,num_accts,outfile);
/* second part */
/* prompts for a transaction and then */
/* call functions to process the requested transaction */
do {
menu();
cin >> choice;
switch(choice)
{
case 'q':
case 'Q':
not_done = false;
print_accts(acctnum_array,balance_array,num_accts,outfile);
break;
case 'b':
case 'B':
balance(acctnum_array,balance_array,num_accts,outfile,cin);
break;
case 'd':
case 'D':
deposit(acctnum_array,balance_array,num_accts,outfile,cin);
break;
case 'w':
case 'W':
withdrawal(acctnum_array,balance_array,num_accts,outfile,cin);
break;
case 'n':
case 'N':
num_accts = new_acct(acctnum_array,balance_array,num_accts,outfile,cin);
break;
case 'x':
case 'X':
num_accts = delete_acct(acctnum_array,balance_array,num_accts,outfile,cin);
break;
default:
outfile << endl << "Error: '" << choice << "' is an invalid selection - try again" << endl << endl;
break;
}
// give user a chance to look at output before printing menu
pause();
} while (not_done);
outfile.close(); // close output file
return 0;
}
/* Function read_accts()
* Input:
* acctnum_array - reference to array of account numbers
* balance_array - reference to array of account balances
* max_accts - maximum number of active accounts allowed
* Process:
* Reads the initial database of accounts and balances
* Output:
* Fills in the initial account and balance arrays and returns the number of active accounts
*/
int read_accts(int acctnum_array[], double balance_array[], int max_accts)
{
// open input file
ifstream dbfile("c:\\bc\\cisc3110\\pgms\\chapter_00\\prj00_01BankAccounts\\myinput.txt"); //comment-out for debugging
//ifstream dbfile("con"); //un-comment for debugging
//ifstream dbfile("/bc/cisc3110/pgms/chapter_00/prj00_01BankAccounts/mytestcases.txt"); //for Mac users
//ifstream dbfile("/dev/stdin"); //for Mac users
int count = 0; //initialize count
while (dbfile >> acctnum_array[count] && count < max_accts)
{
dbfile >> balance_array[count];
count++;
}
dbfile.close();
return count;
}
/* Function print_accts:
* Input:
* acctnum_array - array of account numbers
* balance_array - array of account balances
* num_accts - number of active accounts
* dbfile - reference to output file
* Process:
* Prints the database of accounts and balances
* Output:
* Prints the database of accounts and balances
*/
void print_accts(int acctnum_array[], double balance_array[], int num_accts,
ofstream &dbfile)
{
dbfile << endl << endl;
dbfile << "\t\tDatabase of Bank Accounts" << endl << endl;
dbfile << "Account\tBalance" << endl << endl;
for (int index = 0; index < num_accts; index++)
{
dbfile << acctnum_array[index];
dbfile << "\t$" << balance_array[index];
dbfile << endl;
}
return;
}
/* Function menu()
* Input:
* none
* Process:
* Prints the menu of transaction choices
* Output:
* Prints the menu of transaction choices
*/
void menu()
{
cout << endl << endl;
cout << "Select one of the following transactions:" << endl;
cout << "\t****************************" << endl;
cout << "\t List of Choices " << endl;
cout << "\t****************************" << endl;
cout << "\t W -- Withdrawal" << endl;
cout << "\t D -- Deposit" << endl;
cout << "\t N -- New Account" << endl;
cout << "\t B -- Balance Inquiry" << endl;
cout << "\t X -- Delete Account" << endl;
cout << "\t Q -- Quit" << endl;
cout << endl << "\tEnter your selection: ";
return;
}
/* Function findacct:
* Input:
* acctnum_array - array of account numbers
* num_accts - number of active accounts
* requested_account - requested account requested_number
* Process:
* Performs a linear search on the acct_nunm array for the requested account
* Output:
* If found, the index of the requested account is returned
* Otherwise, returns -1
*/
int findacct(int acctnum_array[], int num_accts, int requested_account)
{
for (int index = 0; index < num_accts; index++)
if (acctnum_array[index] == requested_account)
return index;
return -1;
}
/* Function balance:
* Input:
* acctnum_array - array of account numbers
* balance_array - array of account balances
* num_accts - number of active accounts
* outfile - reference to output file
* cin - reference to the "test cases" input file
* Process:
* Prompts for the requested account
* Calls findacct() to see if the account exists
* If the account exists, the balamce is printed
* Otherwise, an error message is printed
* Output:
* If the account exists, the balance is printed
* Otherwise, an error message is printed
*/
void balance(int acctnum_array[], double balance_array[], int num_accts,
ofstream &outfile, ifstream &cin)
{
int requested_account;
int index;
cout << endl << "Enter the account number: "; //prompt for account number
cin >> requested_account;
index = findacct(acctnum_array, num_accts, requested_account);
if (index == -1) //invalid account
{
outfile << endl << "Transaction Requested: Balance Inquiry" << endl;
outfile << "Error: Account number " << requested_account << " does not exist" << endl;
}
else //valid zccount
{
outfile << endl << "Transaction Requested: Balance Inquiry" << endl;
outfile << "Account Number: " << requested_account << endl;
outfile << "Current Balance: $" << balance_array[index] << endl;
}
return;
}
/* Function deposit:
* Input:
* acctnum_array - array of account numbers
* balance_array - array of account balances
* num_accts - number of active accounts
* outfile - reference to the output file
* cin - reference to the "test cases" input file
* Process:
* Prompts for the requested account
* Calls findacct() to see if the account exists
* If the account exists, prompts for the amount to deposit
* If the amount is valid, it makes the depost and prints the new balance
* Otherwise, an error message is printed
* Output:
* For a valid deposit, the deposit transaction is printed
* Otherwise, an error message is printed
*/
void deposit(int acctnum_array[], double balance_array[], int num_accts,
ofstream &outfile, ifstream &cin)
{
int requested_account;
int index;
double amount_to_deposit;
cout << endl << "Enter the account number: "; //prompt for account number
cin >> requested_account;
index = findacct(acctnum_array, num_accts, requested_account);
if (index == -1) //invalid account
{
outfile << endl << "Transaction Requested: Deposit" << endl;
outfile << "Error: Account number " << requested_account << " does not exist" << endl;
}
else //valid account
{
cout << "Enter amount to deposit: "; //prompt for amount to deposit
cin >> amount_to_deposit;
if (amount_to_deposit <= 0.00) //invalid amount to deposit
{
outfile << endl << "Transaction Requested: Deposit" << endl;
outfile << "Account Number: " << requested_account << endl;
outfile << "Error: " << amount_to_deposit << " is an invalid amount" << endl;
}
else //valid deposit
{
outfile << endl << "Transaction Requested: Deposit" << endl;
outfile << "Account Number: " << requested_account << endl;
outfile << "Old Balance: $" << balance_array[index] << endl;
outfile << "Amount to Deposit: $" << amount_to_deposit << endl;
balance_array[index] += amount_to_deposit; //make the deposit
outfile << "New Balance: $" << balance_array[index] << endl;
}
}
return;
}
void withdrawal(int acctnum_array[], double balance_array[], int num_accts,
ofstream &outfile, ifstream &cin)
{
}
int new_acct(int acctnum_array[], double balance_array[], int num_accts,
ofstream &outfile, ifstream &cin)
{
return num_accts;
}
int delete_acct(int acctnum_array[], double balance_array[], int num_accts,
ofstream &outfile, ifstream &cin)
{
return num_accts;
}
// Function pause()
void pause(void)
{
system("pause");
return;
}
/*
// Function pause() - Alternate
void pause(void)
{
cout << endl << "press ENTER to continue: ";
getchar();
cin.ignore();
return;
}
*/