Update Lesson 4 Task 1

This commit is contained in:
Stanislav Mykhailenko 2023-04-13 17:54:09 +03:00
parent d1cee955df
commit 20d02a283a
GPG key ID: 1E95E66A9C9D6A36
2 changed files with 32 additions and 21 deletions

View file

@ -0,0 +1,32 @@
namespace SingleResponsibility
{
public class Balance
{
private readonly CustomerList _customerList;
public Balance(CustomerList customerList)
{
_customerList = customerList;
}
public void GetBalance(int id)
{
Console.WriteLine($"Your balance is: {GetBalanceById(id)}");
}
public decimal? GetBalanceById(int id)
{
var customer = _customerList.GetById(id);
if (customer != null)
return customer.Balance;
return null;
}
public void UpdateBalance(int id, decimal newBalance)
{
var customer = _customerList.GetById(id);
if (customer != null)
customer.Balance = newBalance;
}
}
}

View file

@ -8,30 +8,9 @@
new Customer(2, "VHarbar", 100000)
};
public void GetBalance(int id)
{
Console.WriteLine($"Your balance is: {GetBalanceById(id)}");
}
public decimal? GetBalanceById(int id)
{
var customer = Customers.FirstOrDefault(x => x.Id == id);
if (customer != null)
return customer.Balance;
else
return null;
}
public Customer? GetById(int id)
{
return Customers.FirstOrDefault(x => x.Id == id);
}
public void UpdateBalance(int id, decimal newBalance)
{
var customer = GetById(id);
if (customer != null)
customer.Balance = newBalance;
}
}
}