📜  门| GATE-CS-2001 |问题 10(1)

📅  最后修改于: 2023-12-03 14:58:24.702000             🧑  作者: Mango

GATE-CS-2001 Problem 10

This is a problem from the computer science GATE (Graduate Aptitude Test in Engineering) exam conducted by IITs (Indian Institutes of Technology) and IISc (Indian Institute of Science) for admission to their post-graduate courses.

Problem Description

The problem statement is as follows:

Design a class that can be used by a bank to keep track of the customers' accounts. The class should allow opening new accounts, balance enquiry, deposit and withdrawal of money, closure of accounts. The class should also allow the customer to transfer amounts between accounts. Use C++ as the implementation language.
Solution

To solve this problem, we can design a class called Bank that contains all the necessary information and functions to manage the customers' accounts.

First, we can define a structure called Account that contains the information of a bank account such as account number, customer name, balance, etc.

struct Account {
    int accNo;
    string name;
    double balance;
    // other information
};

Then, we can define a class called Bank that contains a list of accounts and the required functions to manage them. The Bank class can have the following functions:

  • void addAccount(int accNo, string name, double balance): this function adds a new account to the list.
  • void balanceEnquiry(int accNo): this function displays the balance of a given account.
  • void deposit(int accNo, double amount): this function adds the specified amount to the balance of a given account.
  • void withdrawal(int accNo, double amount): this function subtracts the specified amount from the balance of a given account.
  • void transfer(int fromAccNo, int toAccNo, double amount): this function transfers the specified amount from one account to another.
  • void closeAccount(int accNo): this function closes a given account and removes it from the list.

Below is the implementation of the Bank class:

class Bank {
private:
    vector<Account> accounts;
public:
    void addAccount(int accNo, string name, double balance) {
        Account acc = {accNo, name, balance};
        accounts.push_back(acc);
    }
    void balanceEnquiry(int accNo) {
        for (int i = 0; i < accounts.size(); i++) {
            if (accounts[i].accNo == accNo) {
                cout << "Balance of account " << accNo << ": " << accounts[i].balance << endl;
                return;
            }
        }
        cout << "Account does not exist." << endl;
    }
    void deposit(int accNo, double amount) {
        for (int i = 0; i < accounts.size(); i++) {
            if (accounts[i].accNo == accNo) {
                accounts[i].balance += amount;
                cout << "Deposited " << amount << " to account " << accNo << endl;
                return;
            }
        }
        cout << "Account does not exist." << endl;
    }
    void withdrawal(int accNo, double amount) {
        for (int i = 0; i < accounts.size(); i++) {
            if (accounts[i].accNo == accNo) {
                if (accounts[i].balance < amount) {
                    cout << "Insufficient balance." << endl;
                    return;
                }
                accounts[i].balance -= amount;
                cout << "Withdrawn " << amount << " from account " << accNo << endl;
                return;
            }
        }
        cout << "Account does not exist." << endl;
    }
    void transfer(int fromAccNo, int toAccNo, double amount) {
        bool fromFound = false;
        bool toFound = false;
        int fromIndex = -1;
        int toIndex = -1;
        for (int i = 0; i < accounts.size(); i++) {
            if (accounts[i].accNo == fromAccNo) {
                fromFound = true;
                fromIndex = i;
            } else if (accounts[i].accNo == toAccNo) {
                toFound = true;
                toIndex = i;
            }
        }
        if (!fromFound) {
            cout << "Sender account does not exist." << endl;
            return;
        }
        if (!toFound) {
            cout << "Receiver account does not exist." << endl;
            return;
        }
        if (accounts[fromIndex].balance < amount) {
            cout << "Insufficient balance." << endl;
            return;
        }
        accounts[fromIndex].balance -= amount;
        accounts[toIndex].balance += amount;
        cout << "Transferred " << amount << " from account " << fromAccNo << " to account " << toAccNo << endl;
    }
    void closeAccount(int accNo) {
        for (int i = 0; i < accounts.size(); i++) {
            if (accounts[i].accNo == accNo) {
                accounts.erase(accounts.begin() + i);
                cout << "Account " << accNo << " is closed." << endl;
                return;
            }
        }
        cout << "Account does not exist." << endl;
    }
};

Now, we can create an object of Bank class and use its functions to manage the accounts.

int main() {
    Bank bank;
    bank.addAccount(1001, "John", 1000);
    bank.addAccount(1002, "Jane", 500);
    bank.balanceEnquiry(1001); // Balance of account 1001: 1000
    bank.deposit(1001, 500); // Deposited 500 to account 1001
    bank.balanceEnquiry(1001); // Balance of account 1001: 1500
    bank.withdrawal(1001, 300); // Withdrawn 300 from account 1001
    bank.balanceEnquiry(1001); // Balance of account 1001: 1200
    bank.transfer(1001, 1002, 500); // Transferred 500 from account 1001 to account 1002
    bank.balanceEnquiry(1001); // Balance of account 1001: 700
    bank.balanceEnquiry(1002); // Balance of account 1002: 1000
    bank.closeAccount(1002); // Account 1002 is closed.
    bank.balanceEnquiry(1002); // Account does not exist.
    return 0;
}
Conclusion

In this problem, we designed a class that can be used by a bank to manage the customers' accounts. We used C++ as the implementation language and provided the solution in the form of a Bank class that contains a list of accounts and the required functions to manage them. The functions included opening new accounts, balance enquiry, deposit and withdrawal of money, closure of accounts, and transfer of amounts between accounts.