问题描述
您好,用户!
我目前正在为一个大学项目工作,并且正在尝试创建购物车.到目前为止,它已经完全可用,但是我觉得我的代码很糟糕.我相信必须有一种更简单(或更复杂)的方法来执行我的代码. 本质上,它会一遍又一遍地重复自身,但变量会略有变化
I'm currently working on a project for college and I am attempting to create a shopping cart. Thus far, it is completely functional but I feel like my code is bad. I believe there has to be a much simpler (or complex) method for doing what my code does. It essentially repeats itself over and over with a slight change in variable
'Message for unselected item amount - Below.
Dim itemAmountError As String = "Please select an amount from the dropdown menu above the 'ADD' button"
'Message for unselected item - Below.
Dim itemSelectError As String = "Please select an item from the list to add"
'存储添加到购物车中的商品总数. Dim storeTotal As Double = 0 '存储饮料价格. 倍数的昏暗的蒸馏水= 0.8'蒸馏水"的价格 倍数的暗淡汽水= 1汽水"的价格 昏暗的橙汁加倍= 1.3'橙汁"的价格 点心的可口可乐加倍= 1.2'可口可乐"的价格 百事可乐的Dim百事可乐= 1.19百事可乐"的价格 Dim drPepper为Double = 1.05'Dr Pepper'价格 储杂货价格. 昏暗的香蕉四倍= 0.7'香蕉(4)"的价格 暗淡的苹果十二个双倍= 1'苹果(12)"的价格 Dim packGrapes作为Double = 1.2'Grapes'的价格 将Double的DimLettuce设为Dim = 0.4'Lettuce'的价格 Dim largeCucumber Double = 0.35'黄瓜的价格 储存冻结的价格. Double的Dim quornMince = 2.1'Quorn Mince'的价格 昏暗的十二个汉堡双倍= 1.8'汉堡(12)"的价格 昏暗的冷冻香肠加倍= 2'香肠"的价格 两倍的Dim yrkPudddings ="Yorkshire Puddings"的价格 两倍的Dim onionRings =洋葱圈"的价格 子addItems() '如果是饮料声明-选择特定元素后,将此值添加到lstItemValue 如果lstBeverage.SelectedIndex = 0然后 lstItemValue.Items.Add(stillWater * cboBeverageAmount.SelectedItem) storeTotal = storeTotal +(stillWater * cboBeverageAmount.SelectedItem) ElseIf lstBeverage.SelectedIndex = 1然后 lstItemValue.Items.Add(sparklingWater * cboBeverageAmount.SelectedItem) storeTotal = storeTotal +(气泡水* cboBeverageAmount.SelectedItem) ElseIf lstBeverage.SelectedIndex = 2然后 lstItemValue.Items.Add(orangeJuice * cboBeverageAmount.SelectedItem) storeTotal = storeTotal +(orangeJuice * cboBeverageAmount.SelectedItem) ElseIf lstBeverage.SelectedIndex = 3然后 lstItemValue.Items.Add(可口可乐* cboBeverageAmount.SelectedItem) storeTotal = storeTotal +(可口可乐* cboBeverageAmount.SelectedItem) ElseIf lstBeverage.SelectedIndex = 4然后 lstItemValue.Items.Add(pepsiCola * cboBeverageAmount.SelectedItem) storeTotal = storeTotal +(百事可乐* cboBeverageAmount.SelectedItem) ElseIf lstBeverage.SelectedIndex = 5然后 lstItemValue.Items.Add(drPepper * cboBeverageAmount.SelectedItem) storeTotal = storeTotal +(drPepper * cboBeverageAmount.SelectedItem) 万一 '如果是杂货声明-选择特定元素后,将此值添加到lstItemValue 如果lstGroceries.SelectedIndex = 0然后 lstItemValue.Items.Add(bananasFour * cboGroceriesAmount.SelectedItem) storeTotal = storeTotal +(香蕉四* cboGroceriesAmount.SelectedItem) ElseIf lstGroceries.SelectedIndex = 1然后 lstItemValue.Items.Add(applesTwelve * cboGroceriesAmount.SelectedItem) storeTotal = storeTotal +(applesTwelve * cboGroceriesAmount.SelectedItem) ElseIf lstGroceries.SelectedIndex = 2然后 lstItemValue.Items.Add(packGrapes * cboGroceriesAmount.SelectedItem) storeTotal = storeTotal +(packGrapes * cboGroceriesAmount.SelectedItem) ElseIf lstGroceries.SelectedIndex = 3然后 lstItemValue.Items.Add(singleLettuce * cboGroceriesAmount.SelectedItem) storeTotal = storeTotal +(单个莴苣* cboGroceriesAmount.SelectedItem) ElseIf lstGroceries.SelectedIndex = 4然后 lstItemValue.Items.Add(largeCucumber * cboGroceriesAmount.SelectedItem) storeTotal = storeTotal +(largeCucumber * cboGroceriesAmount.SelectedItem) 万一 '如果是冷冻食品声明-选择特定元素后,将此值添加到lstItemValue 如果lstFrozen.SelectedItem ="Quorn Mince";然后 lstItemValue.Items.Add(quornMince * cboFrozenAmount.SelectedItem) storeTotal = storeTotal +(quornMince * cboFrozenAmount.SelectedItem) ElseIf lstFrozen.SelectedItem =汉堡(12)".然后 lstItemValue.Items.Add(twelveBurgers * cboFrozenAmount.SelectedItem) storeTotal = storeTotal +(十二个汉堡包* cboFrozenAmount.SelectedItem) ElseIf lstFrozen.SelectedItem =香肠"然后 lstItemValue.Items.Add(frozenSausages * cboFrozenAmount.SelectedItem) storeTotal = storeTotal +(冻结的香肠* cboFrozenAmount.SelectedItem) ElseIf lstFrozen.SelectedItem =约克郡布丁"然后 lstItemValue.Items.Add(yrkPuddings * cboFrozenAmount.SelectedItem) storeTotal = storeTotal +(yrkPuddings * cboFrozenAmount.SelectedItem) ElseIf lstFrozen.SelectedItem =洋葱圈".然后 lstItemValue.Items.Add(onionRings * cboFrozenAmount.SelectedItem) storeTotal = storeTotal +(onionRings * cboFrozenAmount.SelectedItem) 万一 结束Sub
'Store the total of the items added to cart. Dim storeTotal As Double = 0 'Storing beverage prices. Dim stillWater As Double = 0.8 'Price of "Still Water" Dim sparklingWater As Double = 1 'Price of "Sparkling Water" Dim orangeJuice As Double = 1.3 'Price of "Orange Juice" Dim cocaCola As Double = 1.2 'Price of "Coca Cola" Dim pepsiCola As Double = 1.19 'Price of "Pepsi" Dim drPepper As Double = 1.05 'Price of "Dr Pepper" 'Storing groceries prices. Dim bananasFour As Double = 0.7 'Price of "Bananas (4)" Dim applesTwelve As Double = 1 'Price of "Apples (12)" Dim packGrapes As Double = 1.2 'Price of "Grapes" Dim singleLettuce As Double = 0.4 'Price of "Lettuce" Dim largeCucumber As Double = 0.35 'Price of "Cucumber" 'Storing frozen prices. Dim quornMince As Double = 2.1 'Price of "Quorn Mince" Dim twelveBurgers As Double = 1.8 'Price of "Burgers (12)" Dim frozenSausages As Double = 2 'Price of "Sausages" Dim yrkPuddings As Double = 0.9 'Price of "Yorkshire Puddings" Dim onionRings As Double = 1.1 'Price of "Onion Rings" Sub addItems() 'If statement for beverages - when a specific element is selected, add this value to lstItemValue If lstBeverage.SelectedIndex = 0 Then lstItemValue.Items.Add(stillWater * cboBeverageAmount.SelectedItem) storeTotal = storeTotal + (stillWater * cboBeverageAmount.SelectedItem) ElseIf lstBeverage.SelectedIndex = 1 Then lstItemValue.Items.Add(sparklingWater * cboBeverageAmount.SelectedItem) storeTotal = storeTotal + (sparklingWater * cboBeverageAmount.SelectedItem) ElseIf lstBeverage.SelectedIndex = 2 Then lstItemValue.Items.Add(orangeJuice * cboBeverageAmount.SelectedItem) storeTotal = storeTotal + (orangeJuice * cboBeverageAmount.SelectedItem) ElseIf lstBeverage.SelectedIndex = 3 Then lstItemValue.Items.Add(cocaCola * cboBeverageAmount.SelectedItem) storeTotal = storeTotal + (cocaCola * cboBeverageAmount.SelectedItem) ElseIf lstBeverage.SelectedIndex = 4 Then lstItemValue.Items.Add(pepsiCola * cboBeverageAmount.SelectedItem) storeTotal = storeTotal + (pepsiCola * cboBeverageAmount.SelectedItem) ElseIf lstBeverage.SelectedIndex = 5 Then lstItemValue.Items.Add(drPepper * cboBeverageAmount.SelectedItem) storeTotal = storeTotal + (drPepper * cboBeverageAmount.SelectedItem) End If 'If statement for groceries - when a specific element is selected, add this value to lstItemValue If lstGroceries.SelectedIndex = 0 Then lstItemValue.Items.Add(bananasFour * cboGroceriesAmount.SelectedItem) storeTotal = storeTotal + (bananasFour * cboGroceriesAmount.SelectedItem) ElseIf lstGroceries.SelectedIndex = 1 Then lstItemValue.Items.Add(applesTwelve * cboGroceriesAmount.SelectedItem) storeTotal = storeTotal + (applesTwelve * cboGroceriesAmount.SelectedItem) ElseIf lstGroceries.SelectedIndex = 2 Then lstItemValue.Items.Add(packGrapes * cboGroceriesAmount.SelectedItem) storeTotal = storeTotal + (packGrapes * cboGroceriesAmount.SelectedItem) ElseIf lstGroceries.SelectedIndex = 3 Then lstItemValue.Items.Add(singleLettuce * cboGroceriesAmount.SelectedItem) storeTotal = storeTotal + (singleLettuce * cboGroceriesAmount.SelectedItem) ElseIf lstGroceries.SelectedIndex = 4 Then lstItemValue.Items.Add(largeCucumber * cboGroceriesAmount.SelectedItem) storeTotal = storeTotal + (largeCucumber * cboGroceriesAmount.SelectedItem) End If 'If statement for frozen food - when a specific element is selected, add this value to lstItemValue If lstFrozen.SelectedItem = "Quorn Mince" Then lstItemValue.Items.Add(quornMince * cboFrozenAmount.SelectedItem) storeTotal = storeTotal + (quornMince * cboFrozenAmount.SelectedItem) ElseIf lstFrozen.SelectedItem = "Burgers (12)" Then lstItemValue.Items.Add(twelveBurgers * cboFrozenAmount.SelectedItem) storeTotal = storeTotal + (twelveBurgers * cboFrozenAmount.SelectedItem) ElseIf lstFrozen.SelectedItem = "Sausages" Then lstItemValue.Items.Add(frozenSausages * cboFrozenAmount.SelectedItem) storeTotal = storeTotal + (frozenSausages * cboFrozenAmount.SelectedItem) ElseIf lstFrozen.SelectedItem = "Yorkshire Puddings" Then lstItemValue.Items.Add(yrkPuddings * cboFrozenAmount.SelectedItem) storeTotal = storeTotal + (yrkPuddings * cboFrozenAmount.SelectedItem) ElseIf lstFrozen.SelectedItem = "Onion Rings" Then lstItemValue.Items.Add(onionRings * cboFrozenAmount.SelectedItem) storeTotal = storeTotal + (onionRings * cboFrozenAmount.SelectedItem) End If End Sub
非常感谢您能提供的任何帮助.如果我需要提供更多代码供您查看,我很乐意这样做,但是,在检查选择哪个索引并将所需的项目/价格添加到购物车方面,前面的代码是到目前为止
I'd appreciate any help you could offer. If I need to provide more code for you to view, I'd be happy to do so, however, in regards to checking which index is selected and adding the desired item(s) / price to the cart, the previous code is as far as it goes.
推荐答案
通常看到您的代码是错误的.你为什么要问自己.程序应该处理数据,而不包含数据,而您的程序实际上就是围绕它构建的.因此,您不会生病或其他任何疾病,您必须有足够的能力进行每次更改.
Generally seen your code is bad. Why would you ask yourself. A program should handle data, not contain data and your program is really build around it. Because of this you cannot become ill or whatever, you have to be available to do every change.
最简单的更改方法是创建一个XML文件,然后将其用于组合框.
The most easiest way for you to change it, is to create an XML file and then use that for your combobox.
弗兰克最近在这个论坛上做了很多工作,因此您可以在这个论坛上搜索他的所作所为,或者等到他醒来.
Frank has lately done a lot around that in this forum, so you can search this forum for what he did or wait until he awakes.
如果您的文章很少,那么我将使用相同的XML文件,但是随后将使用带有标签和文本框的动态构建屏幕,但这是一种稍微不同的方法,因此请首先使用.
With so few articles as you have I would use the same XML file but then with a dynamic build screen with Labels and textboxes but that is a little bit different kind of approach so take this what you have first.
这篇关于创建一个购物车.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!