Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit 1128b0c1 authored by Edouard Mangel's avatar Edouard Mangel
Browse files

implement IRateFetcher interface

parent 5a32fe6c
No related branches found
No related tags found
No related merge requests found
Showing
with 160 additions and 0 deletions
File added
File added
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Credit;
public class CreditSimulationUsecase
{
public CreditSimulationUsecase(IRateFetcher rateFetcher)
{
RateFetcher = rateFetcher;
}
public IRateFetcher RateFetcher { get; }
public Amount computeMonthlyPayment(Amount borrowedAmount, int durationInMonth, RateType type)
{
var rate = RateFetcher.FetchRate(durationInMonth, type);
return MortgageSimulator.ComputeMonthlyPayment(borrowedAmount, durationInMonth, rate);
}
}
namespace Credit;
public interface IRateFetcher
{
public decimal FetchRate(int duration, RateType rate);
}
\ No newline at end of file
namespace Credit;
public class MortgageSimulator
{
public static Amount ComputeMonthlyPayment(Amount borrowedAmount, int durationInMonth, decimal rate)
{
if (borrowedAmount.Value * (int) Math.Pow(10, borrowedAmount.Currency.Precision) < 5000000) {
throw new ArgumentOutOfRangeException(nameof(borrowedAmount), "Can't borrow so little capital.");
}
double numerator = (double)(borrowedAmount * rate / 12);
double denominator = 1 - Math.Pow((double)(1 + rate / 12), - durationInMonth);
double value = numerator / denominator;
return new Amount(value, borrowedAmount.Currency);
}
}
\ No newline at end of file
namespace Credit;
public enum RateType
{
Good ,
Better,
Best
}
\ No newline at end of file
// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;
File added
// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;
File added
// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;
File added
// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;
File added
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Credit;
using NFluent;
namespace UnitTests;
public class CompleteSimulation
{
[Fact]
public void ShouldFetchRateProperly()
{
StubRateFetcher rateFetcher = new StubRateFetcher();
var simulation = new CreditSimulationUsecase(rateFetcher);
var expectedResult = new Amount(266.70d);
var actualResult = simulation.computeMonthlyPayment(50000, 250, RateType.Good);
Check.That(actualResult).IsEqualTo(expectedResult);
}
}
using Credit;
namespace UnitTests;
internal class StubRateFetcher : IRateFetcher
{
public decimal FetchRate(int duration, RateType rate)
{
switch (rate) {
case RateType.Good:
if (duration >= 25 * 12) {
return 0.03m;
} else if (duration >= 20) {
return 0.029m;
}
return 0.00256m;
case RateType.Better:
break;
case RateType.Best:
if (duration <= 7) {
return 0.021m;
}
break;
default:
break;
}
return 0.03m;
}
}
\ No newline at end of file
// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;
File added
// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment