-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWithdrawTransaction.cs
More file actions
48 lines (41 loc) · 987 Bytes
/
WithdrawTransaction.cs
File metadata and controls
48 lines (41 loc) · 987 Bytes
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
using System;
using BankingApp;
public class WithdrawTransaction : Transaction
{
private Account _account;
public WithdrawTransaction(Account account, decimal amount) : base(amount)
{
_account = account;
}
public override void Print()
{
base.Print();
Console.WriteLine($"Account Holder: {_account.Name}");
if (_amount > 0 && _success)
Console.WriteLine($"Amount Withdrawn: {_amount:C}");
}
public override void Execute()
{
base.Execute();
if (_amount > 0 && _account.Withdraw(_amount))
{
_success = true;
}
else
{
_success = false;
}
}
public override void Rollback()
{
if (_success)
{
_account.Deposit(_amount);
}
else
{
throw new InvalidOperationException("Cannot reverse a failed transaction.");
}
base.Rollback();
}
}