Advertisement
AshTurner67

Online 1 B

Oct 21st, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class BankAccount{
  7.     string name;
  8.     string accountNumber;
  9.     int balance;
  10.     public:
  11.     BankAccount(){
  12.         balance = 0;
  13.     }
  14.     BankAccount(string n, string s){
  15.         name = s;
  16.         accountNumber = n;
  17.         balance = 0;
  18.     }
  19.     void setName(string s){
  20.         name = s;
  21.     }
  22.     void setAccountNumber(string n){
  23.         accountNumber = n;
  24.     }
  25.     void deposit(int n){
  26.         if(n >= 0) balance += n;
  27.         else cout << "Invalid operation";
  28.     }
  29.     string getName(){
  30.         return name;
  31.     }
  32.     string getAccountNumber(){
  33.         return accountNumber;
  34.     }
  35.     int getBalance(){
  36.         return balance;
  37.     }
  38.     void withdraw(int amount){
  39.         if(amount <= balance) balance -= amount;
  40.         else cout << "Insufficient Balance";
  41.     }
  42.     void display(){
  43.         cout << '\t' << "Account Number: " << getAccountNumber() << '\n';
  44.         cout << '\t' << "Account Holder: " << getName() << '\n';
  45.         cout << '\t' << "Balance: " << getBalance() << '\n';
  46.     }
  47.  
  48. };
  49.  
  50. class Bank{
  51.     BankAccount collection[100];
  52.     int accountAmount;
  53.     public:
  54.     Bank(){
  55.         accountAmount = 0;
  56.     }
  57.     void addAccount(string n, string name){
  58.         collection[accountAmount].setAccountNumber(n);
  59.         collection[accountAmount].setName(name);
  60.     }
  61.     void deposit(string num, int amount){
  62.         int i = 0;
  63.         while(collection[i].getName() != num){
  64.             i++;
  65.         }
  66.         collection[i].deposit(amount);
  67.     }
  68.     void withdraw(string num, int amount){
  69.         int i = 0;
  70.         while(collection[i].getName() != num){
  71.             i++;
  72.         }
  73.         collection[i].withdraw(amount);
  74.     }
  75.     void displayAllAccounts(){
  76.         cout << "Registered Accounts:" << '\n';
  77.         for(int i = 0; i < accountAmount; i++){
  78.             collection[i].display();
  79.             cout << '\n';
  80.         }
  81.     }
  82.     void removeAccount(string num){
  83.         bool found = false;
  84.         for(int i = 0; i < accountAmount; i++){
  85.             if(collection[i].getAccountNumber() == num){
  86.                 found = true;
  87.             }
  88.             if(found && i+1 < accountAmount){
  89.                 collection[i] = collection[i+1];
  90.             }
  91.         }
  92.         accountAmount--;
  93.  
  94.     }
  95.  
  96.  
  97. };
  98.  
  99. int main()
  100. {
  101.     Bank bank;
  102.     // Add new bank accounts
  103.     bank.addAccount("12345", "John Doe");
  104.     bank.addAccount("67890", "Jane Smith");
  105.     // Perform transactions
  106.     bank.deposit("12345", 1000.0);
  107.     bank.deposit("67890", 500.0);
  108.     // Display account details
  109.     bank.displayAllAccounts();
  110.     // Perform transactions
  111.     bank.withdraw("12345", 200.0);
  112.     bank.withdraw("67890", 500.0);
  113.     // Display account details after transaction
  114.     bank.displayAllAccounts();
  115.     // Remove an account
  116.     bank.removeAccount("67890");
  117.     // Display account details after removal
  118.     bank.displayAllAccounts();
  119.     // Add more accounts
  120.     bank.addAccount("24680", "Sarah Adams");
  121.     bank.addAccount("13579", "Michael Lee");
  122.     // Perform transactions on the newly added accounts
  123.     bank.deposit("24680", 1500.0);
  124.     bank.deposit("13579", 200.0);
  125.     bank.withdraw("13579", 100.0);
  126.     bank.withdraw("24680", 300.0);
  127.     // Display all accounts
  128.     bank.displayAllAccounts();
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement