-
Notifications
You must be signed in to change notification settings - Fork 1
/
bankAccount.hpp
69 lines (56 loc) · 2.24 KB
/
bankAccount.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @Author: Izhar Shaikh <izhar>
* @Date: 2017-03-03T16:56:55-05:00
* @Email: izharits@gmail.com
* @Filename: bankAccount.hpp
* @Last modified by: izhar
* @Last modified time: 2017-03-06T04:15:28-05:00
* @License: MIT
*/
#ifndef __BANK_ACCOUNT__
#define __BANK_ACCOUNT__
#include <pthread.h>
#include <stdint.h>
// -- Typedefs --
typedef class bankAccount bankAccount_t;
typedef class bankAccountNode node_t;
typedef class bankAccountNode* poolHandle_t;
typedef class bankAccountPool bankAccountPool_t;
// -- Class --
class bankAccount
{
private:
int64_t number; // Account Number
int64_t balance; // Balance
pthread_mutexattr_t attr; // Attribute for mutex
pthread_mutex_t mutex; // Mutexcle to protect read/write access to the acc
bool is_initialized = false; // flag for init
public:
void init(); // Initialize
void destroy(); // Destroy
int64_t lock(); // Lock the access to mutex
int64_t trylock(); // Lock the access to mutex
int64_t unlock(); // releases the access to mutex
int64_t getAccountNumber(); // retrieves account number
int64_t getBalance(); // retrieves account balance
void setBalance(int64_t newBalance); // sets account balance
void setAccountNumber(int64_t accountNumber); // sets the account number
};
// -- Pool of bank accounts --
class bankAccountPool
{
private:
poolHandle_t handle;
void *poolMemory;
size_t poolSize;
int64_t totalAccounts;
public:
void initPool(int64_t NumberOfAccounts); // Initialized the pool
void deInitPool(); // Destroy the pool
poolHandle_t getPoolHandle(); // get the handle to the pool
int64_t getTotalAccounts(); // Total accounts in the pool
bankAccount_t* at(int64_t accountNumber); // retrieve bank account
void addAccount(int64_t accountNumber, int64_t balance); // Add new account to pool
void dbgPrintAccountPool(); // prints all the contents of account pool
};
#endif