Code:
/*
Assignment 5
Jessie Brown
COP 2334
11.10.06
Project was created in Borland IDE. This project demonstrates the use of arrays,
functions, sorting, and searching.
*/
#include <iostream>
using namespace std;
//Prototypes -- function declarations.
//-----------------------------------
void Menu_Selection();
void enter_accounts (int[],double[],int,int& total);
void print_all_accounts(int[],double[],int& total);
void total_bank_funds(double[], int&);
void DepositFunction(int,int[],double[], int& total);
void WithdrawFunction(int,int[],double[], int& total);
void DeleteAccount(int,int[],double [],int& total);
int find_account(int[],int&,int);
void sort_accounts (int [], double[], int);
void main()
{
Menu_Selection();
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "\nPress <ENTER> to continue.\n";
fflush(stdin);
cin.get();
}
/*------------------------------------------------------------------------------
This section of code is the initial screen after employee signs in
to system. From here said employee can choose what she must
do with each customer being assisted.
------------------------------------------------------------------------------*/
void Menu_Selection()
{
const int NUMBER = 10;
int account[NUMBER], total=0;
double balance[NUMBER];
int option;
do
{
cout << endl
<< "1. Add a distinct account number: \n \n"
<< "2. Remove an existing account number: \n \n"
<< "3. Deposit Funds into distinct account: \n \n"
<< "4. Withdraw Funds from distinct account: \n \n"
<< "5. View all Customer accounts and Balance: \n \n"
<< "6. Total Funds available from All accounts: \n \n"
<< "7. Exit program: \n \n";
cin >> option;
cout << endl;
switch (option)
{
case 1:
enter_accounts (account,balance,NUMBER,total);
cout << endl;
break;
case 2:
DeleteAccount(NUMBER,account,balance,total);
//code to display deletion of an existing account.
break;
case 3:
DepositFunction(NUMBER,account,balance,total);
//code to display deposit entry.
break;
case 4:
WithdrawFunction(NUMBER,account,balance,total);
//code to display withdrawel deduction.
break;
case 5:
print_all_accounts(account,balance,total);
cout << endl;
break;
case 6:
total_bank_funds(balance, total);
//code to display grand total of all available accounts.
break;
case 7:
cout << "End of Program.\n";
break;
default:
cout << "Not a valid option. Please choose a valid option. \n\n";
}
}while (option != 7);
}
/*------------------------------------------------------------------------------
This function allows a user to add an account with its beginning balance.
accounts are to be sorted and no to account numbers are to be the same
------------------------------------------------------------------------------*/
void enter_accounts (int account[],double balance[],const int NUMBER, int& total)
{
if(total >= NUMBER)
{
cout <<"Maximum account capacity has been reached.\n"
<<"Please delete an account to continue\n";
}
else if(total < NUMBER)
{
cout <<"Please enter new account number.\n";
cin >> account[total];
for (int i =0; i < total; i++)
{
do
{
if (account[total]==account[i])
{
cout << "Account number must be original.\n"
<< "Please enter a new account number.\n";
cin >> account[total];
}
}while (account[total] == account[i]);
}
cout << "Please enter opening balance ($10.00 minimum)\n";
cin >> balance[total];
do
{
if (balance[total] < 10)
{
cout << "Minimum deposit must be $10.00\n";
cin >> balance[total];
}
} while (balance[total] < 10);
sort_accounts (account,balance,total);
total++;
}
}
/*------------------------------------------------------------------------------
This function prints all acounts listed in the "bank array" as well as thier
corresponding balances.
------------------------------------------------------------------------------*/
void print_all_accounts(int account[],double balance[],int& total)
{
for( int i = 0; i < total ; i++ )
{
cout << "Account number " << account[i];
cout << " Account balance is $" << balance[i];
cout << endl;
}
}
/*------------------------------------------------------------------------------
This function prints the total balance of all funds added together.
------------------------------------------------------------------------------*/
void total_bank_funds(double balance[], int& total)
{
double funds = 0;
for (int index = 0; index < total; index++)
funds = funds + balance[index];
if (funds > 0)
{
cout << "The total investments funds available for this bank is $" << funds;
cout << endl;
}
else if (funds <= 0)
{
cout << "This bank needs a new CEO!\n";
cout << endl;
}
}
/*------------------------------------------------------------------------------
This function searches an account number then adds a deposit to this account
------------------------------------------------------------------------------*/
void DepositFunction(int NUMBER, int account[],double balance[], int& total)
{
double deposit;
int target, result;
cout << "Enter a number to search for: ";
cin >> target;
cout << "Please enter the amount you wish to deposit\n";
cin >> deposit;
result = find_account(account, total, target);
if (result == -1)
{
cout << target << " is not on the list.\n";
}
else
balance[result] = balance[result]+deposit;
cout << "Your new balance for " << account[result]
<< " is " << balance[result];
}
/*------------------------------------------------------------------------------
This function finds an account number called and returns the target array or
location of this particular account variable to functions for deposit to account,
withdraw from account and delete account.
------------------------------------------------------------------------------*/
int find_account(int account[],int& total,int target)
{
int index = 0;
bool found = false;
while ((!found) && (index < total))
if (target == account[index])
found = true;
else
index++;
if (found)
return index;
else
return -1;
}
/*------------------------------------------------------------------------------
This function makes a withdrawal from a specific account, if the account will go
into the red withdrawal will not be made.
------------------------------------------------------------------------------*/
void WithdrawFunction(int NUMBER, int account[],double balance[], int& total)
{
double withdrawal;
int target, result;
cout << "Enter a number to search for: ";
cin >> target;
cout << "Please enter the amount you wish to withdraw.\n";
cin >> withdrawal;
result = find_account(account, total, target);
if (result == -1)
{
cout << target << " is not on the list.\n";
}
else
if (withdrawal > balance[result])
{
cout << "You do not have enough funds in this account to make\n"
<< "a withdrawal in this amount!\n";
}
else
{
balance[result] = balance[result]-withdrawal;
cout << "Your new balance for " << account[result]
<< " is $" << balance[result];
}
}
/*------------------------------------------------------------------------------
This Function removes a specific account and then moves all accounts up!
------------------------------------------------------------------------------*/
void DeleteAccount(int NUMBER, int account[],double balance[],int& total)
{
int target, result;
cout << "Enter a number to search for: ";
cin >> target;
result = find_account(account, total, target);
if (result == -1)
{
cout << target << " is not on the list.\n";
}
else if(result >-1)
{
while(result < total)
{
account[result] = account[result+1];
balance[result] = balance [result+1];
result++;
}
}
total--;
}
/*------------------------------------------------------------------------------
This function sorts the accounts, it is done each time a new account it added.
Because when an account is deleted and remaining accounts are only moved up,
there is no need to sort them at the time of deletion
------------------------------------------------------------------------------*/
void sort_accounts (int account[],double balance[],int total )
{
int i,hold;
double hold2;
for (int pass =0; pass <= total; pass++)
for (i=0; i==total-1;i++)
if (account[i] > account[i+1])
{
hold =account[i];
hold2 = balance[i];
account[i]=account[i+1];
balance[i] = balance[i+1];
account[i+1] = hold;
balance[i+1] = hold2;
}
}