/
Transactioner.cs
62 lines (51 loc) · 1.54 KB
/
Transactioner.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
using ALM.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ALM.Web
{
public class Transactioner
{
private readonly BankRepository _repository;
public Transactioner(BankRepository repository)
{
_repository = repository;
}
public Transactioner()
{
}
public decimal Deposit(Account account, decimal amount)
{
account.Credit(amount);
return account.Balance;
}
public decimal Deposit(int accountId, decimal amount)
{
var account = _repository.GetAccount(accountId);
if (account == null)
{
throw new AccountNotFoundException(accountId);
}
return Deposit(account, amount);
}
public decimal Withdraw(Account account, decimal amount)
{
if (account.Balance < amount)
{
throw new InvalidTransactionException($"Cant Withdraw {amount} from account #{account.AccountId}. Balance is only {account.Balance}.");
}
account.Debit(amount);
return account.Balance;
}
public decimal Withdraw(int accountId, decimal amount)
{
var account = _repository.GetAccount(accountId);
if (account == null)
{
throw new AccountNotFoundException(accountId);
}
return Withdraw(account, amount);
}
}
}