-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBankAccount.cs
73 lines (64 loc) · 2.18 KB
/
BankAccount.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
using System;
using System.Collections.Generic;
using System.Text;
namespace myApp
{
public class BankAccount
{
public string Number { get; set; }
public decimal Balance
{
get
{
decimal balance = 0;
foreach (var item in allTransaction)
{
balance += item.Amount;
}
return balance;
}
}
public string Owner { get; set; }
private static int accountNumberSeed = 1234567890;
private List<Transaction> allTransaction = new List<Transaction>();
public void MakeDeposit(decimal amount, DateTime date, string note)
{
if (amount <= 0)
{
throw new ArgumentOutOfRangeException(nameof(amount), "Amount of deposit must be greater than positive");
}
var deposit = new Transaction(amount, date, note);
allTransaction.Add(deposit);
}
public void MakeWithdrawal(decimal amount, DateTime date, string note)
{
if (amount <= 0)
{
throw new ArgumentOutOfRangeException(nameof(amount), "Amount of withdrawal must be positive");
}
if (Balance - amount < 0)
{
throw new InvalidOperationException("The available balanc in your account is not adequate for this transaction");
}
var withdrawal = new Transaction(-amount,date, note);
allTransaction.Add(withdrawal);
}
public BankAccount(string name, decimal initialBalance)
{
this.Owner = name;
this.Number = accountNumberSeed.ToString();
MakeDeposit(initialBalance,DateTime.Now,"intial balance");
accountNumberSeed++;
}
public string GetAccountHistory()
{
var report = new StringBuilder();
report.AppendLine("Date\tAmount\tNote");
foreach (var item in allTransaction)
{
report.AppendLine($"{item.Date.ToShortDateString()}\t{item.Amount}\t{item.Note}");
}
return report.ToString();
}
}
}