Question

In: Computer Science

assigsment-Write a program to simulate a bank transaction. There are two bank accounts: checking and savings....

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

Solutions

Expert Solution

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.


Related Solutions

Write a program to simulate a bank transaction. There are two bank accounts: checking and savings....
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 if and else statments only in c++
Write a Java program to simulate the rolling of two dice. The application should use an...
Write a Java program to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12. Your application should roll the dice 36,000,000 times. Store the results of each roll...
In Java, Write a Java program to simulate an ecosystem containing two types of creatures, bears...
In Java, Write a Java program to simulate an ecosystem containing two types of creatures, bears and fish. The ecosystem consists of a river, which is modeled as a relatively large array. Each cell of the array should contain an Animal object, which can be a Bear object, a Fish object, or null. In each time step, based on a random process, each animal either attempts to move into an adjacent array cell or stay where it is. If two...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
Write a program to simulate the Distributed Mutual Exclusion in ‘C’.
Write a program to simulate the Distributed Mutual Exclusion in ‘C’.
Banks offer various types of accounts, such as savings, checking, certificate of deposits, and money market,...
Banks offer various types of accounts, such as savings, checking, certificate of deposits, and money market, to attract customers as well as meet their specific needs. Two of the most commonly used accounts are savings and checking. Each of these accounts has various options. For example, you may have a savings account that requires no minimum balance but has a lower interest rate. Similarly, you may have a checking account that limits the number of checks you may write. Another...
Banks offer various types of accounts, such as savings, checking, certificate of deposits, and money market,...
Banks offer various types of accounts, such as savings, checking, certificate of deposits, and money market, to attract customers as well as meet their specific needs. Two of the most commonly used accounts are savings and checking. Each of these accounts has various options. For example, you may have a savings account that requires no minimum balance but has a lower interest rate. Similarly, you may have a checking account that limits the number of checks you may write. Another...
Problem Statement: Banks offer various types of accounts, such as savings, checking, certificate of deposits, and...
Problem Statement: Banks offer various types of accounts, such as savings, checking, certificate of deposits, and money market, to attract customers as well as meet with their specific needs. Two of the most commonly used accounts are savings and checking. Each of these accounts has various options. For example, you may have a savings account that requires no minimum balance but has a lower interest rate. Similarly, you may have a checking account that limits the number of checks you...
Banks offer various types of accounts, such as savings, checking, certificate of deposits, and money market,...
Banks offer various types of accounts, such as savings, checking, certificate of deposits, and money market, to attract customers as well as meet with their specific needs. Two of the most commonly used accounts are savings and checking. Each of these accounts has various options. For example, you may have a savings account that requires no minimum balance but has a lower interest rate. Similarly, you may have a checking account that limits the number of checks you may write....
Discuss checking and savings accounts. How do they work? How are they similar? How do they...
Discuss checking and savings accounts. How do they work? How are they similar? How do they differ? What are CDs? What are advantages of an ATM?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT