Zebra0.com

visual-basic pizza

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.

'Programmer: Janet Joy
'Pizza Order
Public Class Form1
    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.ChkLstToppings.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.ChkLstToppings.CheckedItems.Count - 1
            toppings += ChkLstToppings.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 ChkLstToppings.CheckedItems.Count = 1 Then
            display += " and 1 topping: " & toppings
        End If
        If ChkLstToppings.CheckedItems.Count > 1 Then
            display += " and " & ChkLstToppings.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

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        calculate()
    End Sub
End Class

NEXT: Designing the form