Programming GUI 5: Discount

Topic Progress

The program will calculate the cost of pizzas and give a 25% discount if the customer spends more than £20. There is a logic error in the code and you will use a tracing table and research on the internet to change this


You will need (for the copy paste to work make sure Label5 and Label 9 are posisioned as above or change the code:

  • 10 labels
  • 3 NumericUpDown
  • 1 Button

Click on the button and paste this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
        total = 0
        Label5.Text = 0
        Label9.Text = 0
        ListBox1.Items.Clear()
        Dim small As Decimal
        Dim medium As Decimal
        Dim large As Decimal

        small = NumericUpDown1.Value * 2.0
        findTotal(small)
        ListBox1.Items.Add(NumericUpDown1.Value & " Small pizza")
        medium = NumericUpDown2.Value * 4.0
        findTotal(medium)
        ListBox1.Items.Add(NumericUpDown2.Value & " Medium pizza")
        large = NumericUpDown3.Value * 6.0
        findTotal(large)
        ListBox1.Items.Add(NumericUpDown3.Value & " Large pizza")
        If CheckBox1.CheckState = 1 Then
            findTotal(0.5)
            ListBox1.Items.Add("Extra cheese")
        End If
        If CheckBox2.CheckState = 1 Then
            findTotal(1.0)
            ListBox1.Items.Add("Extra pepporomni")
        End If
        If CheckBox3.CheckState = 1 Then
            findTotal(1.5)
            ListBox1.Items.Add("Extra anchovies")
        End If

under the top form tag paste the following code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  Dim total As Decimal = 0
    Private Function findTotal(ByVal num1 As Decimal)
        total = total + num1
        If total < 20 Then
            Label5.Text = total.ToString("####0.00")
        Else
            Dim discount As Decimal
            discount = total / 25 'this line has the error
            Dim discountPrice As Decimal
            discountPrice = total - discount
            Label5.ForeColor = Color.Green
            Label5.Text = discountPrice.ToString("####0.00")
            Label9.Text = discount.ToString("####0.00")
        End If
        Return total
    End Function

The program should look like below (not all code showing)




Test and run the program testing that there is a discount when the cost is over £20

Testing Trace Table

If you create the table as below you will find that there is a difference between expected result and actual result. This has revealed that the line discount = total / 25 has errors. Click on the BBC webpage to find the correct code then change it and test until the expected result is the same as the actual result




Improvements

The testing table above is not good enough for a GCSE Computer Science so you have to test more combinations. Create more rows in word and test more combinations E.g 10 large pizzas with extra cheese. Use a calculator to work out what the expected result should be