-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransferTransaction.cs
More file actions
65 lines (58 loc) · 1.7 KB
/
TransferTransaction.cs
File metadata and controls
65 lines (58 loc) · 1.7 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
using System;
using BankingApp;
namespace BankSystem
{
public class TransferTransaction : Transaction
{
private Account _fromAccount;
private Account _toAccount;
private DepositTransaction _deposit;
private WithdrawTransaction _withdraw;
public TransferTransaction(Account fromAccount, Account toAccount, decimal amount) : base(amount)
{
_fromAccount = fromAccount;
_toAccount = toAccount;
_deposit = new DepositTransaction(toAccount, amount);
_withdraw = new WithdrawTransaction(fromAccount, amount);
}
public override void Print()
{
base.Print();
if (_amount > 0 && _success)
Console.WriteLine($"Transferred {_amount:C} from {_fromAccount.Name}'s account to {_toAccount.Name}'s account");
}
public override void Execute()
{
base.Execute();
if (_amount > 0)
{
_withdraw.Execute();
if (_withdraw.Success)
{
_deposit.Execute();
if (_deposit.Success)
{
_success = true;
}
}
}
else
{
_success = false;
}
}
public override void Rollback()
{
if (_success)
{
_deposit.Rollback();
_withdraw.Rollback();
}
else
{
throw new InvalidOperationException("Cannot reverse a failed transaction.");
}
base.Rollback();
}
}
}