public static double Get(string name, double min, double max)
Console.Write("Enter Now: ");
string inputStr = Console.ReadLine();
if (double.TryParse(inputStr, out result))
if (result >= min && result <= max)
Console.WriteLine("Invalid input. Please enter a valid input within the accepted range.");
public static int Get(string name, int min, int max)
return (int)Get(name, (double)min, (double)max);
public double Loan { get; set; }
public double Rate { get; set; }
public int Term { get; set; }
_payment = CalcPayment(Loan, Rate, Term);
private double _totalCost;
_totalCost = CalcTotalCost(Payment, Term);
private double _totalInterest;
public double TotalInterest
_totalInterest = CalcTotalInterest(TotalCost, Loan);
Loan = UserInput.Get("Loan amount", 1, 100000000);
Rate = UserInput.Get("Interest rate", 0, 25);
Term = (int)UserInput.Get("Loan term in years", 1, 50);
private double CalcPayment(double principal, double annualRate, int months)
double monthlyRate = annualRate / 1200;
double numerator = principal * monthlyRate;
double denominator = 1 - Math.Pow(1 + monthlyRate, -months);
return numerator / denominator;
private double CalcTotalCost(double payment, int months)
private double CalcTotalInterest(double cost, double principal)
public override string ToString()
if (Loan == 0 || Term == 0 || Rate == 0)
return "Mortgage of ${Loan:F2} at {Rate:F2}% for {Term} months";
public static void Main()
Console.WriteLine("Hello, welcome to the mortgage calculator");
Console.WriteLine("-----------------------------------");
while (numMortgages <= 0 || numMortgages > 10)
Console.Write("Please enter the number of mortgages (max 10): ");
string inputStr = Console.ReadLine();
int.TryParse(inputStr, out numMortgages);
Console.WriteLine("Please enter your Loan amount, your rate, and your term, one after the other.");
Mortgage[] mortgages = new Mortgage[numMortgages];
for (int i = 0; i < numMortgages; i++)
Console.WriteLine("\nMortgage");
mortgages[i] = new Mortgage();
Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10} {5,-10}", "Loan", "Rate", "Months", "Payment", "TotalCost", "Interest");
Console.WriteLine("{0,-10:F2} {1,-10:F2}% {2,-10} {3,-10:F2} {4,-10:F2} {5,-10:F2}", mortgages[i].Loan, mortgages[i].Rate, mortgages[i].Term * 12, mortgages[i].Payment, mortgages[i].TotalCost, mortgages[i].TotalInterest);