In: Computer Science
assigsment-Write a program to simulate a bank transaction. There are two bank accounts: checking and savings. First, ask for the initial balances of the bank accounts; reject negative balances. Then ask for the transactions; options are deposit, withdrawal, and transfer. Then ask for the account; options are checking and savings. Then ask for the amount; reject transactions that overdraw an account. At the end, print the balances of both accounts. in c++
work done- output and need help with the if statement for withdraw,deposit,transfer
CODE -
#include<iostream>
using namespace std;
main()
{
// VARIABLE DECLARATION
float amount;
float balance[2];
int choice, acc_type;
// ASKING USER FOR INITIAL BALANCE OF CHECKING ACCOUNT
cout << "Enter the initial balance of checking account: ";
cin >> balance[0];
// LOOP TO VALIDATE THE BALANCE AND MAKE SURE USER ENTERS A VALID INITIAL BALANCE i.e. NON-NEGATIVE BALANCE
while(balance[0]<0)
{
cout << "\nERROR - Balance cannot be negative! Try again." << endl;
cout << "\nEnter the initial balance of checking account: ";
cin >> balance[0];
}
// ASKING USER FOR INITIAL BALANCE OF SAVINGS ACCOUNT
cout << "Enter the initial balance of savings account: ";
cin >> balance[1];
// LOOP TO VALIDATE THE BALANCE AND MAKE SURE USER ENTERS A VALID INITIAL BALANCE i.e. NON-NEGATIVE BALANCE
while(balance[1]<0)
{
cout << "\nERROR - Balance cannot be negative! Try again." << endl;
cout << "\nEnter the initial balance of savings account: ";
cin >> balance[1];
}
// ASKING USER FOR THE TYPE OF TRANSACTION
cout << "\nChoose the type of Transaction:" << endl;
cout << "1. Deposit" << endl;
cout << "2. Withdrawal" << endl;
cout << "3. Transfer" << endl;
cout << "Enter your choice: ";
cin >> choice;
// LOOP TO VALIDATE THE USER'S CHOICE
while(choice!=1 && choice!=2 && choice!=3)
{
cout << "\nInvalid Choice! Try again." << endl;
cout << "\nEnter your choice: ";
cin >> choice;
}
// ASKING USER FOR THE TYPE OF ACCOUNT ON WHICH HE/SHE WANTS TO PERFORM THE TRANSACTION
cout << "\nChoose the account type for which you want to perform the transaction:" << endl;
cout << "1. Checking" << endl;
cout << "2. Savings" << endl;
cout << "Enter your choice: ";
cin >> acc_type;
// LOOP TO VALIDATE THE USER'S CHOICE
while(acc_type!=1 && acc_type!=2)
{
cout << "\nInvalid Choice! Try again." << endl;
cout << "\nEnter your choice: ";
cin >> acc_type;
}
// ASKING USER FOR THE AMOUNT OF THE TRANSACTION
cout << "\nEnter the amount: ";
cin >> amount;
// LOOP TO VALIDATE THE TRANSACTION AMOUNT ENTERED BY USER
while(amount<=0)
{
cout << "\nInvalid Amount! Amount must be greater than 0. Try again." << endl;
cout << "\nEnter the amount: ";
cin >> amount;
}
if(choice == 1)
{
// ADDING THE AMOUNT TO THE USER'S CHOOSEN ACCOUNT IF USER CHOOSES TO DEPOSIT
balance[acc_type-1] += amount;
cout << "\nTransaction Successful!" << endl;
}
else if(choice == 2)
{
// SUBTRACTING THE AMOUNT FROM THE USER'S CHOOSEN ACCOUNT IF USER CHOOSES TO WITHDRAW AND IF THERE ARE SUFFICIENT BALANCE IN HIS/HER ACCOUNT
if(balance[acc_type-1] >= amount)
{
balance[acc_type-1] -= amount;
cout << "\nTransaction Successful!" << endl;
}
// DISPLAYING TRANSACTION REJECTION MESSAGE IF THERE ARE INSUFFICIENT FUNDS IN THE USER'S CHOOSEN ACCOUNT.
else
cout << "\nInsufficient Funds in your account! Transaction Rejected.\n" << endl;
}
else
{
// SUBTRACTING THE AMOUNT FROM USER'S CHOOSEN ACCOUNT IF USER CHOOSES TO TRANSFER
// AND IF THERE ARE SUFFICIENT BALANCE IN HIS/HER ACCOUNT AND ADDING SAME AMOUNT TO HIS/HER ANOTHER ACCOUNT
if(balance[acc_type-1] >= amount)
{
balance[acc_type-1] -= amount;
balance[1 - (acc_type-1)] += amount;
cout << "\nTransaction Successful!" << endl;
}
// DISPLAYING TRANSACTION REJECTION MESSAGE IF THERE ARE INSUFFICIENT FUNDS IN THE USER'S CHOOSEN ACCOUNT.
else
cout << "\nInsufficient Funds in your account! Transaction Rejected.\n" << endl;
}
// DISPLAYING THE BALANCES OF BOTH THE ACCOUNTS
cout << "\nChecking Account Balance: " << balance[0] << endl;
cout << "Savings Account Balance: " << balance[1] << endl;
}
SCREENSHOTS -
CODE -
OUTPUT -
SAMPLE RUN 1 -
SAMPLE RUN 2 -
SAMPLE RUN 3 -
SAMPLE RUN 4 -
If you have any doubt regarding the solution, then do
comment.
Do upvote.