Question a little help calculate price?

Michael_85

Member
Joined
Mar 15, 2009
Messages
9
Programming Experience
Beginner
Hi i have wrote code to output a message box tell user what they have seleted for pizza menu like thin base etc but i would now like to tell them how much the pizza will cost . so i would like to assign price to each items like deepPan cheese and so on i will show you the code i have wrote already if i have not followed rules of posting here or there is not enough detail please ask for anything else
VB.NET:
Private Sub btnOrderNow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOrderNow.Click

' Declare variable required
Dim m_strToppings As String 'To hold the toppings requested
Dim m_strPizzaType As String 'To identify the pizza type' chosen

m_strToppings = "" 'Set the string variable to empty
' Test for pizza type.
If rdoMargherita.Checked = True Then
m_strPizzaType = "Margherita"
Else
If rdoHawaiian.Checked = True Then
m_strPizzaType = "Hawaiian"
Else
If rdoSpicyBeef.Checked = True Then
m_strPizzaType = "SpicyBeef"
Else
If rdoPolloFunghi.Checked = True Then
m_strPizzaType = "PolloFunghi"
Else
m_strPizzaType = "Tropicana"
End If
End If
End If
End If
' Test which CHECK BOXES have been ticked and, if they
' have, add them to the list.
' N.B. If the user ticks a Check Box, its Value Property is

' set to 1. Also, the Caption Property value is added to
' the string variable.
If chkCheese.CheckState = 1 Then
m_strToppings = m_strToppings & chkCheese.Text & " "
End If
If chkPineapple.CheckState = 1 Then
m_strToppings = m_strToppings & chkPineapple.Text & " "
End If
If chkOnions.CheckState = 1 Then
m_strToppings = m_strToppings & chkOnions.Text & " "
End If
If chkPeppers.CheckState = 1 Then
m_strToppings = m_strToppings & chkPeppers.Text
End If
' Test which Base option button was clicked using the
' Checked property.
' N.B. Only ONE of the buttons can have the value TRUE!
If Me.rdoThin.Checked = True Then
MessageBox.Show("You chose " & m_strPizzaType & "with a Thin Crust and toppings :" & m_strToppings,"Pizza Types", MessageboxButtons.OK)
Else
MessageBox.Show("You chose " & m_strPizzaType & " with aDeep Crust and toppings :" &m_strToppings, "Pizza Types" , MessageBoxButtons.OK)
End If
End Sub
End Class
 
Last edited by a moderator:
A update

I was going about it all wrong as am sure you saw i have uppdated my code sorry i took so long i was in work

Public Class frmPizzatoppings2



Private Sub btnOrderNow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOrderNow.Click
' Declare variable required

Dim m_strToppings As String
Dim m_strPizzaType As String
Dim TotalCost As Decimal

' chosen
m_strToppings = "" 'Set the string variable to empty
' Test for pizza type.
If rdoMargherita.Checked = True Then
m_strPizzaType = "Margherita"
TotalCost = TotalCost + 0.5
Else
If rdoHawaiian.Checked = True Then
m_strPizzaType = "Hawaiian"
TotalCost = TotalCost + 0.5
Else
If rdoSpicyBeef.Checked = True Then
m_strPizzaType = "SpicyBeef"
TotalCost = TotalCost + 0.5
Else
If rdoPolloFunghi.Checked = True Then
m_strPizzaType = "PolloFunghi"
TotalCost = TotalCost + 0.5
Else
m_strPizzaType = "Tropicana"
TotalCost = TotalCost + 0.5
End If
End If
End If
End If

' Test which CHECK BOXES have been ticked and, if they
' have, add them to the list.
' N.B. If the user ticks a Check Box, its Value Property is
' set to 1. Also, the Caption Property value is added to
' the string variable.
If chkCheese.CheckState = 1 Then
m_strToppings = m_strToppings & chkCheese.Text & " "
TotalCost = TotalCost + 0.75
End If
If chkPineapple.CheckState = 1 Then
m_strToppings = m_strToppings & chkPineapple.Text & " "
TotalCost = TotalCost + 0.75
End If
If chkOnions.CheckState = 1 Then
m_strToppings = m_strToppings & chkOnions.Text & " "
TotalCost = TotalCost + 0.75
End If
If chkPeppers.CheckState = 1 Then
m_strToppings = m_strToppings & chkPeppers.Text
TotalCost = TotalCost + 0.75
End If
' Test which Base option button was clicked using the
' Checked property.
' N.B. Only ONE of the buttons can have the value TRUE!

If rdoThinCrust.Checked = True Then
TotalCost = TotalCost + 8.5
MessageBox.Show("You Chose" & m_strPizzaType & " With a Thin Crust And toppings : " & m_strToppings, "Pizza Types" & TotalCost, MessageBoxButtons.OK)
Else
rdoDeep.Checked = True
TotalCost = TotalCost + 9.5
MessageBox.Show("You Chose" & m_strPizzaType & " with A Deep Crust and toppings : " & m_strToppings, "Pizza Types" & TotalCost, MessageBoxButtons.OK)

End If

End Sub

I am try to add of the all of the values to total cost and display them in the they message box

End Class
 
If you look in the properties of a control you will see it has a Tag property. The Tag property takes an object so you can use pretty much anything you want for it's value. You can set this in design time or at run time. At run time you might use:

VB.NET:
rdoThinCrust.Tag = 8.5
 
this is helpful

but then how do i add it to the total cost and why does the code i have wrote not display the the total cost i have added it as you can to the message box. am i adding it wrong please let me konw thaks
 
VB.NET:
Dim TotalCost As Decimal
To
VB.NET:
Dim TotalCost As Integer
Try that

If that doesn't work
VB.NET:
If rdoThinCrust.Checked = True Then
TotalCost = TotalCost + 8.5
MessageBox.Show("You Chose" & m_strPizzaType & " With a Thin Crust And toppings : " & m_strToppings, "Pizza Types" & CStr(TotalCost), MessageBoxButtons.OK)
Else
rdoDeep.Checked = True
TotalCost = TotalCost + 9.5
MessageBox.Show("You Chose" & m_strPizzaType & " with A Deep Crust and toppings : " & m_strToppings, "Pizza Types" & CStr(TotalCost), MessageBoxButtons.OK)
 
You should leave the TotalCost as Decimal or a Double, an Integer will trunctate your decimals. Your TotalCost is displaying in the Title bar of the Message Box. Not sure what you mean by:

but then how do i add it to the total cost

If you are talking about the Tag code that I provided then, you would have to cast the value from the Tag to a decimal.

VB.NET:
  Dim TotalCost As Decimal
        ' chosen
        m_strToppings = "" 'Set the string variable to empty
        ' Test for pizza type.
        If rdoMargherita.Checked = True Then
            m_strPizzaType = "Margherita"
            [B]TotalCost += Convert.ToDecimal(rdoMargherita.Tag)[/B]
        Else
            If rdoHawaiian.Checked = True Then
                m_strPizzaType = "Hawaiian"
                [B]TotalCost += Convert.ToDecimal(rdoHawaiian.Tag)[/B] 
           Else
                If rdoSpicyBeef.Checked = True Then
                    m_strPizzaType = "SpicyBeef"
                    [B]TotalCost += Convert.ToDecimal(rdoSpicyBeef.Tag)[/B]
                Else
                    If rdoPolloFunghi.Checked = True Then
                        m_strPizzaType = "PolloFunghi"
                        [B]TotalCost += Convert.ToDecimal(rdoPolloFunghi.Tag)[/B]
                    Else
                        m_strPizzaType = "Tropicana"
                        [B]TotalCost += Convert.ToDecimal(rdoTropicana.Tag)[/B]
                    End If
                End If
            End If
        End If

There are different ways to handle this situation.
 
Maybe not the best solution, but shows a bit OO ;)

Form with ComboBox (cboBasePizza), CheckedListBox (clbToppings) and a TextBox (txtCurrentPrice). Code:
VB.NET:
Public Class Form1

    Private WithEvents currentPizza As Pizza = Nothing

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' load pizzas:
        cboBasePizza.Items.Add(New Pizza("Small", 3.99))
        cboBasePizza.Items.Add(New Pizza("Medium", 5.99))
        cboBasePizza.Items.Add(New Pizza("Large", 7.99))
        ' load toppings:
        With clbToppings
            .Visible = False
            .Items.Add(New Topping("Cheese", 0.75))
            .Items.Add(New Topping("Salami", 1.25))
            .Items.Add(New Topping("Tomatoes", 0.25))
        End With
    End Sub

    Private Sub clbToppings_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles clbToppings.ItemCheck
        Dim t As Topping = clbToppings.Items(e.Index)
        If e.CurrentValue = CheckState.Checked And e.NewValue = CheckState.Unchecked Then
            currentPizza.RemoveTopping(t)
        ElseIf e.CurrentValue = CheckState.Unchecked And e.NewValue = CheckState.Checked Then
            currentPizza.AddTopping(t)
        End If
    End Sub

    Private Sub cboBasePizza_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboBasePizza.SelectedIndexChanged
        currentPizza = CType(sender, ComboBox).SelectedItem
        currentPizza.RemoveAllToppings()
        If Not clbToppings.Visible Then clbToppings.Visible = True
        For Each t As Topping In clbToppings.CheckedItems
            currentPizza.AddTopping(t)
        Next
    End Sub

    Private Sub currentPizza_PriceChanged(ByVal CurrentPrice As Decimal) Handles currentPizza.PriceChanged
        txtCurrentPrice.Text = CurrentPrice
    End Sub

End Class
The "pizza" class:
VB.NET:
Public Class Topping

    Public ReadOnly Name As String = ""
    Public ReadOnly Price As Decimal = 0

    Public Sub New(ByVal _name As String, ByVal _price As Decimal)
        Name = _name
        Price = _price
    End Sub

    Public Overrides Function ToString() As String
        Return Name & " (" & Price.ToString & ")"
    End Function

End Class

Public Class Pizza

    Private Toppings As New List(Of Topping)
    Public ReadOnly Name As String = ""
    Public ReadOnly Price As Decimal = 0

    Public Event PriceChanged(ByVal CurrentPrice As Decimal)

    Public Sub New(ByVal _name As String, ByVal _price As String)
        Name = _name
        Price = _price
    End Sub

    Public Overrides Function ToString() As String
        Return Name
    End Function

    Public Sub AddTopping(ByVal _topping As Topping)
        If Not Toppings.Contains(_topping) Then
            Toppings.Add(_topping)
            CalculatePrice()
        End If
    End Sub

    Public Sub RemoveTopping(ByVal _topping As Topping)
        If Toppings.Contains(_topping) Then
            Toppings.Remove(_topping)
            CalculatePrice()
        End If
    End Sub

    Public Sub RemoveAllToppings()
        Toppings.Clear()
        CalculatePrice()
    End Sub

    Public Sub CalculatePrice()
        Dim pr As Decimal = Price
        For Each t As Topping In Toppings
            pr += t.Price
        Next
        RaiseEvent PriceChanged(pr)
    End Sub

End Class
 
I like it picoflop. The project that Michael_85 proposed sounds like a homework type of assignment, so your solution might be out of bounds. :) but i still like it.
 
Update On Code

Hi al Thank you All very mush for the replys it good to now peole will help some one new i have dated my code before i got to read your replys it will show the price of rdothin but will not add on the rest like rdoHawaiian or chkCheese when i print out messagebox this is what i have now thanks

VB.NET:
Public Class frmPizzatoppings2



    Private Sub btnOrderNow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOrderNow.Click
        ' Declare variable required

        Dim m_strToppings As String
        Dim m_strPizzaType As String
        Dim TotalCost As Decimal

        ' chosen
        m_strToppings = "" 'Set the string variable to empty
        ' Test for pizza type.
        If rdoMargherita.Checked = True Then
            m_strPizzaType = "Margherita"
            TotalCost = 0.5D

        Else
            If rdoHawaiian.Checked = True Then
                m_strPizzaType = "Hawaiian"
                TotalCost = 0.5D

            Else
                If rdoSpicyBeef.Checked = True Then
                    m_strPizzaType = "SpicyBeef"
                    TotalCost = 0.5D

                Else
                    If rdoPolloFunghi.Checked = True Then
                        m_strPizzaType = "PolloFunghi"
                        TotalCost = 0.5D

                    Else
                        m_strPizzaType = "Tropicana"
                        TotalCost = 0.5D

                    End If
                End If
            End If
        End If

        ' Test which CHECK BOXES have been ticked and, if they
        ' have, add them to the list.
        ' N.B. If the user ticks a Check Box, its Value Property is
        ' set to 1. Also, the Caption Property value is added to
        ' the string variable.

        If chkCheese.CheckState = 1 Then
            m_strToppings = m_strToppings & chkCheese.Text & " "
            TotalCost = 0.7D


        End If
        If chkPineapple.CheckState = 1 Then
            m_strToppings = m_strToppings & chkPineapple.Text & " "
            TotalCost = 0.7D


        End If
        If chkOnions.CheckState = 1 Then
            m_strToppings = m_strToppings & chkOnions.Text & " "
            TotalCost = 0.7D


        End If
        If chkPeppers.CheckState = 1 Then
            m_strToppings = m_strToppings & chkPeppers.Text
            TotalCost = 0.7D


        End If
        ' Test which Base option button was clicked using the
        ' Checked property.
        ' N.B. Only ONE of the buttons can have the value TRUE!

        If rdoThin.Checked = True Then
            TotalCost = +8.5D
            MessageBox.Show(" The Total Cost is £ " & CStr(TotalCost) & MessageBoxButtons.OK)
            MessageBox.Show("You Chose " & m_strPizzaType & " With a Thin Crust And toppings : " & m_strToppings, "Pizza Types" & MessageBoxButtons.OK)
        Else
            rdoDeep.Checked = True
            TotalCost = +9.5D
            MessageBox.Show(" The Total Cost is £ " & CStr(TotalCost) & MessageBoxButtons.OK)
            MessageBox.Show("You Chose " & m_strPizzaType & " with A Deep Crust and toppings : " & m_strToppings, "Pizza Types" & MessageBoxButtons.OK)

        End If

    End Sub

End Class
 
Sssshhhh You might be right

I like it picoflop. The project that Michael_85 proposed sounds like a homework type of assignment, so your solution might be out of bounds. :) but i still like it.

It not like i anit try i have been in hundred different site reading, how to write this code read all my note i am just staring this but i am glad you still like lol:p
 
In your PizzaType radio buttons change the nested if statements to

VB.NET:
IF XX = 1 Then
            m_strPizzaType = "Margherita"
            decTotalCost += 0.5D
ElseIF X = 1 Then
     'do something
ElseIF X = 1 Then
     'do something
Else 
    'Default do something
End If

In the topping sections its not accumatlating. For example if you check to toppings it is only adding one. Change to decTotalCost += 0.7D in each of the statements

VB.NET:
        If chkCheese.CheckState = 1 Then
            m_strToppings [COLOR="red"]&=[/COLOR] chkCheese.Text & " "
            decTotalCost [COLOR="red"]+= [/COLOR]0.7D
        End If

Likewise change
TotalCost = +9.5D
to
TotalCost += 9.5D
 
Back
Top