I'm currently working on a banking system project and have created 3 objects representing 3 different customers. I've created a menu for the user to navigate various options on various accounts for the customers. I have way to much code in my int main() and am wondering how I can use a loop to create the customers and their bank account dynamically. My code for one customer is as follows:
if (input == 1111)
{
cout << "Please enter PIN number \t \t (It's 1234)" << endl;
cin >> pin;
cout << endl;
if (pin == 1234)
{
cout << "Name: " << Ross.getCustomerName() << endl << "ID: " << Ross.getCustomerID() << endl << "Number of accounts: " << Ross.getNumberOfAccounts() << endl;
cout << "------------------------------------" << endl;
cout << "The balance of account #1 is: " << Ross.accounts[0].getAccountBalance();
cout << "\t Available Funds: " << Ross.accounts[0].getAvailableBalance() << endl;
cout << "The balance of account #2 is: " << Ross.accounts[1].getAccountBalance();
cout << "\t Available Funds: " << Ross.accounts[1].getAvailableBalance() << endl;
cout << "The balance of account #3 is: " << Ross.accounts[2].getAccountBalance();
cout << "\t Available Funds: " << Ross.accounts[2].getAvailableBalance() << endl;
cout << "Please select an account: ";
cin >> input;
// Entering Ross #1 account to complete transactions
if (input == 1)
{
cout << endl << "Please enter 1, 2, 3, 4 or 5:" << endl;
cout << "1 - Withdrawl" << endl << "2 - Deposit" << endl << "3 - Loan" << endl << "4 - Overdraft" << endl << "5 - Interest" << endl;
cin >> input;
// Making withdrawl from Ross #1 account
if (input == 1)
{
// makes the withdrawl
}
// Making deposit to Ross #1 account
elseif (input == 2)
{
// makes a deposit
}
// Applying for a loan on Ross #1 account
elseif (input == 3)
{
// applying for the loan
}
// Applying for an overdraft on Ross #1 account
elseif (input == 4)
{
// Applying for an overdraft
}
// Calculating the interest rate
elseif (input == 5)
{
// Calculates the interest
}
}
So having this for multiple accounts for multiple customers in the main is inefficient. I know using loops is the way to fix this but i'm unsure how to assign the balance values etc to each customer when creating them.
Ok so when i'm trying to separate them into functions to display the balances, I have 3 customers. Is there anyway to pass through each customers balances to the function or do i need to create 3 different functions to display each customer?
For instance the above example is displaying one customer but what if i wanted to display <class name> Michael and transact on his accounts?
For instance the above example is displaying one customer but what if i wanted to display <class name> Michael and transact on his accounts?
You just pass Michael to the function like so: display_info( Michael );. It would be better to rename the parameter Ross to something like customer, I just named it Ross because I assumed you were working on one customer.
I do have a slight problem when I am doing this for other functions such as withdrawing from the account
1 2 3 4 5 6 7 8 9 10
void withdraw(Customers Cust, int AccNo)
{
cout << "How much would you like to withdrawl?: ";
cin >> input; cout << endl;
accountBalance = Cust.accounts[AccNo].getAccountBalance();
availableBalance = Cust.accounts[AccNo].getAvailableBalance();
Cust.accounts[AccNo].setAccountBalance(Calculate.makeWithdrawl(accountBalance, input));
Cust.accounts[AccNo].setAvailableBalance(Calculate.makeWithdrawlOverdraft(availableBalance, input));
cout << "New balance: " << Calculate.makeWithdrawl(accountBalance, input) << endl;
}
I am required to use getters and setters to store the balance of the accounts and when this function is called it does correctly display the balance after the withdrawl however it does not store the result after the program loops back to the start.
The makeWithdrawl(), setAccountBalance() and getAccountBalance() functions are:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int Calculations::makeWithdrawl(int accountBalance, int Input)
{
int sum = accountBalance - Input;
return sum;
}
void BankAccounts::setAccountBalance(int x)
{
balance = x;
}
int BankAccounts::getAccountBalance()
{
return balance;
}
the available ones are pretty much the same except for the 'available' balance. Any ideas what i'm getting wrong?
void withdraw(Customers Cust, int AccNo)
Your Customer object is passed by value, so any changes made within the function won't affect the original variable.
You'll need to pass by reference to modify the original variable. void withdraw(Customers& Cust, int AccNo)