-
Notifications
You must be signed in to change notification settings - Fork 0
/
002-code.cs
101 lines (84 loc) · 2.62 KB
/
002-code.cs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
namespace AbstractedTransaction
{
public class TransferTransaction : Transaction, TransactonInterface
{
Account _fromAccount;
Account _toAccount;
DepositTransaction _deposit;
WithdrawTransaction _withdraw;
public override bool Success
{
get
{
if (_deposit.Success && _withdraw.Success)
{
return true;
}
return false;
}
}
public TransferTransaction(Account fromAccount, Account toAccount, decimal amount) : base(amount)
{
_fromAccount = fromAccount;
_toAccount = toAccount;
DepositTransaction deposit = new DepositTransaction(toAccount, amount);
_deposit = deposit;
WithdrawTransaction withdraw = new WithdrawTransaction(fromAccount, amount);
_withdraw = withdraw;
}
public override void Print()
{
if (Success)
{
Console.WriteLine("Transferred $" + _amount + " from " + _fromAccount.Name + " to " + _toAccount.Name);
_withdraw.Print();
_deposit.Print();
}
else if (Reversed)
Console.WriteLine("transaction rolled back..");
else
Console.WriteLine("Transaction unsuccessfull!");
}
public override void Execute()
{
base.Execute();
if (Executed)
{
throw new InvalidOperationException("The transfer transaction has been already attempted");
}
if (_amount > _fromAccount.Balance)
{
throw new InvalidOperationException("Insuffiecient funds!");
}
_withdraw.Execute();
if (_withdraw.Success)
{
_deposit.Execute();
if (!_deposit.Success)
{
_withdraw.Rollback();
}
}
base.setExecute();
}
public override void Rollback()
{
base.Rollback();
if (!Success)
{
throw new InvalidOperationException("Transaction wasn't success..");
}
else if (Reversed)
{
throw new InvalidOperationException("Transaction already rolled back. .");
}
else
{
_deposit.Rollback();
_withdraw.Rollback();
base.setReverese();
}
}
}
}