Store Demo Program

This demo program computes the total cost of purchasing donuts (75 cents each) at a store including 6% PA state sales tax. When the user clicks a button, the program computes and displays the total cost of purchasing the desired amount of donuts in a label.

Before you start designing the interface or planning the project, you should build a test plan. Here is a Microsoft Excel spreadsheet that can be used to test the program.

Since the first step of writing a project is to design the interface, a hand-sketched interface is shown below.

  • txtDonuts - A text box for the user's input for the number of donuts that he/she would like to purchase.
  • lblTotalCost - A label to store the final price with tax included.
  • btnClear - a button to clear the total cost label and the number of donuts textbox.
  • btnExit - a button to end the program
  • other objects such as picture boxes and labels for prompt messages, the title of the store, graphics of the donuts, etc.

The second step of the writing a project is to plan the algorithm. Let's work together in class to develop the pseudocode for this program.

  • declare variables and constants
  • user inputs the number of donuts
  • user clicks the Compute button
  • multiply the number of donuts (numDonuts) by the price per donut (PRICE_PER_DONUT) and the tax rate (TAX_RATE) and store that total in the variable totalCost
  • display the total cost in a label with a dollar sign
  • clear the textbox and total cost label when the user clicks the Clear button
  • end the program when the user clicks the Exit button

The third step of writing a project is to code the program. The objects are placed on the form in the correct order so the focus moves correctly when the Tab key is pressed. They are immediately named with the proper prefixes.

Variables and constants are used so the program is easy to understand and upgrade in the future.
  • PRICE_PER_DONUT - A constant to store the price of each donut (75 cents).
  • TAX_RATE - A constant to store the tax rate (1.06).
  • numDonuts - The number of donuts purchased by the customer
  • totalCost - the total cost of the purchased donuts.

Determine which button should be double-clicked so that the code can be typed into its Click method?

Type the declaration statements for the variables and constants. Determine the proper data types. Make sure that each variable is explained with an inline comment

Dim numDonuts As Integer = 0                   ' # of donuts purchased
Dim totalCost As Double = 0                    ' total cost of the purchase
Const PRICE_PER_DONUT As Double = 0.75         ' price per donut
Const TAX_RATE As Double = 1.06                ' PA state sales tax


Use Val to convert the inputted number of donuts to a numeric value.

numDonuts = Val(txtDonuts.Text)

Type one or more assignment statements that compute the total cost and stores that value into totalCost

totalCost = numDonuts * PRICE_PER_DONUT * TAX_RATE
.
Write a statement that converts the total cost and concatenates it with a dollar sign for display in the label lblTotalCost.

lblTotalCost.Text = "$" + Str(totalCost)

Be sure that your program follows the Coding Standards so it includes blank lines, comments, and good variable and object names. Also, it is important to use a test plan with 10 or more test cases to help debug your program. It is helpful to build the test plan as a spreadsheet.

Here is the code for the compute button's Click method:


    ' computes the cost of purchasing donuts
    Private Sub btnCompute_Click(sender As System.Object, e As System.EventArgs) Handles btnCompute.Click

        ' *********************** declaration statements *******
        Dim numDonuts As Integer = 0                ' # of donuts purchased
        Dim totalCost As Double = 0                 ' total cost of purchases
        Const PRICE_PER_DONUT As Double = 0.75      ' price per donut
        Const TAX_RATE As Double = 1.06             ' PA state sales tax

        ' ********************* obtain user input **************
        numDonuts = Val(txtDonuts.Text)

        ' ********************** calculations ******************
        totalCost = numDonuts * PRICE_PER_DONUT * TAX_RATE

        ' ********************** display the output ************
        lblTotalCost.Text = "$" + Str(totalCost)


    End Sub
        
         
        

Note that the statements in the body of the method above could be reduced to a single line of code! This is bad style though since it is more difficult to understand and upgrade a program that does not use variables and constants that have good names.

lblTotalCost.Text = "$" + Str(Val(txtDonuts.Text) * 0.75 * 1.06)