/ Files / Bank.cpp
Bank.cpp
 1  #include <iostream>
 2  #include <vector>
 3  
 4  using namespace std;
 5  
 6  struct Transaction
 7  {
 8      int difference;
 9      string type;
10  };
11  
12  class Account
13  {
14      private:
15      int balance;
16      vector<Transaction>Transactions;
17  
18      public:
19      Account(int initBalance)
20      {
21          if (initBalance >= 0)
22          {
23              balance = initBalance;
24          } else
25          {
26              balance = 0;
27              cout << "Error in initial balance, setting to 0" << endl;
28          }
29      }
30  
31      void credit(int amount)
32      {
33          balance += amount;
34  
35          Transaction transaction;
36          transaction.difference = amount;
37          transaction.type = "Credit";
38          Transactions.push_back(transaction);
39      }
40      void debit(int amount)
41      {
42          if (amount > balance)
43          {
44              cout << "Debit amount exceeded account balance." << endl;
45          }
46          else
47          {
48              balance -= amount;
49  
50              Transaction transaction;
51              transaction.difference = amount;
52              transaction.type = "Debit";
53              Transactions.push_back(transaction);
54          }
55      }
56      int getBalance () const
57      {
58          return balance;
59      }
60  
61      void listTransactions() const
62      {
63          cout << "Transactions:" << endl;
64          for (int i = 0; i < Transactions.size(); i++)
65          {
66              cout << "Transaction #" << i + 1 << endl;
67              cout << "Difference: " << Transactions[i].difference << endl;
68              cout << "Type: " << Transactions[i].type << endl;
69          }
70      }
71  
72  };
73  
74  int main()
75  {
76      Account Account1(500);
77  
78      Account1.credit(200);
79      Account1.debit(100);
80  
81      cout << Account1.getBalance() << endl;
82  
83      Account1.listTransactions();
84  
85      return 0;
86  }