C++ Program based on previous code for "ProfessionalSolutions" Only !!

profileAli Haider
BankAccount2.cpp

// BankAccount2.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <fstream> #include <sstream> #include <cstdlib> //needed for system function #include <iostream> using namespace std; struct Depositer{ string first_name; string last_name; string social_security_num; }; class BankAccount{ public: int account_num; string account_type; double account_balance; Depositer depositer; BankAccount(){} BankAccount(string fname, string lname, string ssn, int acc_num, string acc_type, double balance) { depositer.first_name = fname; depositer.last_name = lname; depositer.social_security_num = ssn; account_num = acc_num; account_type = acc_type; account_balance = balance; } }; /////////////////////////////////////// Function Prototypes //////////////////////////////////////////////////// int read_accts(BankAccount account[], int max_accts); void print_accts(const BankAccount account[], int num_accts, ofstream &dbfile); void menu(); int findacct(const BankAccount account[], int num_accts, int requested_account); void balance(const BankAccount account[], int num_accts, ofstream &outfile); void deposit(BankAccount account[], int num_accts, ofstream &outfile); void withdrawal(BankAccount account[], int num_accts, ofstream &outfile); int new_acct(BankAccount account[], int num_accts, ofstream &outfile); int delete_acct(BankAccount account[], int num_accts, ofstream &outfile); void pause(void); void account_info(const BankAccount account[], int num_accts, ofstream &outfile); ////////////////////////////////////// Prototypes End Here ////////////////////////////////////////////////////// int _tmain(int argc, _TCHAR* argv[]) { const int MAX_NUM = 50; int num_accts; //number of accounts char choice; //menu item selected bool not_done = true; BankAccount bankaccounts[MAX_NUM]; ofstream outfile("F:\\output.txt"); // output file num_accts = read_accts(bankaccounts, MAX_NUM); print_accts(bankaccounts, num_accts, outfile); do { menu(); cin >> choice; switch (choice) { case 'q': case 'Q': not_done = false; print_accts(bankaccounts, num_accts, outfile); break; case 'b': case 'B': balance(bankaccounts, num_accts, outfile); break; case 'd': case 'D': deposit(bankaccounts, num_accts, outfile); break; case 'w': case 'W': withdrawal(bankaccounts, num_accts, outfile); break; case 'n': case 'N': num_accts = new_acct(bankaccounts, num_accts, outfile); break; case 'x': case 'X': num_accts = delete_acct(bankaccounts, num_accts, outfile); break; case 'i': case 'I': account_info(bankaccounts, num_accts, outfile); break; default: cout << 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); 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(BankAccount account[], int max_accts) { cout << "\t\t\t\tEnter Account Information" << endl << endl; string input; char choice; int count = 0; //initialize count string fname, lname, ssn, acc_type; int acc_num; double acc_balance; do{ cout << "Enter First Name" << endl << endl; cin >> fname; cout << "Enter Last Name" << endl << endl;; cin >> lname; cout << "Enter Social Security Number" << endl << endl;; cin >> ssn; cout << "Enter Account Number" << endl << endl;; cin >> acc_num; cout << "Select Account type" << endl << endl;; cout << "1. Checking" << endl; cout << "2. Savings" << endl; cout << "3. CD" << endl; char ch; cin >> ch; if (ch == '1') { acc_type = "checking"; } else if (ch == '2') { acc_type = "Savings"; } else if (ch == '3') { acc_type = "CD"; } acc_balance = 0.0; BankAccount obj = BankAccount(fname, lname, ssn, acc_num, acc_type, acc_balance); account[count] = obj; count++; cout << "want to create another account Y/N ?" << endl << endl; cin >> choice; } while (count < max_accts && choice == 'Y' || choice == 'y'); 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(const BankAccount account[], int num_accts, ofstream &dbfile) { dbfile << endl << endl; dbfile << "\t\tDatabase of Bank Accounts" << endl << endl; for (int index = 0; index < num_accts; index++) { dbfile << "First Name\t" << account[index].depositer.first_name << endl; dbfile << "Last Name\t" << account[index].depositer.last_name << endl; dbfile << "SSN\t\t" << account[index].depositer.social_security_num << endl; dbfile << "Account Num\t" << account[index].account_num << endl; dbfile << "Balance\t\t" << account[index].account_balance << endl; dbfile << "Type\t\t" << account[index].account_type << endl; dbfile << endl << 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 I -- Account Info" << 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(const BankAccount account[], int num_accts, int requested_account) { for (int index = 0; index < num_accts; index++) //if (acctnum_array[index] != 0) { if (account[index].account_num == 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(const BankAccount account[], int num_accts, ofstream &outfile) { cout << endl << endl; outfile << endl << endl; int requested_account; int index; cout << endl << "Enter the account number: "; //prompt for account number cin >> requested_account; index = findacct(account, 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 << "First Name: $" << account[index].depositer.first_name << endl; outfile << "Account Number: " << requested_account << endl; outfile << "Current Balance: $" << account[index].account_balance << 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(BankAccount account[], int num_accts, ofstream &outfile) { cout << endl << endl; outfile << endl << endl; int requested_account; int index; double amount_to_deposit; cout << endl << "Enter the account number: "; //prompt for account number cin >> requested_account; index = findacct(account, 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: $" << account[index].account_balance << endl; outfile << "Amount to Deposit: $" << amount_to_deposit << endl; account[index].account_balance += amount_to_deposit; //make the deposit outfile << "New Balance: $" << account[index].account_balance << endl; } } return; } void withdrawal(BankAccount account[], int num_accts, ofstream &outfile) { cout << endl << endl; outfile << endl << endl; int requested_account; int index; double amount_to_withdraw; cout << endl << "Enter the account number: "; //prompt for account number cin >> requested_account; index = findacct(account, num_accts, requested_account); if (index == -1) //invalid account { outfile << endl << "Transaction Requested: Withdraw" << endl; outfile << "Error: Account number " << requested_account << " does not exist" << endl; } else //valid account { cout << "Enter amount to withdraw: "; //prompt for amount to withdraw cin >> amount_to_withdraw; if (amount_to_withdraw <= 0.00) //invalid amount to deposit { outfile << endl << "Transaction Requested: Withdraw" << endl; outfile << "Account Number: " << requested_account << endl; outfile << "Error: " << amount_to_withdraw << " is an invalid amount" << endl; } else if (account[index].account_balance - amount_to_withdraw < 0) { outfile << endl << "Transaction Requested: Withdraw" << endl; outfile << "Account Number: " << requested_account << endl; outfile << "Old Balance: $" << account[index].account_balance << endl; outfile << "ERROR: Withdraw Amount is more than current balance : " << amount_to_withdraw << endl; } else //valid deposit { outfile << endl << "Transaction Requested: Withdraw" << endl; outfile << "Account Number: " << requested_account << endl; outfile << "Old Balance: $" << account[index].account_balance << endl; outfile << "Amount to Withdraw: $" << amount_to_withdraw << endl; account[index].account_balance -= amount_to_withdraw; //make the Withdraw outfile << "New Balance: $" << account[index].account_balance << endl; } } return; } int new_acct(BankAccount account[], int num_accts, ofstream &outfile) { cout << endl << endl; outfile << endl << endl; cout << "Transaction Requested: Create Account" << endl; outfile << "Transaction Requested: Create Account" << endl; int new_account_num; cout << "Enter Account Number" << endl << endl; cin >> new_account_num; if (new_account_num <= 0.00) { outfile << "ERROR: Invalid Account number" << new_account_num << endl << endl; return num_accts; } int index; string fname, lname, ssn, acc_type; int acc_num; double acc_balance; index = findacct(account, num_accts, new_account_num); if (index == -1) // account number doesnot exist so new account can be created with this number { cout << "Enter First Name" << endl << endl; cin >> fname; cout << "Enter Last Name" << endl << endl;; cin >> lname; cout << "Enter Social Security Number" << endl << endl;; cin >> ssn; cout << "Enter Account Number" << endl << endl;; cin >> acc_num; cout << "Select Account type" << endl << endl;; cout << "1. Checking" << endl; cout << "2. Savings" << endl; cout << "3. CD" << endl; char ch; cin >> ch; if (ch == '1') { acc_type = "checking"; } else if (ch == '2') { acc_type = "Savings"; } else if (ch == '3') { acc_type = "CD"; } acc_balance = 0.0; BankAccount obj = BankAccount(fname, lname, ssn, acc_num, acc_type, acc_balance); account[num_accts] = obj; outfile << endl << "New Account Created" << endl; outfile << "Account number " << account[num_accts].account_num << " Balance " << account[num_accts].account_balance << endl; num_accts += 1; } else // account number already exist { outfile << "ERRROR: Account Number " << new_account_num << " already exist" << endl << endl; } return num_accts; } int delete_acct(BankAccount account[], int num_accts, ofstream &outfile) { cout << endl << endl; outfile << endl << endl; cout << "Transaction Requested: Delete Account" << endl; outfile << "Transaction Requested: Delete Account" << endl; int account_num; cout << "Enter Account Number" << endl << endl; cin >> account_num; int index; index = findacct(account, num_accts, account_num); if (index == -1) // account number doesnot exist so new account can be created with this number { outfile << "ERROR: Account Number " << account_num << " Doesnot Exist " << endl << endl; } else // account number already exist { if (account[index].account_balance == 0){ for (int i = index + 1; i < num_accts; ++i) { account[i - 1].depositer.first_name = account[i].depositer.first_name; account[i - 1].depositer.last_name = account[i].depositer.last_name; account[i - 1].depositer.social_security_num = account[i].depositer.social_security_num; account[i - 1].account_balance = account[i].account_balance; account[i - 1].account_num = account[i].account_num; account[i - 1].account_type = account[i].account_type; } num_accts -= 1; cout << "\n Account has been deleted."; outfile<<<< "\n Account has been deleted."; } else { outfile << "ERROR: Account cannot be deleted Remaining balance is more than zero " << endl << endl; } } return num_accts; } int findacctssn(const BankAccount account[], int num_accts, string requested_account) { for (int index = 0; index < num_accts; index++) //if (acctnum_array[index] != 0) { if (account[index].depositer.social_security_num == requested_account) return index; } return -1; } void account_info(const BankAccount account[], int num_accts, ofstream &outfile) { cout << endl << endl; outfile << endl << endl; cout << "Transaction Requested: Acount Info" << endl; outfile << "Transaction Requested: Account Info" << endl; string requested_account; int index; cout << endl << "Enter the Social Security Number: "; //prompt for account number cin >> requested_account; index = findacctssn(account, num_accts, requested_account); if (index == -1) // account number doesnot exist so new account can be created with this number { outfile << "ERROR: Account with Social Security Number " << requested_account << " Doesnot Exist " << endl << endl; } else { outfile << "First Name\t" << account[index].depositer.first_name << endl; outfile << "Last Name\t" << account[index].depositer.last_name << endl; outfile << "SSN\t\t" << account[index].depositer.social_security_num << endl; outfile << "Account Num\t" << account[index].account_num << endl; outfile << "Balance\t\t" << account[index].account_balance << endl; outfile << "Type\t\t" << account[index].account_type << endl; outfile << endl << endl; } } // Function pause() void pause(void) { system("pause"); return; }