Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Public Class Form1

'Declare class-level constants


Const dblMONTHS_YEAR As Double = 12 'Months per year
Const dblNEW_RATE As Double = 0.05 'Interest rate, new cars
Const dblUSED_RATE As Double = 0.08 'Interest rate, used cars
'Class-velve variable to hold the annual interest rate
Dim dblAnnualRate As Double = dblNEW_RATE

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


With Me
.Timer1.Enabled = True
.ToolStripStatusLabel1.Text = "Programmed by Crystal Weatherly"
End With
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
With Me
.ToolStripStatusLabel2.Text = Date.Now.ToString
End With
End Sub
Private Sub lblMessage_Click(sender As Object, e As EventArgs) Handles
lblMessage.Click
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
'Close the application
With Me
.Close()
End With
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
'Clear all user entries
With Me
.txtCost.Text = String.Empty
.txtDownPayment.Text = String.Empty
.txtMonths.Text = String.Empty
.radNew.Checked = True
.radUsed.Checked = False
.lblAnnualRate.Text = Nothing
.lblMessage.Text = Nothing
.lstOutput.Items.Clear()
'Reset focus to txtCost
txtCost.Focus()
End With
End Sub
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles
btnCalculate.Click
'Declare variables
Dim dblVehicleCost As Double
'Vehicle cost
Dim dblDownPayment As Double
'Down payment

Dim
Dim
Dim
Dim
Dim
Dim
Dim
Dim

intMonths As Integer
'Number of months for the loan
dblLoan As Double
'Amount of the loan
dblMonthlyPayment As Double 'Monthly payment
dblInterest As Double
'Interest paid for the period
dblPrincipal As Double
'Principal paid for the period
intCount As Integer
'Counter for the loop
strOut As String
'Used to hold a line of output
blnInputOk As Boolean = True

'Get the vehicle cost, validate at the same time


If Not Double.TryParse(txtCost.Text, dblVehicleCost) Then
lblMessage.Text = "Vehicle cost must be a number"
blnInputOk = False
End If
'Get the down payment, validate at the same time
If Not Double.TryParse(txtDownPayment.Text, dblDownPayment) Then
lblMessage.Text = "Down payment must be a number"
blnInputOk = False
End If
'Get the number of months, validate at the same time
If Not Integer.TryParse(txtMonths.Text, intMonths) Then
lblMessage.Text = "Months must be an integer"
blnInputOk = False
End If
If blnInputOk = True Then
'Calculate the loan amount and monthly payment
dblLoan = dblVehicleCost - dblDownPayment
dblMonthlyPayment = Pmt(dblAnnualRate / dblMONTHS_YEAR, intMonths, -dblLoan)
'Clear the list box and message label
lstOutput.Items.Clear()
lblMessage.Text = String.Empty
For intCount = 1 To intMonths
'Calculate the interest for this period
dblInterest = IPmt(dblAnnualRate / dblMONTHS_YEAR, intCount, intMonths, dblLoan)
'Calculate the principal for this period
dblPrincipal = PPmt(dblAnnualRate / dblMONTHS_YEAR, intCount, intMonths,
-dblLoan)
'Start building the output string with the months
strOut = "Month " & intCount.ToString("d2")
'Add the payment amount to the output string
strOut &= ": payment = " & dblMonthlyPayment.ToString("n2")
'Add the interest amount to the output string
strOut &= ", interest = " & dblInterest.ToString("n2")
'Add the principal for the period
strOut &= ", principal = " & dblPrincipal.ToString("n2")
'Add the output string to the list box

lstOutput.Items.Add(strOut)
Next
End If
End Sub
Private Sub radNew_CheckedChanged(sender As Object, e As EventArgs) Handles
radNew.CheckedChanged
'If the new radio button is checked, then the user has selected a new car loan.
If radNew.Checked = True Then
dblAnnualRate = dblNEW_RATE
lblAnnualRate.Text = dblNEW_RATE.ToString("P")
lstOutput.Items.Clear()
End If
End Sub
Private Sub radUsed_CheckedChanged(sender As Object, e As EventArgs) Handles
radUsed.CheckedChanged
'If the Used radio button is check, then the user has selected a used car loan.
If radUsed.Checked = True Then
dblAnnualRate = dblUSED_RATE
lblAnnualRate.Text = dblUSED_RATE.ToString("P")
lstOutput.Items.Clear()
End If
End Sub
End Class

You might also like