user3318131
user3318131

Reputation: 23

Visual basic - beginning program code

My output is always 14.95 for package A..everything else seems to be working fine.

For the program I need to:

An internet service provicer offers 3 subscriptions packages to its customers, plus a discount for nonprofit organizations

btnCalc_Click

    Public Class Form1
    Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
        'Declare variables and constant

        Dim decTotal As Decimal
        Dim intHours As Integer
        Const decNonProfit As Decimal = 0.8D
     Try

            intHours = CInt(txtHoursUsed.Text)

            If CInt(txtHoursUsed.Text) > 744 Then
                MessageBox.Show("Monthly Hours Can't Exceed 744")
                txtHoursUsed.Text = String.Empty
                lblTotalDue.Text = String.Empty
            End If

            'Calculate A Package Total without discount
            If radPackageA.Checked = True And intHours > 10 Then
                decTotal = (9.95 + ((intHours - 10) * 2))
            ElseIf intHours <= 10 Then
                decTotal = 9.95
            End If

            'Calculate B Package Total without discount
            If radPackageB.Checked = True And intHours > 20 Then
                decTotal = (14.95 + ((intHours - 20) * 1))
            ElseIf intHours <= 20 Then
                decTotal = 14.95
            End If

            'Calculate C Package Total without discount
            If radPackageC.Checked = True Then
                decTotal = 19.95
            End If

            'Calculate Total for packages if Nonprofit is checked
            If chkNonProfit.Checked = True Then
                decTotal = decTotal * decNonProfit

            End If

            'Show total due
            lblTotalDue.Text = decTotal.ToString("c")

        Catch ex As Exception
            MessageBox.Show("Input Error")

        End Try

    End Sub

btnClear_Click

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        radPackageA.Checked = False
        radPackageB.Checked = False
        radPackageC.Checked = False
        chkNonProfit.Checked = False
        lblTotalDue.Text = String.Empty
        txtHoursUsed.Text = String.Empty
    End Sub

btnExit_Click

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

    End Class

Upvotes: 2

Views: 3377

Answers (2)

ɐsɹǝʌ ǝɔıʌ
ɐsɹǝʌ ǝɔıʌ

Reputation: 4512

You're using ElseIf to bifurcate the conditions in a wrong way. Following your code you'll get dectTotal = 14.95 everytime radPackageB.Checked = False and intHours <= 20

It should be like this:

        If radPackageB.Checked Then
            If intHours > 20 Then
                decTotal = (14.95 + ((intHours - 20) * 1))
            ElseIf intHours <= 20 Then
                decTotal = 14.95
            End If
        End If

So feel free to accept Mark's answer

Upvotes: 1

Mark Hall
Mark Hall

Reputation: 54532

You need to enclose your entire conditional in your selection check. What is happening is that your false condition is always being run when repackageB.Checked is false.

            'Calculate A Package Total without discount
            If radPackageA.Checked Then
                If intHours > 10 Then
                    decTotal = (9.95 + ((intHours - 10) * 2))
                ElseIf intHours <= 10 Then
                    decTotal = 9.95
                End If
            End If

            'Calculate B Package Total without discount
            If radPackageB.Checked Then
                If intHours > 20 Then
                    decTotal = (14.95 + ((intHours - 20) * 1))
                ElseIf intHours <= 20 Then
                    decTotal = 14.95
                End If
            End If

            'Calculate C Package Total without discount
            If radPackageC.Checked Then
                decTotal = 19.95
            End If

Upvotes: 2

Related Questions