Al Capwned
Al Capwned

Reputation: 90

Arithmetic problem when using Case statements

I must design a Windows Forms app that calculates the total cost of Broadway tickets for a given group. The user enters how many people are in the group and the total cost is calculated. We must use case statements to accomplish this.

Broadway ticket group discounts are given as follows:

What I'm having trouble with is getting certain cases to use the given arithmetic. The first two groups (1-8 and 9-12) work fine, but then the program keeps calculating as if the group discount for 13-24 is still $219. Its the same for the 25-99 group.

My Code (txtNumber is the textbox object, lbltotal is the total label)

Option Strict On

Public Class frm_broadway_ticket_group
    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) 
      Handles btnCalculate.Click
        Dim decTotal1 As Decimal
        Dim decNumber As Decimal

        decNumber = Convert.ToDecimal(txtNumber.Text)

        Select Case decNumber
            Case Is <= 8
                decTotal1 = decNumber * 249
            Case Is >= 9
                decTotal1 = decNumber * 219
            Case Is <= 12
                decTotal1 = decNumber * 219
            Case Is >= 13
                decTotal1 = decNumber * 199
            Case Is <= 24
                decTotal1 = decNumber * 199
            Case Is >= 25
                decTotal1 = decNumber * 169
            Case Is <= 99
                decTotal1 = decNumber * 169
        End Select

        lbltotal.Text = decTotal1.ToString("C")
    End Sub
End Class

TLDR: What I'm trying to do is get the groups that are 13-24 to multiply by 199 and the 25-99 groups to multiply by 169. Both groups will only multiply by 219, which is the previous group ticket price.

I have a feeling that I'm missing something, but my textbook and professor hardly covered these types of statements

Upvotes: 0

Views: 108

Answers (2)

Jujucat
Jujucat

Reputation: 168

Why use those when you can use To?

        Case Is <= 8
            decTotal1 = decNumber * 249
        Case 9 To 12
            decTotal1 = decNumber * 219
        Case 13 To 24
            decTotal1 = decNumber * 199
        Case 25 To 99
            decTotal1 = decNumber * 169

Upvotes: 2

jmcilhinney
jmcilhinney

Reputation: 54487

All your >= cases are not useful and actually detrimental. For instance, if you get to Case Is <= 12 then you already know that you didn't match Case Is <= 8 so you know for a fact that the value is greater than or equal to 9, so what's the point testing for that at all? That means that that case is useless but, worse than that, Case Is >= 9 is going to match anything above 9, so none of your other cases are ever going to be tested.

Get rid of all those useless cases and just keep the <= ones. Depending on the specifics, you may also want to add Case Else at the bottom, in case decNumber is greater than 99.

Upvotes: 1

Related Questions