Can anybody implement this algorithm to code in VBA?

l0lcat

New Member
Joined
May 23, 2015
Messages
5
'This function is the starting point of the program.
'It welcomes the user, and asks which package they want (after displaying a menu of options).
'Once they enter a valid package it uses getQuantity to get the number of each type of ticket
'(adult, child and concession) and then finds the total cost using calcTotalCost.
'If the total cost is over 100 then it uses getDiscount to see if they get a 20% discount,
'and tells the user if they get it. Finally it prints the total cost and ends.
Code:
Sub main()
    'display welcome to user and ask which package they want
    'get package from user, convert it to uppercase
    'while package is not 'B', or 'P', or 'E'
    '        Print Error; Message
    '        get package from user, convert it to uppercase
    'adultTickets = call getQuantity("How many adult tickets? ")
    'childTickets = call getQuantity("How many child tickets? ")
    'concTickets = call getQuantity("How many concession tickets? ")
    'ticketCost = call calcTotalCost(package, adultTickets, childTickets, concTickets)
    'if ticketCost is > 100 then:
    'discounted = call getDiscount()
    'If discounted = True Then:
    '        ticketCost = ticketCost * 0.8
    '        display "You received a 20% discount"
    'display ticketCost
End Sub


'This function will take the type of package, and the number of each type of ticket
'(adult, child and concession) as inputs and will calculate the total cost of all
'tickets for the appropriate package. This value will then be returned.
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
End Function


'This function generates a random number, and if it is equal to the 'lucky number'
'then it returns 'True', otherwise it returns 'False'
Function getDiscount()
    'generate randomNum between 1-10 inclusive
    'If randomNum = LUCKYNUM Then:
        'discount = True
    'otherwise:
        'discount = False
    'return discount
End Function


'This function will take a prompt as input, display it to the user and get a value in response.
'If the user enters a value below zero then an error is displayed, and they will be asked again
'until they enter a value of 0 or more. This value is then returned from the function
Function getQuantity(prompt):
    'display prompt
    'get value from user
    'while value is less than zero:
        'Print an Error
        'display prompt
        'get value
    'Return 'value'
End Function
 
Last edited by a moderator:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
So, the problem is ???
AND
can you please use code tags ???
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,290
Members
449,149
Latest member
mwdbActuary

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