BillHung.Net


powered by FreeFind     sms

Lab 04 GUI Mortgage calculator

15 Nov 2006

Output

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

//Bill Chun Wai Hung
//11/15/2006
//learned GUI interfaces
namespace Lab04_Windows_Mortgage_Calculator
{
public partial class Form1 : Form
{ //variables needed for calculations
double dLoanAmount, dAnnualInterestRate, dLoanYear;
double loanTotal, monthlyPayment;
public Form1()
{
InitializeComponent();
}
private void textBox_Input(object sender, EventArgs e)
{ //check if there are values in all 3 textboxes
if (textBox1.TextLength > 0 &&
textBox2.TextLength > 0 &&
textBox3.TextLength > 0)
{
try
{ //convert string input to integers for calculation 
dLoanAmount = System.Convert.ToDouble(textBox1.Text);
dAnnualInterestRate = System.Convert.ToDouble(textBox2.Text);
dLoanYear = System.Convert.ToDouble(textBox3.Text);
}
catch
{
clearTextbox();
}
//calculate the answers by functions 
monthlyPayment = this.calculateMonthlyPayment();
loanTotal = this.calculateLoanTotal();
//output answers
textBox4.Text = monthlyPayment.ToString();
textBox5.Text = loanTotal.ToString("C", System.Threading.Thread.CurrentThread.CurrentCulture);
}
}
//control the input string
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
char c = e.KeyChar;
if (!char.IsDigit(e.KeyChar) && (c != '.'))
{
textBox5.Text = "Invalid Input";
e.Handled = true;
} 
}
//calculate the monthly payment
public double calculateLoanTotal()
{
return (monthlyPayment * dLoanYear * 12.0);
}
//calculate the monthlypayment
public double calculateMonthlyPayment()
{
double P, L, c, n; //Payment, P = L[c(1+c)^n]/[(1 + c)^n -1 ]

L = dLoanAmount;
n = dLoanYear * 12;
c = dAnnualInterestRate / 12.0 / 100;

P = L * (c * Math.Pow(1 + c, n)) / (Math.Pow(1 + c, n) - 1);

return P;
}
private void button1_Click(object sender, EventArgs e)
{ //the clear button
clearTextbox(); 
}
private void clearTextbox()
{ //clear all the textboxes
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
}
}
}