Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class BankAccount{
- string name;
- string accountNumber;
- int balance;
- public:
- BankAccount(){
- balance = 0;
- }
- BankAccount(string n, string s){
- name = s;
- accountNumber = n;
- balance = 0;
- }
- void setName(string s){
- name = s;
- }
- void setAccountNumber(string n){
- accountNumber = n;
- }
- void deposit(int n){
- if(n >= 0) balance += n;
- else cout << "Invalid operation";
- }
- string getName(){
- return name;
- }
- string getAccountNumber(){
- return accountNumber;
- }
- int getBalance(){
- return balance;
- }
- void withdraw(int amount){
- if(amount <= balance) balance -= amount;
- else cout << "Insufficient Balance";
- }
- void display(){
- cout << '\t' << "Account Number: " << getAccountNumber() << '\n';
- cout << '\t' << "Account Holder: " << getName() << '\n';
- cout << '\t' << "Balance: " << getBalance() << '\n';
- }
- };
- class Bank{
- BankAccount collection[100];
- int accountAmount;
- public:
- Bank(){
- accountAmount = 0;
- }
- void addAccount(string n, string name){
- collection[accountAmount].setAccountNumber(n);
- collection[accountAmount].setName(name);
- }
- void deposit(string num, int amount){
- int i = 0;
- while(collection[i].getName() != num){
- i++;
- }
- collection[i].deposit(amount);
- }
- void withdraw(string num, int amount){
- int i = 0;
- while(collection[i].getName() != num){
- i++;
- }
- collection[i].withdraw(amount);
- }
- void displayAllAccounts(){
- cout << "Registered Accounts:" << '\n';
- for(int i = 0; i < accountAmount; i++){
- collection[i].display();
- cout << '\n';
- }
- }
- void removeAccount(string num){
- bool found = false;
- for(int i = 0; i < accountAmount; i++){
- if(collection[i].getAccountNumber() == num){
- found = true;
- }
- if(found && i+1 < accountAmount){
- collection[i] = collection[i+1];
- }
- }
- accountAmount--;
- }
- };
- int main()
- {
- Bank bank;
- // Add new bank accounts
- bank.addAccount("12345", "John Doe");
- bank.addAccount("67890", "Jane Smith");
- // Perform transactions
- bank.deposit("12345", 1000.0);
- bank.deposit("67890", 500.0);
- // Display account details
- bank.displayAllAccounts();
- // Perform transactions
- bank.withdraw("12345", 200.0);
- bank.withdraw("67890", 500.0);
- // Display account details after transaction
- bank.displayAllAccounts();
- // Remove an account
- bank.removeAccount("67890");
- // Display account details after removal
- bank.displayAllAccounts();
- // Add more accounts
- bank.addAccount("24680", "Sarah Adams");
- bank.addAccount("13579", "Michael Lee");
- // Perform transactions on the newly added accounts
- bank.deposit("24680", 1500.0);
- bank.deposit("13579", 200.0);
- bank.withdraw("13579", 100.0);
- bank.withdraw("24680", 300.0);
- // Display all accounts
- bank.displayAllAccounts();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement