Is it possible to simplify (refactor) my Pizza program code (VB.Net 2010)?

I just created a Pizza program (for a summer project not related to education) that uses flags to determine what the client wants. Basically a simple pizza ordering program. I was wondering how I used the If over operator and others again for different parameters and variables, is it possible to use multiplication arrays only for VB code ...

I was advised to use constructors in VB.Net, but you had no experience, could you help? Or is there an easier way to create this program ... or a better way to program? How about reducing repetition in code?

Option Strict On
Public Class Form1

Dim CurrentBalance As String
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

    Dim SmallPizza As String
    SmallPizza = "5"
    CurrentBalance = Label4.Text
    If CheckBox1.Checked Then
        ListBox1.Items.Add("Small Pizza")
        Label4.Text = CStr(Val(CurrentBalance) + Val(SmallPizza))
    Else
        ListBox1.Items.Remove("Small Pizza")
        Label4.Text = CStr(Val(CurrentBalance) - Val(SmallPizza))
    End If
End Sub

Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
    Dim MediumPizza As String
    MediumPizza = "7"
    CurrentBalance = Label4.Text
    If CheckBox2.Checked Then
        ListBox1.Items.Add("Medium Pizza")
        Label4.Text = CStr(Val(CurrentBalance) + Val(MediumPizza))
    Else
        ListBox1.Items.Remove("Medium Pizza")
        Label4.Text = CStr(Val(CurrentBalance) - Val(MediumPizza))
    End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    CurrentBalance = Label4.Text
    MessageBox("Total cost: Β£" & CurrentBalance)
End Sub

Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
    Dim LargePizza As String
    LargePizza = "9"
    CurrentBalance = Label4.Text
    If CheckBox3.Checked Then
        ListBox1.Items.Add("Large Pizza")
        Label4.Text = CStr(Val(CurrentBalance) + Val(LargePizza))
    Else
        ListBox1.Items.Remove("Large Pizza")
        Label4.Text = CStr(Val(CurrentBalance) - Val(LargePizza))
    End If
End Sub

Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
    Dim ExtraLargePizza As String
    ExtraLargePizza = "7"
    CurrentBalance = Label4.Text
    If CheckBox4.Checked Then
        ListBox1.Items.Add("Extra Large Pizza")
        Label4.Text = CStr(Val(CurrentBalance) + Val(ExtraLargePizza))
    Else
        ListBox1.Items.Remove("Extra Large Pizza")
        Label4.Text = CStr(Val(CurrentBalance) - Val(ExtraLargePizza))
    End If
End Sub

Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
    Dim Ham As String
    Ham = "1"
    CurrentBalance = Label4.Text
    If CheckBox5.Checked Then
        ListBox1.Items.Add("Ham")
        Label4.Text = CStr(Val(CurrentBalance) + Val(Ham))
    Else
        ListBox1.Items.Remove("Ham")
        Label4.Text = CStr(Val(CurrentBalance) - Val(Ham))
    End If
End Sub

Private Sub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox6.CheckedChanged
    Dim Pineapple As String
    Pineapple = "1"
    CurrentBalance = Label4.Text
    If CheckBox6.Checked Then
        ListBox1.Items.Add("Pineapple")
        Label4.Text = CStr(Val(CurrentBalance) + Val(Pineapple))
    Else
        ListBox1.Items.Remove("Pineapple")
        Label4.Text = CStr(Val(CurrentBalance) - Val(Pineapple))
    End If
End Sub

Private Sub CheckBox7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox7.CheckedChanged
    Dim Bananna As String
    Bananna = "1"
    CurrentBalance = Label4.Text
    If CheckBox7.Checked Then
        ListBox1.Items.Add("Bananna")
        Label4.Text = CStr(Val(CurrentBalance) + Val(Bananna))
    Else
        ListBox1.Items.Remove("Bananna")
        Label4.Text = CStr(Val(CurrentBalance) - Val(Bananna))
    End If
End Sub

Private Sub CheckBox8_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox8.CheckedChanged
    Dim Meat As String
    Meat = "1"
    CurrentBalance = Label4.Text
    If CheckBox8.Checked Then
        ListBox1.Items.Add("Meat")
        Label4.Text = CStr(Val(CurrentBalance) + Val(Meat))
    Else
        ListBox1.Items.Remove("Meat")
        Label4.Text = CStr(Val(CurrentBalance) - Val(Meat))
    End If
End Sub

Private Sub CheckBox9_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox9.CheckedChanged
    Dim ExtraCheese As String
    ExtraCheese = "1"
    CurrentBalance = Label4.Text
    If CheckBox9.Checked Then
        ListBox1.Items.Add("Extra Cheese")
        Label4.Text = CStr(Val(CurrentBalance) + Val(ExtraCheese))
    Else
        ListBox1.Items.Remove("Extra Cheese")
        Label4.Text = CStr(Val(CurrentBalance) - Val(ExtraCheese))
    End If
End Sub

Private Sub CheckBox10_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox10.CheckedChanged
    Dim Pepperoni As String
    Pepperoni = "1"
    CurrentBalance = Label4.Text
    If CheckBox10.Checked Then
        ListBox1.Items.Add("Pepperoni")
        Label4.Text = CStr(Val(CurrentBalance) + Val(Pepperoni))
    Else
        ListBox1.Items.Remove("Pepperoni")
        Label4.Text = CStr(Val(CurrentBalance) - Val(Pepperoni))
    End If
End Sub

Private Sub CheckBox11_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox11.CheckedChanged
    Dim Special As String
    Special = "1"
    CurrentBalance = Label4.Text
    If CheckBox11.Checked Then
        ListBox1.Items.Add("Special")
        Label4.Text = CStr(Val(CurrentBalance) + Val(Special))
    Else
        ListBox1.Items.Remove("Special")
        Label4.Text = CStr(Val(CurrentBalance) - Val(Special))
    End If
End Sub

Private Sub CheckBox12_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox12.CheckedChanged
    Dim Pickup As String
    Pickup = "1"
    CurrentBalance = Label4.Text
    If CheckBox12.Checked Then
        ListBox1.Items.Add("Pickup")
        Label4.Text = CStr(Val(CurrentBalance) - Val(Pickup))
    Else
        Label4.Text = CStr(Val(CurrentBalance) + Val(Pickup))
        ListBox1.Items.Remove("Pickup")
    End If
End Sub

Private Sub CheckBox13_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox13.CheckedChanged
    Dim Deliver As String
    Deliver = "5"
    CurrentBalance = Label4.Text
    If CheckBox13.Checked Then
        ListBox1.Items.Add("Deliver")
        Label4.Text = CStr(Val(CurrentBalance) + Val(Deliver))
    Else
        Label4.Text = CStr(Val(CurrentBalance) - Val(Deliver))
        ListBox1.Items.Remove("Deliver")
    End If
End Sub

Private Sub ChesseToastToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChesseToastToolStripMenuItem.Click
    Dim CheeseToast As String
    CheeseToast = "8"
    CurrentBalance = Label4.Text
    ListBox1.Items.Add("Cheese Toast")
    Label4.Text = CStr(Val(CurrentBalance) + Val(CheeseToast))
End Sub

Private Sub GarlicToastToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GarlicToastToolStripMenuItem.Click
    Dim GarlicToast As String
    GarlicToast = "11"
    CurrentBalance = Label4.Text
    ListBox1.Items.Add("Garlic Toast")
    Label4.Text = CStr(Val(CurrentBalance) + Val(GarlicToast))
End Sub

Private Sub BreadSticksToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BreadSticksToolStripMenuItem.Click
    Dim Breadsticks As String
    Breadsticks = "14"
    CurrentBalance = Label4.Text
    ListBox1.Items.Add("Bread Sticks")
    Label4.Text = CStr(Val(CurrentBalance) + Val(Breadsticks))
End Sub

Private Sub CashToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CashToolStripMenuItem.Click
    ListBox1.Items.Add("Paying by Cash")
End Sub

Private Sub ChequeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChequeToolStripMenuItem.Click
    ListBox1.Items.Add("Paying by Cheque")
End Sub

Private Sub DebitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DebitToolStripMenuItem.Click
    ListBox1.Items.Add("Paying by Debit Card")
End Sub

Private Sub CreditCardToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreditCardToolStripMenuItem.Click
    ListBox1.Items.Add("Paying by Crebit Card")
End Sub

Private Sub VoucherToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VoucherToolStripMenuItem.Click
    Dim Voucher As String
    Voucher = "5"
    CurrentBalance = Label4.Text
    ListBox1.Items.Add("Voucher")
    Label4.Text = CStr(Val(CurrentBalance) - Val(Voucher))
End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
    End
End Sub

Private Sub MessageBox(ByVal p1 As String)
    Throw New NotImplementedException
End Sub

Help would be greatly appreciated.

+3
source share
4

VB.NET:

  • Strict on (- )
  • : Dim s As String = "MyString"
  • Microsoft.VisualBasic -Namespace (, MessageBox MsgBox). *

: Dictionary(Of String, Decimal), .

Private prices As New Dictionary(Of String, Decimal)
Private balance As Decimal = 0D

' Add this to the constructor '
prices.Add("Ham", 1D)
prices.Add("Banana", 1D)
prices.Add("Pineapple", 1D)
' etc. ... '

For Each item As KeyValuePair(Of String, Decimal) In prices
    CheckedListBox1.Add(item.Key)
Next

' End of Constructor '

' Don't forget to give your Controls meaningful names!
Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
    If e.NewValue Then
        balance += prices(CheckedListBox1.Item(e.Index))
    Else
        balance -= prices(CheckedListBox1.Item(e.Index))
    End if

    Label4.Text = balance.ToString("C2")
End Sub

*: , Console-Projects, Forms -Namespace. , .

+5

/ : name, price, checkboxInstance

  • name (, ,...)

  • Label4.Text

  • checkboxInstance , :

, :

  If checkboxInstance.Checked Then
       ListBox1.Items.Add(name)
       Label4.Text = Val(CurrentBalance) - Val(price)
  Else
      Label4.Text = Val(CurrentBalance) + Val(price)
      ListBox1.Items.Remove(name)
  End If

VB.NET. , , , .

: , . "tag" , , 1, checkbox2,.. Visual Studio , (myCheckboxes [0], myCheckboxes [1],...). http://www.thevbprogrammer.com/VBNET_09/09-05-ControlArrays.htm - .

+3

( ) , RadioButtons GroupBoxes. - , CalculateCost Sub, , . , . ExtraLargePizza String - Integer ( Decimal) - Val (...) .

0

You seem to have many repetitions in your code file. Perhaps you should consider using the factory template to make it easier to create different types of pizza you create.

Good luck and hope this helps some.

0
source

Source: https://habr.com/ru/post/1754019/


All Articles