2010-07-10 11 views
3

J'ai créé un programme simplement Pizza (pour un projet d'été non lié à l'éducation) qui utilise des cases à cocher pour déterminer ce que le client veut. Fondamentalement, un simple programme de commande de pizza. Je me demandais comme j'ai utilisé l'instruction If sur et autre encore pour différents paramètres et variables, est-il possible d'utiliser multiplier les tableaux simplement le code VB ...Est-il possible de simplifier (refactoriser) mon code de programme Pizza (VB.Net 2010)?

On m'a conseillé d'utiliser Constructeurs dans VB.Net , mais n'ont aucune expérience actuelle, pourriez-vous aider? Ou y a-t-il une façon plus simple de créer ce programme ... ou une meilleure façon de programmer? Qu'en est-il de diminuer la répétition dans le 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 

L'aide serait grandement appréciée.

+0

Cela semble assez familier à un projet de cours OU il y a quelques années. Projet d'été ou devoirs? – blowdart

+0

Désolé de dire mais non, Juste un petit projet que je me suis mis à faire à mon propre rythme ... Sinon, je m'ennuierai plutôt pendant l'été. – Janitor

+0

Assez juste :) Je me demandais s'ils avaient mis à jour leurs bits Java! – blowdart

Répondre

5

Certains principes de VB.NET:

  • toujours tour Option Strict sur (je l'ai vu des horreurs à cause de cela)
  • Vous pouvez attribuer directement des valeurs aux variables comme ceci: Dim s As String = "MyString"
  • vous une faveur et déposez les Microsoft.VisualBasic -nameSpace de votre projet et commencer à utiliser les nouvelles classes (comme MessageBox au lieu de MsgBox) *

à votre projet. Un pari Le moyen de refactoring pourrait être simplement de tout bourrer dans un Dictionary(Of String, Decimal), cela pourrait fonctionner mieux.

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 

*: Comme Konrad Rudolph a souligné, cela pourrait être une bonne solution pour console-projets, si vous ne voulez pas inclure le Forms -nameSpace. Cependant, je ne suis pas d'accord là-dessus.

+0

Je ne suis pas d'accord avec 'MsgBox' vs' MessageBox' (en particulier puisque ce dernier nécessite une référence à 'Microsoft.Windows.Forms' qui n'est pas inclus par défaut dans les projets de console. –

+0

@Konrad Rudolph: Mh, vrai, mais je pense que c'est une question de goût personnel, je préfère inclure l'espace de nommage 'Forms' puis l'ancien VB6, et je n'utiliserais jamais un MessageBox dans une console. -Application ... mais ce n'est peut-être que moi – Bobby

+0

Je dois être d'accord sur les deux aspects: le 'MsgBox' dans les applications console que j'utilise parfois pour le débogage (par exemple quand je ne veux pas perturber le flux normal de la console) ...). –

3

Vous pouvez faire une fonction pour ajouter/supprimer des éléments avec des paramètres: nom, prix, checkBoxInstance

  • nom contiendrait le nom d'un élément (Bon, Pick-up, ...)

  • prix serait utilisé pour changer la valeur dans Label4.Text

  • checkBoxInstance pourrait vous aider à décider quelle opération à faire:

donc le corps de la fonction ressemblerait à ceci:

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 

Je ne me souviens pas vraiment de la syntaxe VB.NET. Mais comme c'est votre devoir je suppose qu'il est préférable de donner un indice plutôt qu'un code.

EDIT: Il y a encore une chose à faire pour simplifier radicalement le code. Vous pouvez utiliser la propriété "tag" de case à cocher où vous enregistreriez le nom des marchandises et au lieu d'avoir checkbox1, checkbox2, .. vous pouvez dans le concepteur de Visual Studio faire un tableau de cases à cocher afin que vous puissiez les indexer (myCheckboxes [0] , myCheckboxes [1], ...). http://www.thevbprogrammer.com/VBNET_09/09-05-ControlArrays.htm - Consultez ce site Web.

+0

Merci, je vais jeter un oeil à des tableaux ... Très apprécié. – Janitor

+0

Juste par curiosité: Avez-vous marqué cette réponse par la coche? Il a été coché hier et maintenant ce n'est pas le cas. C'est intéressant parce que décoller n'est pas possible autant que je sache. –

0

Certaines des options (taille et mode de paiement) semblent mutuellement exclusives, donc vous seriez mieux placé pour placer ces boutons RadioBox dans deux GroupBox séparés. Je suggère également de ne pas effectuer de calculs dans les mises à jour, et à la place avoir un CalculateCost Sub que tous les événements reportent à quand le coût a besoin de recalculer. Vous devriez éviter de définir par ex. ExtraLargePizza en tant que chaîne - vous pouvez simplement en faire un entier (ou une décimale) - vous n'aurez donc pas besoin de l'inclure dans Val (...) lorsque vous effectuerez vos calculs.

0

Vous semblez avoir beaucoup de répétitions dans votre fichier de code. Vous pouvez envisager d'utiliser un factory pattern pour faciliter la création des différents types de pizza que vous créez.

Bonne chance et j'espère que cela vous aidera.