Skip to content

Commit 03b19b7

Browse files
init present Value Module
1 parent 3883d68 commit 03b19b7

File tree

4 files changed

+65
-45
lines changed

4 files changed

+65
-45
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Algorithms.Financial;
4+
using FluentAssertions;
5+
using NUnit.Framework;
6+
7+
namespace Algorithms.Tests.Financial;
8+
9+
public static class PresentValueTests
10+
{
11+
[Test]
12+
public static void Present_Value_General_Tests()
13+
{
14+
PresentValue.Calculate(0.13, [10.0, 20.70, -293.0, 297.0])
15+
.Should()
16+
.Be(4.69);
17+
18+
PresentValue.Calculate(0.07, [-109129.39, 30923.23, 15098.93, 29734.0, 39.0])
19+
.Should()
20+
.Be(-42739.63);
21+
22+
PresentValue.Calculate(0.07, [109129.39, 30923.23, 15098.93, 29734.0, 39.0])
23+
.Should()
24+
.Be(175519.15);
25+
26+
PresentValue.Calculate(0.0, [109129.39, 30923.23, 15098.93, 29734.0, 39.0])
27+
.Should()
28+
.Be(184924.55);
29+
}
30+
31+
[Test]
32+
public static void Present_Value_Exception_Tests()
33+
{
34+
Assert.Throws<ArgumentException>(() => PresentValue.Calculate(-1.0, [10.0, 20.70, -293.0, 297.0]));
35+
Assert.Throws<ArgumentException>(() => PresentValue.Calculate(1.0, []));
36+
}
37+
}

Algorithms.Tests/Financial/PricePlusTaxTest.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Algorithms.Financial;
6+
7+
/// <summary>
8+
/// PresentValue is the value of an expected income stream determined as of the date of valuation.
9+
/// </summary>
10+
public static class PresentValue
11+
{
12+
public static double Calculate(double discountRate, List<double> cashFlows)
13+
{
14+
if (discountRate < 0)
15+
{
16+
throw new ArgumentException("Discount rate cannot be negative");
17+
}
18+
19+
if (cashFlows == null || cashFlows.Count == 0)
20+
{
21+
throw new ArgumentException("Cash flows list cannot be empty");
22+
}
23+
24+
double presentValue = cashFlows.Select((t, i) => t / Math.Pow(1 + discountRate, i)).Sum();
25+
26+
return Math.Round(presentValue, 2);
27+
}
28+
}

Algorithms/Financial/PricePlusTax.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)