The Code for Kred.


                //The Loan Model
                public class Loan
                {
                    public decimal Amount { get; set; }
                    public decimal Rate { get; set; }
                    public decimal TotalInterest { get; set; }
                    public decimal TotalCost { get; set; }
                    public decimal Payment { get; set; }
                    public int Term { get; set; }
                    public List< LoanPayment > Payments { get; set; } = new();
                }

                //The LoanPayment Model
                public class LoanPayment
                {
                    public int Month { get; set; }
                    public decimal Payment { get; set; }
                    public decimal MonthlyPrincipal { get; set; }
                    public decimal MonthlyInterest { get; set; }
                    public decimal TotalInterest { get; set; }
                    public decimal Balance { get; set; }
                }

                //The LoanHelper Object
                public class LoanHelper
                {
                    public Loan GetPayments(Loan loan)
                    {
                        loan.Payment = CalcPayment(loan.Amount, loan.Rate, loan.Term);

                        //Local variables used for calculations in loop
                        var balance = loan.Amount;
                        var totalInterest = 0.0m;
                        var monthlyInterest = 0.0m;
                        var monthlyPrincipal = 0.0m;
                        var monthlyRate = CalcMonthlyRate(loan.Rate);

                        //Calculate every row an add to list
                        for (int month = 1; month <= loan.Term; month++)
                        {
                            monthlyInterest = CalcMonthlyInterest(balance, monthlyRate);
                            totalInterest += monthlyInterest;
                            monthlyPrincipal = loan.Payment - monthlyInterest;
                            balance -= monthlyPrincipal;

                            //Temp instance used to copy row data to list
                            LoanPayment loanPayment = new();

                            loanPayment.Month = month;
                            loanPayment.Payment = loan.Payment;
                            loanPayment.MonthlyPrincipal = monthlyPrincipal;
                            loanPayment.MonthlyInterest = monthlyInterest;
                            loanPayment.TotalInterest = totalInterest;
                            loanPayment.Balance = balance;

                            loan.Payments.Add(loanPayment);
                        }

                        loan.TotalInterest = totalInterest;
                        loan.TotalCost = loan.Amount + loan.TotalInterest;

                        return loan;
                    }

                    //Calculate monthly payment
                    private decimal CalcPayment(decimal amount, decimal rate, int term)
                    {
                        rate = CalcMonthlyRate(rate);

                        var rateD = Convert.ToDouble(rate);
                        var amountD = Convert.ToDouble(amount);
                        var paymentD = (amountD * rateD) / (1 - Math.Pow(1 + rateD, - term));

                        return Convert.ToDecimal(paymentD);
                    }

                    private decimal CalcMonthlyRate(decimal rate)
                    {
                        return rate / 1200;
                    }

                    private decimal CalcMonthlyInterest(decimal balance, decimal monthlyRate)
                    {
                        return balance * monthlyRate;
                    }
                }

                //The Controller
                [HttpGet]
                public IActionResult App()
                {
                    //Set default values
                    Loan loan = new();
                    loan.Payment = 0.0m;
                    loan.TotalInterest = 0.0m;
                    loan.TotalCost = 0.0m;
                    loan.Rate = 10m;
                    loan.Amount = 150000m;
                    loan.Term = 60;

                    return View(loan);
                }

                [HttpPost]
                [ValidateAntiForgeryToken]
                public IActionResult App(Loan loan)
                {
                    var loanHelper = new LoanHelper();

                    Loan newLoan = loanHelper.GetPayments(loan);
                    return View(newLoan);
                }

                //The View
                < div class="col text-center" >
                    < div id="paymentTitle" >
                        < p class="h5">Your Monthly Payments< /p >
                    < /div >
                    < p id="payment" class="h1" >
                        Model.Payment.ToString("C")
                    < /p >
                    < div class="row mt-5" >
                        < div class="col-6" >
                            < label class="text-left" >Total Principal< /label >
                        < /div >
                        < div class="col-6 text-right" id="totalPrincipal" >
                            Model.Amount.ToString("C")
                        < /div >
                        < div class="col-6" >
                            < label class="text-left" >Total Interest< /label >
                        < /div >
                        < div class="col-6 text-right" id="totalInterest" >
                            Model.TotalInterest.ToString("C")
                        < /div >
                        < div class="col-6" >
                            < label class="text-left" >Total Cost< /label >
                        < /div >
                        < div class="col-6 text-right" id="totalCost" >
                            Model.TotalCost.ToString("C")
                        < /div >
                    < /div >
                < /div >
            < /div >

            //Display down payment table if it exists
            if (Model.Payments.Count > 0)
            {
                    < div class="row mt-3" >
                    < div class="col px-0" >
                        < table id="scheduleTable" class="table table-striped table-hover" >
                            < thead class="tableHeader" >
                                < tr >
                                    < th >Month< /th >
                                    < th >Payment< /th >
                                    < th >Principal< /th >
                                    < th >Interest< /th >
                                    < th >Total Interest< /th >
                                    < th >Balance< /th >
                                < /tr >
                            < /thead >
                            < tbody >

                                //Display each row
                                foreach (var item in Model.Payments)
                                {
                                    < tr >
                                        < td >item.Month< /td >
                                        < td >item.Payment.ToString("C")< /td >
                                        < td >item.MonthlyPrincipal.ToString("C")< /td >
                                        < td >item.MonthlyInterest.ToString("C")< /td >
                                        < td >item.TotalInterest.ToString("C")< /td >
                                        < td >item.Balance.ToString("C")< /td >
                                    < /tr >
                                }
                            < /tbody >
                        < /table >
                    < /div >
                < /div >   
            }
                
                

Kred is written in ASP.NET Core MVC using C# and .NET6. The C# code (specific to the loan calculations) is structed in two models, a helper object and two action result methods.

Get Method

The get method is an IActionResult method called App, which simply combines the Loan object and the view and returns the view in response to an http get request.

Post Method

The post method is an IActionResult method also called App. This method is run in response to an http post request, and takes in a Loan model containing user input data and a list of type LoanPayment. The input data stored in the model is then passed to the GetPayments method in the LoanHelper object. The output is then stored in an instance of Loan model called newLoan, which is combined with the correct view and returned in response to the post request.

The Loan Model

This model is a class called Loan, which contains seven properties used to share data between the controller and the view. One of theese properties is a list of type LoanPayment. which is used to display multiple rows of calculated values in a table. The remaining six properties are two different number types used to calculate the values to be listed in the LoanPayments list.

The LoanPayment Model

This model is a class called LoanPayment, which contains six properties used to hold data for a list.

The LoanHelper Object

This is the object preforming all the calculations needed for this app to function. By using a for loop it calculates all the payments (including interests and pricipals) month by month, and adds theese values to the LoanPayments list.

The View

The view is a cshtml page for interacting with the app, and displaying the calculated payment results. Using razor synthax we can display the results in a table of six coloumns, which will only appear if there are any results to display. The table rows are printed using a foreach loop which runs once for each list item (in this case) it is given, displaying data values contained in the Model.Payment list.