'Calculating from properties of different objects OOP VB

I am practicing OOP using VB. I creating a simple kiosk like program for a sandwich shop which the user will be able to select different items from i.e sandwiches, drinks, sides. I have created a class for the sandwiches:

Public Class sandwich
 Public Sub sandwich(sandwichName As String, sandwichDescription As String, sandwichCost As Decimal)
        Me.sandwichName = sandwichName
        Me.sandwichDescription = sandwichDescription
        Me.sandwichDescription = sandwichCost
        'me. refers to the current instance of the class
    End Sub

And the objects for these sandwiches with the event handler

 Private Sub btnChickpeaPlus_Click(sender As Object, e As EventArgs) Handles btnChickpeaPlus.Click
        Dim garlicButterChkpea As New sandwich
        garlicButterChkpea.sandwichName = "Garlic Butter Chickpea"
        garlicButterChkpea.sandwichCost = 7.99
        garlicButterChkpea.sandwichDescription = "Mashed chickpeas on wheat bread with sliced tomato, red onion, lettuce and garlic sauce"
        ListBox1.Items.Add(1 & " " & garlicButterChkpea.sandwichName & " " & garlicButterChkpea.sandwichCost)
        'the name and cost of the sandwich will be displayed in a listbox when item is selected
        txtChickpea.Text += 1
        'adds item by one whenever the plus sign is clicked
    End Sub


    Private Sub btnAvocadoPlus_Click(sender As Object, e As EventArgs) Handles btnAvocadoPlus.Click
        Dim avocadoReuben As New sandwich
        avocadoReuben.sandwichName = "Avocado Reuben"
        avocadoReuben.sandwichDescription = "Avocado with saukeraut, sliced tomatoes on Rye bread with vegan mayo"
        avocadoReuben.sandwichCost = 7.99
        ListBox1.Items.Add(1 & " " & avocadoReuben.sandwichName & " " & avocadoReuben.sandwichCost)
        'the name and cost of the sandwich will be displayed in a listbox when item is selected
        txtAvocado.Text += 1
        'adds item by one whenever the plus sign is clicked
    End Sub


    Private Sub btnTunaPlus_Click(sender As Object, e As EventArgs) Handles btnTunaPlus.Click
        Dim tunaMayo As New sandwich
        tunaMayo.sandwichName = "Tuna Mayo"
        tunaMayo.sandwichDescription = "Wild Tuna in olive oil with vegan mayo with organic
                                        lettuce and diced red onion"
        tunaMayo.sandwichCost = 8.99
        ListBox1.Items.Add("1" & " " & tunaMayo.sandwichName & " " & tunaMayo.sandwichCost)
        'the name and cost of the sandwich will be displayed in a listbox when item is selected
        txtTuna.Text += 1
        'adds item by one whenever the plus sign is clicked
    End Sub

I am currently calculating the total price of selected objects using this code:

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        tunaCost = 8.99 * Val(txtTuna.Text)
        steakCost = 9.99 * Val(txtSteak.Text)
        chickpeaCost = 7.99 * Val(txtChickpea.Text)
        avocadoCost = 7.99 * Val(txtAvocado.Text)
        lemonadeCost = 4.99 = Val(txtboxLemonade.Text)
        cokeCost = 2.99 = Val(txtCoke.Text)
        txtBoxTotal.Text = tunaCost + steakCost + avocadoCost + chickpeaCost + lemonadeCost + cokeCost
        'when the complete order cost is clicked the total cost of the order will be added
        lblTotal.Text = txtBoxTotal.Text
    End Sub

I had to declare separate variables such as cokeCost because I couldn't figure out how to calculate the costs from the objects themselves. I was also having trouble creating a drinks class so I just declared the drinks under the sandwich class properties like so:

Private Sub btnLemonadePlus_Click(sender As Object, e As EventArgs) Handles btnLemonadePlus.Click
        Dim lemonade As New sandwich
        lemonade.sandwichName = "Lemonade"
        lemonade.sandwichCost = 4.99
        lemonade.sandwichDescription = "Freshly squeezed lemonade"
        ListBox1.Items.Add(1 & " " & lemonade.sandwichName & " " & lemonade.sandwichCost)
        txtboxLemonade.Text += 1
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnCokePlus.Click
        Dim coke As New sandwich
        coke.sandwichName = "Coke"
        coke.sandwichCost = 2.99
        coke.sandwichDescription = "Orignial Coca-Cola in 500ml bottle"
        ListBox1.Items.Add(1 & " " & coke.sandwichName & " " & coke.sandwichCost)
        txtCoke.Text += 1
    End Sub

I think I could probably just change the parent class name to just item and have the drinks and sandwiches inherit off of that.

Can anyone advice me on how to make the code simpler and how to create a function that could calculate the total cost of selected items instead of creating more variables please?



Solution 1:[1]

I think I could probably just change the parent class name to just item and have the drinks and sandwiches inherit off of that.

Yes, you should do that. I suggest to create 2 base classes, one for food and one for drinks. And if you want to go fancy with practicing OOP, consider to define Interfaces that your objects implement.

As for calculating costs, add a method to your object that does this, This one lets you adjust the item cost "on the fly" if needed, e.g. in the evening sell the last sandwiches at a discount to clear your stock:

Public Class sandwich
  Public Function GetTotal(ByVal numberOf As Int32, Optional price As Currency = 0) As Currency
      ' Price differs from normal item price? Could be an item at discount or a premium
      If price > 0 Then
         Return numberOf * price
      Else
         Return numberOf * Me.sandwichCost
      End If
   End Function
End Class

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hel O'Ween