Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Visual Basic Programming Help?

I have to make an auto loan calculator, I created the basic code, but my professor want us to change the interest rate depending on the credit score of the buyer. I am not sure how to do it.

Here's what our professor asked us to do:

1. Amount of the loan (vehicle coast)

2. Down Payment

3. Number of months

4. New or Used Car

5. Annual interest rate, the interest rate is a function of the credit score of the buyer

a. credit score of 700-699 interest rate is 2%

b. credit score of 600-699 interest rate is 3%

c. credit score of 500-599 interest rate is 4%

d. credit score of 400-499 interest rate is 5%

e. credit score less than 400 interest rate is 10%

6. If the car is a new car than 0.5% will be added to the interest rate

And here is my code.

Thanks for the help.

Public Class Form1

'Class Level Constants

Const dblMONTHS_YEAR As Double = 12 'Months Per Year

Const dblNEW_RATE As Double = 0.05 'IR, New Car

Const dblUSED_RATE As Double = 0.08 'IR, Used Car

'Class Varible to hol the annual interest rate

Dim dblAnnualRate As Double = dblNEW_RATE

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

Dim dblVehicleCost As Double 'Cost

Dim dblDownPayment As Double 'Down Payment

Dim intMonths As Integer '# of months for the loan

Dim dblLoan As Double 'Amount of loan

Dim dblMonthlyPayment As Double 'Monthly Payment

Dim dblInterest As Double 'Interest Paid for the period

Dim dblPrincipal As Double 'Principle paid for the period

Dim intCount As Integer 'Counter for the loop

Dim strOut As String ' Used to hold a line of output

Dim blnInputOk As Boolean = True

'Get the vehicle cost, validating at the same time.-

If Not Double.TryParse(txtCost.Text, dblDownPayment) Then

lblMessage.Text = "Vehicle cost must be a number."

blnInputOk = False

End If

'Get the down payment, validating 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)

End If

'Clear the list box and message label.

ListBox1.Items.Clear()

lblMessage.Text = String.Empty

For intCount = 1 To intMonths

'Calculate the interes rate 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 month.

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

ListBox1.Items.Add(strOut)

Next

End Sub

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click

radNew.Checked = True

dblAnnualRate = dblNEW_RATE

lblAnnualRate.Text = dblNEW_RATE.ToString("p")

txtCost.Clear()

txtDownPayment.Clear()

txtMonths.Clear()

ListBox1.Items.Clear()

lblMessage.Text = String.Empty

txtCost.Focus()

End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click

Me.Close()

End Sub

Private Sub radNew_CheckedChanged(sender As Object, e As EventArgs) Handles radNew.CheckedChanged

If radNew.Checked = True Then

dblAnnualRate = dblNEW_RATE

lblAnnualRate.Text = dblNEW_RATE.ToString("p")

ListBox1.Items.Clear()

End If

End Sub

Private Sub radUsed_CheckedChanged(sender As Object, e As EventArgs) Handles radUsed.CheckedChanged

If radUsed.Checked = True Then

dblAnnualRate = dblNEW_RATE

lblAnnualRate.Text = dblUSED_RATE.ToString("p")

ListBox1.Items.Clear()

End If

End Sub

End Class

1 Answer

Relevance
  • 8 years ago
    Favorite Answer

    The following code may give you some new ideas. There are 4 text boxes, a button, 2 radiobuttons and a listbox.

    Textbox 1 is for car cost

    Textbox 2 is for down payment

    Textbox 3 is for loan length in months

    Textbox 4 is for credit score

    RadioButton1 is to select New Car interest rate

    RadioButton1 is to select UsedCar interest rate

    The code in the trsxt boxes Key_Press event is to allow only decimal numbers to be entered in the text box

    Public Class Form1

    Dim Flag1 As Integer

    Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    Dim c As Char = e.KeyChar

    If Not (Char.IsDigit(c) Or c = "." Or Char.IsControl(c)) Then

    e.Handled = True

    End If

    End Sub

    Private Sub TextBox2_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress

    Dim c As Char = e.KeyChar

    If Not (Char.IsDigit(c) Or c = "." Or Char.IsControl(c)) Then

    e.Handled = True

    End If

    End Sub

    Private Sub TextBox3_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress

    Dim c As Char = e.KeyChar

    If Not (Char.IsDigit(c) Or c = "." Or Char.IsControl(c)) Then

    e.Handled = True

    End If

    End Sub

    Private Sub TextBox4_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress

    Dim c As Char = e.KeyChar

    If Not (Char.IsDigit(c) Or c = "." Or Char.IsControl(c)) Then

    e.Handled = True

    End If

    End Sub

    Private Sub RadioButton1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged

    Flag1 = 1

    End Sub

    Private Sub RadioButton2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton2.CheckedChanged

    Flag1 = 2

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    Dim Cost, DwnPay, MthPay, Rate, RateAdd, FinalRate As Double

    Dim Balance, Interest, Principal As Double

    Cost = Val(TextBox1.Text)

    DwnPay = Val(TextBox2.Text)

    Dim Mth As Integer = Val(TextBox3.Text)

    Dim CreditScore As Integer = Val(TextBox4.Text)

    If RadioButton1.Checked = True Then

    RateAdd = 0.5

    Else

    RateAdd = 0

    End If

    If CreditScore > 699 Then

    Rate = 2

    ElseIf CreditScore > 599 Then

    Rate = 3

    ElseIf CreditScore > 499 Then

    Rate = 4

    ElseIf CreditScore > 399 Then

    Rate = 5

    Else

    Rate = 10

    End If

    FinalRate = (Rate + RateAdd) / 1200

    MthPay = Pmt(FinalRate, Mth, -(Cost - DwnPay)).ToString("$#0.00")

    ListBox1.Items.Add("Car Cost: " & Cost.ToString("$#0.00"))

    ListBox1.Items.Add("Down Payment: " & DwnPay.ToString("$#0.00"))

    ListBox1.Items.Add("Amount Financed: " & (Cost - DwnPay).ToString("$#0.00"))

    ListBox1.Items.Add("Length of Loan (Months): " & Mth)

    ListBox1.Items.Add("Credit Score: " & CreditScore)

    ListBox1.Items.Add("Basic Interest Rate: " & Rate & "%")

    ListBox1.Items.Add("New Car Interest Rate Adder: " & RateAdd & "%")

    ListBox1.Items.Add("Final Interest Rate: " & Rate + RateAdd & "%")

    ListBox1.Items.Add("Final Monthly Interest Rate: " & FinalRate & "%")

    ListBox1.Items.Add("Monthly Payment: " & MthPay.ToString("$#0.00"))

    ListBox1.Items.Add(" ")

    ListBox1.Items.Add(" ")

    Balance = Cost - DwnPay

    ListBox1.Items.Add("AMORTIZATION TABLE ")

    ListBox1.Items.Add("====================================== ")

    ListBox1.Items.Add("Mth" & vbTab & "Payment" & vbTab & "Interest" & vbTab & "Principal" & vbTab & "Balance")

    For i = 1 To Mth

    Interest = (Balance * FinalRate).ToString("$#0.00")

    Principal = (MthPay - Interest).ToString("$#0.00")

    Balance -= Principal

    ListBox1.Items.Add(i & vbTab & MthPay & vbTab & Interest & vbTab & Principal & vbTab & Balance)

    Next

    End Sub

    End Class

    TexMav

Still have questions? Get your answers by asking now.