//THIS IS GOOD FOR USING STRUCTS AND ARRAYS
//**NOTE**: THIS IS NOT COPYRIGHTED MATERIAL AND IS FREE FOR
//DISTRIBUTION AND PERSONAL USE
#include
#include
#include
#include
using namespace std;
// Create bank account data type
struct acct { // bank account data
int num; // account number
string name; // owner of account
float balance;// balance in account
};
// Function Prototypes
int find(acct bank[ ], int numaccts, int goal);
void insert(acct bank[ ], int &numaccts, acct newacct);
void remove(acct bank[], int &numaccts, acct deleteacct);
void transfer(acct bank[], int &numaccts, float amount, acct account);
int main ()
//PURPOSE: simulate a small bank
//PRECONDITIONS: existing accounts in file account.txt
// in project folder
//POSTCONDITIONS: finds, inserts and deletes accounts
{
acct bank[1000]; // list of bank accounts
int numaccts; // total number of accounts
ifstream vault; // file with list of bank accounts
string command; // user request
int goal; // account number to find or delete
int loc; // subscript of account in bank list
acct account; // a bank account
float amount;
// read existing accounts from file
vault.open("accounts.txt");
numaccts=0;
vault >> bank[0].num >> bank[0].name >> bank[0].balance;
while (!vault.eof()) {
numaccts = numaccts + 1;
vault >> bank[numaccts].num >> bank[numaccts].name
>> bank[numaccts].balance;
}
// display dollar values with two digits to right of decimal point
cout << setiosflags(ios::fixed) << setprecision(2);
// get ready to process user commands
cout << "The bank is now open." ;
cout << endl << endl;
// loop to process user commands
cout << endl << "----------" << endl;
cout << "Enter a command (exit to stop): ";
cin >> command;
while (command != "exit") {
if (command == "find") { // FIND COMMAND
// get account to find
cout << "Enter account number: ";
cin >> goal;
// search for account
loc = find(bank, numaccts, goal);
// display account data or error message
if (loc >= 0) {
cout << "Account: " << bank[loc].num << "\t"
<< "Owner: " << bank[loc].name << "\t"
<< "Balance: " << bank[loc].balance << endl;
}
else {
cout << "Account " << goal <<" does not exist." << endl;
}
}
else if (command == "list") { // LIST COMMAND
cout << "List of accounts:" << endl;
for (loc=0; loc
<< "Owner: " << bank[loc].name << "\t"
<< "Balance: " << bank[loc].balance << endl;
}
}
else if (command == "insert") { // INSERT COMMAND
cout << "Enter new account data: " << endl;
cout << "Account number: ";
cin >> account.num;
cout << "Account name: ";
cin >> account.name;
cout << "Account balance: ";
cin >> account.balance;
insert(bank, numaccts, account);
}
else if(command=="remove"){ //REMOVE COMMAND
cout<<"Enter the account number to remove: "<
while(find(bank,numaccts,account.num) == -1){
cout<<"Invalid. Enter a valid account number: "<
}
remove(bank,numaccts,account);
}
else if(command=="transfer"){
cout<<"Enter first account number: "<
while(find(bank,numaccts,account.num) == -1){
cout<<"Invalid. Enter a valid account number: "<
}
cout<<"Enter the balance to transfer: "<
transfer(bank,numaccts,amount,account);
cout<<"Enter second account number: "<
while(find(bank,numaccts,account.num) == -1){
cout<<"Invalid. Enter a valid account number: "<
}
amount=-1*amount;
transfer(bank,numaccts,amount,account);
}
else { // INVALID COMMAND
cout << "Invalid command." << endl;
}
cout << endl << "----------" << endl;
cout << "Enter a command (exit to stop): ";
cin >> command;
}
// exit command from user
cout << endl << "----------" << endl;
cout << "The bank is now closed.";
cout << endl << endl;
return 0;
}
int find(acct bank[ ], int numaccts, int goal)
//PURPOSE: search for a goal account in the list of accounts
//PRECONDITIONS: 0 <= numaccts, numaccts is number of account in
// list of accounts (i.e., bank)
//POSTCONDITIONS: returns the subscript of goal account or -1 if
// the goal account is not found
{
int k; // loop variable
//search the the goal account in list of accounts
for (k=0; k
return k;
}
}
// didn't find goal account
return -1;
}
void insert(acct bank[ ], int &numaccts, acct newacct)
//PURPOSE: insert a new account into the account list
//PRECONDITIONS: 0 <= numaccts, numaccts is number of account
// in account list, newacct is a new account,
// account list is in order by account number
//POSTCONDITIONS: account list modified to add new account,
// order of account list is maintained
{
int k,j; // loop variables
// find place to put new account to maintain order of list
k=0;
while ((bank[k].num < newacct.num) && (k < numaccts)) {
k = k + 1;
}
// add new account to list
if (k == numaccts) { // goes at end of list
bank[numaccts] = newacct;
numaccts = numaccts + 1;
}
else { // goes in middle of list
// make room to add new account
for (j=numaccts; j>k; j--) {
bank[j] = bank[j-1];
}
// add new account to vacated spot in list
bank[k] = newacct;
numaccts = numaccts + 1;
}
}
void remove(acct bank[], int &numaccts, acct deleteacct)
//PURPOSE: remove an account from the account list
//PRECONDITION: 0<=numaccts, numaccts is # of accounts in list
// account list is in order by account number
//POSTCONDITION: returns list with one less account, which is the one
// that the user chose to remove
{
int x,y; //loop variables
y=find(bank, numaccts, deleteacct.num);
for(x=y; y<=numaccts; y++){
bank[y]=bank[y+1];
}
numaccts=numaccts-1;
}
void transfer(acct bank[], int &numaccts, float amount, acct account)
{
int i;
i=find(bank,numaccts,account.num);
bank[i].balance=bank[i].balance - amount;
}