-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountService.cs
More file actions
81 lines (70 loc) · 2.24 KB
/
Copy pathAccountService.cs
File metadata and controls
81 lines (70 loc) · 2.24 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
namespace DebuggingAndRefactoringTask1;
public class AccountService
{
private readonly List<Account> accounts = [];
/// <summary>
/// Add an account
/// </summary>
/// <param name="id">Id of account</param>
/// <param name="name">Name of account holder</param>
public void AddAccount(string id, string name)
{
// TOOO: Check if account with same ID already exists
var account = new Account { Id = id, Name = name, Balance = 0 };
accounts.Add(account);
}
public decimal DepositMoney(string id, decimal amount)
{
foreach (var account in accounts)
{
if (account.Id == id)
{
account.Balance += amount;
return account.Balance;
}
}
throw new InvalidOperationException("Account not found.");
}
/// <summary>
/// Withdraws money from the account with the specified ID.
/// </summary>
/// <param name="id">Account id</param>
/// <param name="amount">Amount to withdraw</param>
/// <returns>true if withdrawal successful, false if not</returns>
/// <exception cref="InvalidOperationException">Thrown if account doesn't exist</exception>
public bool WithdrawMoney(string id, decimal amount)
{
foreach (var account in accounts)
{
if (account.Id == id)
{
if (account.Balance >= amount)
{
account.Balance -= amount;
return true;
}
else
{
return false;
}
}
}
throw new InvalidOperationException("Account not found.");
}
public void DisplayAccountDetails()
{
Console.WriteLine("Enter Account ID:");
string? id = Console.ReadLine();
foreach (var account in accounts)
{
if (account.Id == id)
{
Console.WriteLine($"Account ID: {account.Id}");
Console.WriteLine($"Account Holder: {account.Name}");
Console.WriteLine($"Balance: {account.Balance}");
return;
}
}
Console.WriteLine("Account not found.");
}
}