19 Oct 2006
using System;
using System.Collections.Generic;
using System.Text;
namespace Lab01_Mortgage_Calculator
{
//Bill Chun Wai Hung
//10/11/2006
// learn how to use different data types
// how to input and output through the console
// how to pass variables to and from functions
public class Program
{
public static void Main(string[] args)
{
int intLoanAmount, intAnnualInterestRate, intLoanYear;
double loanTotal, monthlyPayment;
Program p = new Program();
// check the number of argument passed
if (args.Length != 3)
{
String strInput;
//prompt for inputs
Console.Write("Loan amount: $");
strInput = Console.ReadLine();
intLoanAmount = System.Convert.ToInt32(strInput);
Console.Write("Annual interest rate percent: ");
strInput = Console.ReadLine();
intAnnualInterestRate = System.Convert.ToInt32(strInput);
Console.Write("Loan years: ");
strInput = Console.ReadLine();
intLoanYear = System.Convert.ToInt32(strInput);
}
else
{
//get numbers from arguments
intLoanAmount = System.Convert.ToInt32(args[0]);
intAnnualInterestRate = System.Convert.ToInt32(args[1]); ;
intLoanYear = System.Convert.ToInt32(args[2]); ;
}
//calculate the answers by functions
monthlyPayment = p.calculateMonthlyPayment(intLoanAmount, intAnnualInterestRate, intLoanYear);
loanTotal = p.calculateLoanTotal(monthlyPayment, intLoanYear);
//output the answers
Console.WriteLine("Loan Total: ${0:f2}", loanTotal);
Console.WriteLine("Monthly Payment: ${0:f2}", monthlyPayment);
}
public double calculateLoanTotal(double monthlyPayment, int intLoanYear)
{
return (monthlyPayment * intLoanYear * 12.0);
}
public double calculateMonthlyPayment(int intLoanAmount, int intAnnualInterestRate, int intLoanYear)
{
double P, L, c, n; //Payment, P = L[c(1+c)^n]/[(1 + c)^n -1 ]
L = intLoanAmount;
n = intLoanYear * 12;
c = intAnnualInterestRate / 12.0 / 100;
P = L * (c * Math.Pow(1 + c, n)) / (Math.Pow(1 + c, n) - 1);
return P;
}
}
}
|