The Pizza Project

We will do a large project in several steps.
After we have designed the form, the only thing we need to do is calculate and display the price.

Public Class Form1

    Private Sub BtnCalc_Click(sender As System.Object, e As System.EventArgs) Handles BtnCalc.Click
        calculate()
    End Sub
    Private Sub calculate()
        Dim j As Integer
        Dim cost As Double
        Dim order As String = "" 'will say small, medium, or large
        Dim toppings As String = "" 'will contain list of toppings
        Dim crust As String = "" 'will say thin or thick
        'Because the tag has the price, we can use the tag for the selected size as the base cost
        If RadSmall.Checked Then
            cost = RadSmall.Tag 'base price for a small pizza
            order = "small"
        ElseIf RadMedium.Checked Then
            cost = RadMedium.Tag 'base price for a medium pizza
            order = "medium"
        Else
            cost = RadLarge.Tag
            order = "large" 'base price for a large pizza
        End If
        If RadThick.Checked Then
            crust = " thick crust"
        Else
            crust = " thin crust"
        End If
        If ChkCheese.Checked Then cost += ChkCheese.Tag 'add cost of extra cheese
        cost += Me.CheckBoxToppings.CheckedItems.Count 'add $1 per topping or * cost per topping
        'loop through all of the selected toppings to make list of toppings
        For j = 0 To Me.CheckBoxToppings.CheckedItems.Count - 1
            toppings += CheckBoxToppings.CheckedItems.Item(j) & " " 'space between
        Next
        'Notice how the display is built one phrase at a time
        Dim display As String = "You ordered a " & order & crust & " pizza"
        If ChkCheese.Checked Then display += " with extra cheese"
        If CheckBoxToppings.CheckedItems.Count = 1 Then
            display += " and 1 topping: " & toppings
        End If
        If CheckBoxToppings.CheckedItems.Count > 1 Then
            display += " and " & CheckBoxToppings.CheckedItems.Count & " toppings: " & toppings
        End If
        Me.RichTextBox1.Text = display
        'chr(13) is the new line character.
        Me.RichTextBox1.AppendText(Chr(13) & "Your total is " & FormatCurrency(cost))
    End Sub
End Class



INDEX, The Pizza Project, Designing the form, Write the code to calculate the price, Read the list of toppings from a file
Next lesson: Files in Visual Basic

Copyright © Zebra0.com
All rights reserved worldwide.

 
 

Write the code to calculate the price