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.
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.
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.
Determine which button should be double-clicked so that the code can be typed into its Click method? 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. |