-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithdraw_transaction.cpp
More file actions
72 lines (57 loc) · 1.66 KB
/
withdraw_transaction.cpp
File metadata and controls
72 lines (57 loc) · 1.66 KB
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
70
71
72
#include <iostream>
#include "account.h"
#include "withdraw_transaction.h"
using namespace std;
namespace banking_system
{
WithdrawTransaction::WithdrawTransaction(Account& account, double amount) : _account(account), _amount(amount)
{
while (amount <= 0 || amount >= account.Balance())
{
cout << "Invalid Amount!\n";
}
_amount = amount;
}
bool WithdrawTransaction::Executed()
{
return _executed;
}
bool WithdrawTransaction::Success()
{
return _success;
}
bool WithdrawTransaction::Reversed()
{
return _reversed;
}
void WithdrawTransaction::Execute()
{
if (_executed && _success)
throw invalid_argument("Already Executed\n");
_executed = true;
_account.Withdraw(_amount);
_success = true;
}
void WithdrawTransaction::Rollback()
{
if (!_executed && !_success)
throw invalid_argument("No execution to rollback\n");
if (_reversed)
throw invalid_argument("Already rolled\n");
_account.Deposit(_amount);
_reversed = true;
}
void WithdrawTransaction::Print()
{
cout << "For the account: " << _account.Name() << endl;
if (!_executed)
cout << "Execution failed\n";
else if (!_success)
cout << "Withdrawal failed\n";
else if (_success && _reversed)
cout << "Withdrawal reversed\n";
else if (_success)
cout << "Successful withdrawal\n";
_account.Print();
}
}