The following code is the text of the MortgageCalculationService.egl file
at the end of Lesson 3.
package services;
service MortgageCalculationService
function amortize(inputData MortgageCalculationResult inOut)
amt MONEY = inputData.loanAmount;
// convert to monthly rate
rate DECIMAL(10, 8) = (1 + inputData.interestRate / 1200);
// convert to months
term INT = (inputData.term * 12);
// calculate monthly payment amount
pmt MONEY = (amt * (rate - 1) * Mathlib.pow(rate, term))
/ (MathLib.pow(rate, term) - 1);
totalInterest MONEY = (pmt * term) - amt;
// update result record
inputData.monthlyPayment = pmt;
inputData.interest = totalInterest;
end
end
record MortgageCalculationResult
// user input
loanAmount MONEY;
interestRate DECIMAL(10,8);
term INT;
// calculated fields
monthlyPayment MONEY;
interest MONEY;
end