54 lines
1.4 KiB
C#
54 lines
1.4 KiB
C#
namespace FinancialApi.Domain.Entities;
|
|
|
|
public record Account(int Id, decimal Balance, string AccountType)
|
|
{
|
|
public Account ApplyPosting(decimal amount, EntryType entryType)
|
|
{
|
|
if (amount <= 0)
|
|
{
|
|
throw new ArgumentException("Credit amount must be positive.");
|
|
}
|
|
|
|
var newBalance = Balance;
|
|
if (AccountType == "Revenue")
|
|
{
|
|
newBalance = entryType == EntryType.Credit ? newBalance + amount : newBalance - amount;
|
|
}
|
|
else
|
|
{
|
|
newBalance = entryType == EntryType.Debit ? newBalance - amount : newBalance + amount;
|
|
}
|
|
|
|
if (newBalance < 0 && AccountType == "Liability")
|
|
{
|
|
throw new InvalidOperationException("Insufficient funds.");
|
|
}
|
|
|
|
return this with { Balance = newBalance };
|
|
}
|
|
|
|
public Account Debit(decimal amount)
|
|
{
|
|
if (amount <= 0)
|
|
{
|
|
throw new ArgumentException("Debit amount must be positive.");
|
|
}
|
|
|
|
if (Balance - amount < 0)
|
|
{
|
|
throw new InvalidOperationException("Insufficient funds.");
|
|
}
|
|
|
|
return this with { Balance = Balance - amount };
|
|
}
|
|
|
|
public Account Credit(decimal amount)
|
|
{
|
|
if (amount <= 0)
|
|
{
|
|
throw new ArgumentException("Credit amount must be positive.");
|
|
}
|
|
|
|
return this with { Balance = Balance + amount };
|
|
}
|
|
} |