In this article, I will be showing an implementation of my favorite method, the brute force method, to implement a compound interest calculation in a web calculator project.
The brute force method means finding your way into the desired solution as quickly as possible, "no matter" what. If you break your problem, you will have basically these parts,
We are going to add an option to calculate compound interest in the .NET Core Web Calculator.
This example above is going to be used later as unit testing.
The article base
Adding new items into the project
Let's start adding a new folder, called business, and a class inside it.:
Add a new class as the model for the input/output,
I included the same reference as used to describe the formula, in the 3.3.1 section of this article, at their properties display value
- namespace NetCoreCalculator.Models
- {
- public class CompoundInterestModel
- {
- [Display( Name = "Result - A" )]
- public double Result { get; set; }
-
- [Display( Name = "Initial Investment - P" )]
- public double Investment { get; set; }
-
- [Display( Name = "Interest Rate - r" )]
- public double InterestRate { get; set; }
-
- [Display( Name = "Period of Time - n" )]
- public int TimePeriod { get; set; }
-
- }
- }
Add a new View for the Compound Interest calculation,
- @model NetCoreCalculator.Models.CompoundInterestModel
-
- @{
- ViewData["Title"] = "Thiago Vivas";
- }
-
- <h2>Compound Interest Calculation</h2>
-
- <hr />
- <div class="row">
- <div class="col-md-4">
- <form asp-action="CompoundInterest">
- <div asp-validation-summary="ModelOnly" class="text-danger"></div>
- <div class="form-group">
- <label asp-for="Investment" class="control-label"></label>
- <input asp-for="Investment" class="form-control" />
- <span asp-validation-for="Investment" class="text-danger"></span>
- </div>
- <div class="form-group">
- <label asp-for="InterestRate" class="control-label"></label>
- <input asp-for="InterestRate" class="form-control" />
- <span asp-validation-for="InterestRate" class="text-danger"></span>
- </div>
- <div class="form-group">
- <label asp-for="TimePeriod" class="control-label"></label>
- <input asp-for="TimePeriod" class="form-control" />
- <span asp-validation-for="TimePeriod" class="text-danger"></span>
- </div>
- <div class="form-group">
- <label asp-for="Result" class="control-label"></label>
- <input disabled="disabled" asp-for="Result" class="form-control" />
- </div>
- <div class="form-group">
- <input type="submit" value="Calculate" class="btn btn-default" />
- </div>
- </form>
- </div>
- </div>
-
- @section Scripts {
- @{await Html.RenderPartialAsync( "_ValidationScriptsPartial" );}
- }
Modify the home controller in order to access the new view,
- namespace NetCoreCalculator.Controllers
- {
- public class HomeController : Controller
- {
- [HttpGet]
- public IActionResult Index()
- {
- return View();
- }
-
- [HttpPost]
- public IActionResult Index( Operation model )
- {
- if ( model.OperationType == OperationType.Addition )
- model.Result = model.NumberA + model.NumberB;
- return View( model );
- }
-
- [HttpPost]
- public IActionResult CompoundInterest( CompoundInterestModel model )
- {
- return View( model );
- }
-
- [HttpGet]
- public IActionResult CompoundInterest()
- {
- return View();
- }
- }
- }
Algorithm breakdown
The formula,
A = P (1 + r) (n)
Where,
A = the future value of the investment/loan, including interest. Our result
P = the principal investment amount
r = the interest rate
n = the number of times that interest is compounded
Algorithm base
- Calculate n * t;
- Calculate 1 + r/n
- Calculate the (result from number 2) ^ (the result from number 1)
- Calculate P * (result from number 3)
Applying the algorithm using the brute force method
First, let's organize our method signatures and connect our front-end to our back-end,
P.S.: The method and variable names do not matter yet.
Right now, I think that we only need to send the result as output and the model as input. Here we have the signature of the method,
- public static class Calculation
- {
- public static double doesNotMatter( CompoundInterestModel model )
- {
- return 0;
- }
- }
And here, our controller,
- public class HomeController : Controller
- {
- [HttpGet]
- public IActionResult Index()
- {
- return View();
- }
-
- [HttpPost]
- public IActionResult Index( Operation model )
- {
- if ( model.OperationType == OperationType.Addition )
- model.Result = model.NumberA + model.NumberB;
- return View( model );
- }
-
- [HttpPost]
- public IActionResult CompoundInterest( CompoundInterestModel model )
- {
- model.Result = Calculation.doesNotMatter( model );
- return View( model );
- }
-
- [HttpGet]
- public IActionResult CompoundInterest()
- {
- return View();
- }
- }
Now, let's apply our algorithm in the doesNotMatter method. And this is the result,
- public static class Calculation
- {
- public static double doesNotMatter( CompoundInterestModel model )
- {
- var asdasd = model.TimePeriod;
- var sdoad = 1 + ( (model.InterestRate / 100) / model.TimePeriod );
- var asdasds = Math.Pow( sdoad, asdasd );
- var owee = asdasds * model.Investment;
- return owee;
- }
- }
Testing the result
This is a very important step, let's test our result with our previous example,
Example
- Initial Value: 1000;
- Period of time: 10 months;
- Interest Rate: 2% per month;
- Value addition: 100 per month;
- Result: 2313,89
The result is completely different from what we expected, so let's fix it,
After fixing the formula, this is the method which matches with the expected result:
Code clean-up
Now, it is time to adjust our variables and methods so they may have the right name for their purpose. In the end, you must look into your code and be proud of it, having in your mind that anything else is necessary to make your code readable and reusable.
Organize your methods
That's the final result, which must make sense now,
- public static class Calculation
- {
- public static double CalculateCompoundInterest( CompoundInterestModel model )
- {
- var asdasd = model.TimePeriod;
- var sdoad = 1 + GetPercentValue( model.InterestRate );
- var asdasds = Math.Pow( sdoad, model.TimePeriod );
- var owee = asdasds * model.Investment;
- return owee;
- }
-
- private static double GetPercentValue( double number )
- {
- return number / 100;
- }
- }
Organize your variables
Now revise your variables and rename them according to their purpose,
- public static class Calculation
- {
- public static double CalculateCompoundInterest( CompoundInterestModel model )
- {
- var initialInvestment = model.TimePeriod;
- var interestRateCalculation = 1 + GetPercentValue( model.InterestRate );
- var powerResult = Math.Pow( interestRateCalculation, model.TimePeriod );
- return powerResult * model.Investment;
- }
-
- private static double GetPercentValue( double number )
- {
- return number / 100;
- }
- }
Comment your code
That's a very important step, go through your code and comment as much as possible,
-
-
-
- public static class Calculation
- {
-
-
-
-
-
-
- public static double CalculateCompoundInterest( CompoundInterestModel model )
- {
-
- var initialInvestment = model.TimePeriod;
-
-
- var interestRateCalculation = 1 + GetPercentValue( model.InterestRate );
-
-
- var powerResult = Math.Pow( interestRateCalculation, model.TimePeriod );
-
-
- return powerResult * model.Investment;
- }
-
-
-
-
-
-
-
- private static double GetPercentValue( double number )
- {
- return number / 100;
- }
- }
Re-test your result
In order to guarantee that you did not break anything, you must run 100% of your tests again.
Congratulations, you have successfully applied the brute force method using .NET Core.