Adding journal entries and database tables. Increased starting balances.
This commit is contained in:
@@ -1,14 +1,40 @@
|
||||
namespace FinancialApi.Application;
|
||||
|
||||
public record Account(int Id, decimal Balance)
|
||||
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 ApplyFee(decimal amount)
|
||||
{
|
||||
if (amount <= 0)
|
||||
{
|
||||
throw new ArgumentException("Fee must be positive.");
|
||||
}
|
||||
|
||||
|
||||
if (Balance - amount < 0)
|
||||
{
|
||||
throw new InvalidOperationException("Insufficient funds.");
|
||||
@@ -16,18 +42,19 @@ public record Account(int Id, decimal Balance)
|
||||
|
||||
return this with { Balance = Balance - amount };
|
||||
}
|
||||
|
||||
|
||||
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 };
|
||||
}
|
||||
|
||||
@@ -37,7 +64,7 @@ public record Account(int Id, decimal Balance)
|
||||
{
|
||||
throw new ArgumentException("Credit amount must be positive.");
|
||||
}
|
||||
|
||||
|
||||
return this with { Balance = Balance + amount };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user