EXCEL VBA : ticket ordering system

flame_Zero

New Member
Joined
May 22, 2016
Messages
6
Does anyone know how to code this as Sub main and functions? Help!!


When the program runs it will welcome the user and ask them which package (of 3) they are ordering, Basic, Premium or Extravagant. All tickets must be from the same package.

Users will enter B P or E (system must handle lower case as well as upper). Incorrect input will be met with an error message, and the user will be asked again until they get it right.

Regardless of which package they pick they will then be asked how many adult, child and concession tickets they are ordering. Numbers must be 0 or greater. Invalid input will result in an immediate error and re-prompt (ONLY for the invalid value).

The system will then calculate the total cost of the tickets using the following figures:


Basic
Adult
10
Child
5
Concession
8

<tbody>
</tbody>

Premium prices are 2 times the price of the basic tickets. Extravagant prices are 5 times the price of the basic tickets.



If the total cost is more than $100 then there is a 1 in 10 chance of receiving a 20% discount. If necessary this discount is applied, and the user will be informed. The final ticket cost will then be displayed.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Untested

Code:
Sub Tickets()    Dim sPackage As String
    Dim bCont As Boolean
    Dim ad As Integer
    Dim ch As Integer
    Dim con As Integer
    Dim num As Variant
    Dim multi As Integer
    Dim total As Long
    Dim randnum As Double
    Dim bDisc As Boolean
    
    bCont = False
    bDisc = False
    
    Do Until bCont
        sPackage = UCase(InputBox("Please enter B, P or E:"))
        If Len(sPackage) = 1 Then
            Select Case sPackage
                Case "B"
                    multi = 1
                    bCont = True
                Case "P"
                    multi = 2
                    bCont = True
                Case "E"
                    multi = 5
                    bCont = True
                Case Else
                    MsgBox "Please type B, P or E", vbOKOnly
            End Select
        Else
            MsgBox "Please type B, P or E", vbOKOnly
        End If
    Loop
    
    bCont = False
    
    Do Until bCont
        num = InputBox("Enter # of adult tickets:")
        If IsNumeric(num) And num >= 0 Then
            ad = num
            bCont = True
        Else
            MsgBox "Please enter 0 or a positive number", vbOKOnly
        End If
    Loop
    
    bCont = False
    
    Do Until bCont
        num = InputBox("Enter # of children tickets:")
        If IsNumeric(num) And num >= 0 Then
            ch = num
            bCont = True
        Else
            MsgBox "Please enter 0 or a positive number", vbOKOnly
        End If
    Loop
    
    bCont = False


    Do Until bCont
        num = InputBox("Enter # of concession tickets:")
        If IsNumeric(num) And num >= 0 Then
            con = num
            bCont = True
        Else
            MsgBox "Please enter 0 or a positive number", vbOKOnly
        End If
    Loop
    
    total = ad * 10 * multi + ch * 5 * multi + con * 8 * multi
    
    If total >= 100 Then
        Randomize
        randnum = Rnd()
        If randnum <= 0.2 Then
            bDisc = True
        End If
    End If
    
    If bDisc Then
        MsgBox "Total cost: " & Format(total * 0.8, "$0.00") & vbCrLf & "Adult tickets: " & ad & vbCrLf & "Child tickets: " & ch & vbCrLf & "Concession tickets: " & con & vbCrLf & "20% discount included"
    Else
        MsgBox "Total cost: " & Format(total, "$0.00") & vbCrLf & "Adult tickets: " & ad & vbCrLf & "Child tickets: " & ch & vbCrLf & "Concession tickets: " & con & vbCrLf
    End If
End Sub
 
Upvote 0
THANKS SO MUCH! IT WORKS, but do you have any idea on how to separate the codes to functions? like you will have a sub main() then call the functions from there?






Untested

Code:
Sub Tickets()    Dim sPackage As String
    Dim bCont As Boolean
    Dim ad As Integer
    Dim ch As Integer
    Dim con As Integer
    Dim num As Variant
    Dim multi As Integer
    Dim total As Long
    Dim randnum As Double
    Dim bDisc As Boolean
    
    bCont = False
    bDisc = False
    
    Do Until bCont
        sPackage = UCase(InputBox("Please enter B, P or E:"))
        If Len(sPackage) = 1 Then
            Select Case sPackage
                Case "B"
                    multi = 1
                    bCont = True
                Case "P"
                    multi = 2
                    bCont = True
                Case "E"
                    multi = 5
                    bCont = True
                Case Else
                    MsgBox "Please type B, P or E", vbOKOnly
            End Select
        Else
            MsgBox "Please type B, P or E", vbOKOnly
        End If
    Loop
    
    bCont = False
    
    Do Until bCont
        num = InputBox("Enter # of adult tickets:")
        If IsNumeric(num) And num >= 0 Then
            ad = num
            bCont = True
        Else
            MsgBox "Please enter 0 or a positive number", vbOKOnly
        End If
    Loop
    
    bCont = False
    
    Do Until bCont
        num = InputBox("Enter # of children tickets:")
        If IsNumeric(num) And num >= 0 Then
            ch = num
            bCont = True
        Else
            MsgBox "Please enter 0 or a positive number", vbOKOnly
        End If
    Loop
    
    bCont = False


    Do Until bCont
        num = InputBox("Enter # of concession tickets:")
        If IsNumeric(num) And num >= 0 Then
            con = num
            bCont = True
        Else
            MsgBox "Please enter 0 or a positive number", vbOKOnly
        End If
    Loop
    
    total = ad * 10 * multi + ch * 5 * multi + con * 8 * multi
    
    If total >= 100 Then
        Randomize
        randnum = Rnd()
        If randnum <= 0.2 Then
            bDisc = True
        End If
    End If
    
    If bDisc Then
        MsgBox "Total cost: " & Format(total * 0.8, "$0.00") & vbCrLf & "Adult tickets: " & ad & vbCrLf & "Child tickets: " & ch & vbCrLf & "Concession tickets: " & con & vbCrLf & "20% discount included"
    Else
        MsgBox "Total cost: " & Format(total, "$0.00") & vbCrLf & "Adult tickets: " & ad & vbCrLf & "Child tickets: " & ch & vbCrLf & "Concession tickets: " & con & vbCrLf
    End If
End Sub
 
Upvote 0
These 3 functions. then call them from sub main(). do you think you can help? I'm stuck


Function calctotalCost(packageCode, numAdultTickets, numChildTickets, numConcTickets) As Integer
'adultCost = numAdultTickets * ADULT_TICKET_PRICE
'childCost = numChildTickets * CHILD_TICKET_PRICE
'concCost = numConcTickets * CONC_TICKET_PRICE
'totalCost = adultCost + childCost + concCost
'
'if packageCode = 'P' then:
' totalCost = totalCost * PREMIUM_FACTOR
'otherwise if packageCode = 'E' then:
' totalCost = totalCost * EXTRAVAGANT_FACTOR
'
'return totalCost

Function
getDiscount()

'generate randomNum between 1-10 inclusive
'If randomNum = LUCKYNUM Then:
'discount = True
'otherwise:
'discount = False
'return discount


Function getQuantity(prompt):
'display prompt
'get value from user
'while value is less than zero:
'Print an Error
'display prompt
'get value
'Return 'value'

what functions do you want it separated into?
 
Upvote 0

Forum statistics

Threads
1,216,030
Messages
6,128,418
Members
449,449
Latest member
Quiet_Nectarine_

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top