Winforms 如何实现自动售货机?
对于一个学校项目,我被要求使用VisualBasic在windows窗体应用程序中创建一个虚拟自动售货机。我大约完成了80%,但我有一些问题,我已经研究了多次,但仍然找不到帮助 首先,在自动售货机上,当我购买商品时,如果剩余(更改)的信用低于产品的原始价格,它将显示一条错误消息,说明您没有足够的信用来购买产品。如果信用卡不足以在购买前购买产品,而不是在购买后立即购买产品,我如何使其仅显示该消息 其次,我将如何着手为购买后剩余的信用创建一个系统,因为目前我只有一个信用返回按钮,用户将使用该按钮提取购买后剩余的信用,而不是一个用于支付零钱的系统 最后,我如何使用户不被允许在文本框中输入文本,因为自动售货机使用两个文本框,一个显示信用,另一个显示其他一般消息,例如澄清您购买了某个产品或您没有足够的信用购买某个产品 以下是我目前的代码:Winforms 如何实现自动售货机?,winforms,visual-studio-2010,visual-studio,Winforms,Visual Studio 2010,Visual Studio,对于一个学校项目,我被要求使用VisualBasic在windows窗体应用程序中创建一个虚拟自动售货机。我大约完成了80%,但我有一些问题,我已经研究了多次,但仍然找不到帮助 首先,在自动售货机上,当我购买商品时,如果剩余(更改)的信用低于产品的原始价格,它将显示一条错误消息,说明您没有足够的信用来购买产品。如果信用卡不足以在购买前购买产品,而不是在购买后立即购买产品,我如何使其仅显示该消息 其次,我将如何着手为购买后剩余的信用创建一个系统,因为目前我只有一个信用返回按钮,用户将使用该按钮提取
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
RichTextBox1.Text = "You Have Selected Galaxy Caramel, Please Insert 60p"
If credit >= 60 Then RichTextBox1.Text = "Thank you for purchasing a Galaxy Caramel bar"
credit = credit - 60
TextBox1.Text = credit
If credit < 60 Then RichTextBox1.Text = "You do not have enough credit to purchase a Galaxy Caramel Bar"
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
RichTextBox1.Text = "You Have Selected Cadburys Milk Chocolate, Please Insert 75p"
If credit >= 75 Then RichTextBox1.Text = "Thank you for purchasing a Cadburys Milk Chocolate Bar"
credit = credit - 75
TextBox1.Text = credit
If credit < 75 Then RichTextBox1.Text = "You do not have enough credit to purchase a Cadburys Milk Chocolate Bar"
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
RichTextBox1.Text = "You Have Selected Bounty, Please Insert 70p"
If credit >= 70 Then RichTextBox1.Text = "Thank you for purchasing a Bounty bar"
credit = credit - 70
TextBox1.Text = credit
If credit < 70 Then RichTextBox1.Text = "You do not have enough credit to purchase a Bounty Bar"
End Sub
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
RichTextBox1.Text = "You Have Selected Yorkie, Please Insert 60p"
If credit >= 60 Then RichTextBox1.Text = "Thank you for purchasing a Yorkie bar"
credit = credit - 60
TextBox1.Text = credit
If credit < 60 Then RichTextBox1.Text = "You do not have enough credit to purchase a Yorkie Bar"
End Sub
Private Sub Button8_Click(sender As System.Object, e As System.EventArgs) Handles Button8.Click
RichTextBox1.Text = "You Have Selected Doritos Tangy Cheese, Please Insert 85p"
If credit >= 85 Then RichTextBox1.Text = "Thank you for purchasing Doritos Tangy Cheese Crisps"
credit = credit - 85
TextBox1.Text = credit
If credit < 85 Then RichTextBox1.Text = "You do not have enough credit purchase Doritos Tangy Cheese Crisps"
End Sub
Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
RichTextBox1.Text = "You Have Selected Doritos Cool Original, Please Insert 75p"
If credit >= 75 Then RichTextBox1.Text = "Thank you for purchasing Doritos Cool Original Crisps"
credit = credit - 75
TextBox1.Text = credit
If credit < 75 Then RichTextBox1.Text = "You do not have enough credit to purchase Doritos Cool Original Crisps"
End Sub
Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
RichTextBox1.Text = "You Have Selected Walkers Cheese & Onion, Please Insert 70p"
If credit >= 70 Then RichTextBox1.Text = "Thank you for purchasing Walkers Cheese & Onion Crisps"
credit = credit - 70
TextBox1.Text = credit
If credit < 70 Then RichTextBox1.Text = "You do not have enough credit to purchase Walkers Cheese & Onion Crisps"
End Sub
Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
RichTextBox1.Text = "You Have Selected Mccoy's Cheddar, Please Insert 80p"
If credit >= 80 Then RichTextBox1.Text = "Thank you for purchasing Mccoy's Cheddar Crisps"
credit = credit - 80
TextBox1.Text = credit
If credit < 80 Then RichTextBox1.Text = "You do not have enough credit to purchase Mccoy's Cheddar Crisps"
End Sub
Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click
RichTextBox1.Text = "You Have Selected Pepsi Max, Please Insert £1.10"
If credit >= 110 Then RichTextBox1.Text = "Thank you for purchasing Pepsi Max"
credit = credit - 110
TextBox1.Text = credit
If credit < 110 Then RichTextBox1.Text = "You do not have enough credit to purchase Pepsi Max"
End Sub
Private Sub Button12_Click(sender As System.Object, e As System.EventArgs) Handles Button12.Click
RichTextBox1.Text = "You Have Selected Mountain Dew, Please Insert 99p"
If credit >= 99 Then RichTextBox1.Text = "Thank you for purchasing Mountain Dew"
credit = credit - 99
TextBox1.Text = credit
If credit < 99 Then RichTextBox1.Text = "You do not have enough credit to purchase Mountain Dew"
End Sub
Private Sub Button10_Click(sender As System.Object, e As System.EventArgs) Handles Button10.Click
RichTextBox1.Text = "You Have Selected Fanta, Please Insert £1.05"
If credit >= 105 Then RichTextBox1.Text = "Thank you for purchasing Fanta"
credit = credit - 105
TextBox1.Text = credit
If credit < 105 Then RichTextBox1.Text = "You do not have enough credit to purchase Fanta"
End Sub
Private Sub Button11_Click(sender As System.Object, e As System.EventArgs) Handles Button11.Click
RichTextBox1.Text = "You Have Selected Dr.Pepper, Please Insert £1.20"
If credit >= 120 Then RichTextBox1.Text = "Thank you for purchasing Dr.Pepper"
credit = credit - 120
TextBox1.Text = credit
If credit < 120 Then RichTextBox1.Text = "You do not have enough credit to purchase Dr.Pepper"
End Sub
Private Sub Button13_Click(sender As System.Object, e As System.EventArgs) Handles Button13.Click
RichTextBox1.Text = "You Have Selected Buxton Mineral Water, Please Insert 90p"
If credit >= 90 Then RichTextBox1.Text = "Thank you for purchasing Buxton Mineral Water"
credit = credit - 90
TextBox1.Text = credit
If credit < 90 Then RichTextBox1.Text = "You do not have enough credit to purchase Buxton Mineral Water"
End Sub
Private Sub Button14_Click(sender As System.Object, e As System.EventArgs) Handles Button14.Click
credit = credit + 1
TextBox1.Text = credit
End Sub
Private Sub Button21_Click(sender As System.Object, e As System.EventArgs) Handles Button21.Click
credit = credit + 2
TextBox1.Text = credit
End Sub
Private Sub Button20_Click(sender As System.Object, e As System.EventArgs) Handles Button20.Click
credit = credit + 5
TextBox1.Text = credit
End Sub
Private Sub Button19_Click(sender As System.Object, e As System.EventArgs) Handles Button19.Click
credit = credit + 10
TextBox1.Text = credit
End Sub
Private Sub Button18_Click(sender As System.Object, e As System.EventArgs) Handles Button18.Click
credit = credit + 20
TextBox1.Text = credit
End Sub
Private Sub Button17_Click(sender As System.Object, e As System.EventArgs) Handles Button17.Click
credit = credit + 50
TextBox1.Text = credit
End Sub
Private Sub Button16_Click(sender As System.Object, e As System.EventArgs) Handles Button16.Click
credit = credit + 100
TextBox1.Text = credit
End Sub
Private Sub Button15_Click(sender As System.Object, e As System.EventArgs) Handles Button15.Click
credit = credit + 200
TextBox1.Text = credit
End Sub
Private Sub Button22_Click(sender As System.Object, e As System.EventArgs) Handles Button22.Click
credit = 0
TextBox1.Text = credit
RichTextBox1.Text = "Credit has been returned"
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
If credit < 0 Then credit = 0
TextBox1.Text = credit
If credit >= 1000 Then credit = 1000
TextBox1.Text = credit
If credit = 1000 Then RichTextBox1.Text = "Maximum credit of £10"
End Sub
Private Sub Button23_Click(sender As System.Object, e As System.EventArgs) Handles Button23.Click
RichTextBox1.Text = "You Have Selected Monster Munch, Please Insert 90p"
If credit >= 90 Then RichTextBox1.Text = "Thank you for purchasing Monster Munch Crisps"
credit = credit - 90
TextBox1.Text = credit
If credit < 90 Then RichTextBox1.Text = "You do not have enough credit to purchase Monster Munch Crisps"
End Sub
Private子按钮1\u单击(发送方作为System.Object,e作为System.EventArgs)处理按钮1。单击
RichTextBox1.Text=“您已选择Galaxy焦糖,请插入60p”
如果信用>=60,则RichTextBox1.Text=“感谢您购买Galaxy焦糖棒”
信用=信用-60
TextBox1.Text=信用卡
如果信用<60,则RichTextBox1.Text=“您没有足够的信用购买Galaxy焦糖棒”
端接头
私有子按钮2\u单击(发送者作为System.Object,e作为System.EventArgs)处理按钮2。单击
RichTextBox1.Text=“您已选择Cadburys牛奶巧克力,请插入75便士”
如果信用>=75,则RichTextBox1.Text=“感谢您购买Cadburys牛奶巧克力”
信用=信用-75
TextBox1.Text=信用卡
如果信用<75,则RichTextBox1.Text=“您没有足够的信用购买Cadburys牛奶巧克力”
端接头
私有子按钮3\u单击(发件人作为System.Object,e作为System.EventArgs)处理按钮3。单击
RichTextBox1.Text=“您已选择赏金,请插入70便士”
如果信用>=70,则RichTextBox1.Text=“感谢您购买赏金吧”
信用=信用-70
TextBox1.Text=信用卡
如果信用<70,则RichTextBox1.Text=“您没有足够的信用购买赏金条”
端接头
私有子按钮4\u单击(发件人作为System.Object,e作为System.EventArgs)处理按钮4。单击
RichTextBox1.Text=“您已选择Yorkie,请插入60p”
如果信用>=60,则RichTextBox1.Text=“感谢您购买约克酒吧”
信用=信用-60
TextBox1.Text=信用卡
如果信用<60,则RichTextBox1.Text=“您没有足够的信用购买约克酒吧”
端接头
私有子按钮8\u单击(发件人作为System.Object,e作为System.EventArgs)处理按钮8。单击
RichTextBox1.Text=“您已选择多丽托斯浓烈奶酪,请插入85便士”
如果信用>=85,则RichTextBox1.Text=“感谢您购买多力多司香脆奶酪”
信用=信用-85
TextBox1.Text=信用卡
如果信用<85,则RichTextBox1.Text=“您没有足够的信用购买多力多滋味奶酪薯片”
端接头
私有子按钮7\u单击(发件人作为System.Object,e作为System.EventArgs)处理按钮7。单击
RichTextBox1.Text=“您已选择Doritos Cool Original,请插入75p”
如果信用>=75,则RichTextBox1.Text=“感谢您购买Doritos Cool原创薯片”
信用=信用-75
TextBox1.Text=信用卡
如果信用度<75,则RichTextBox1.Text=“您没有足够的信用度购买Doritos Cool Original Crisps”
端接头
私有子按钮6\u单击(发件人作为System.Object,e作为System.EventArgs)处理按钮6。单击
RichTextBox1.Text=“您已选择Walkers奶酪和洋葱,请插入70便士”
如果信用>=70,则RichTextBox1.Text=“感谢您购买Walkers奶酪和洋葱薯片”
信用=信用-70
TextBox1.Text=信用卡
如果信用<70,则RichTextBox1.Text=“您没有足够的信用购买Walkers奶酪和洋葱薯片”
端接头
私有子按钮5\u单击(发件人作为System.Object,e作为System.EventArgs)处理按钮5。单击
RichTextBox1.Text=“您选择了Mccoy的切达干酪,请插入80便士”
如果信用>=80,则RichTextBox1.Text=“感谢您购买麦考伊的切达薯片”
信用=信用-80
TextBox1.Text=信用卡
如果信用<80,则RichTextBox1.Text=“您没有足够的信用购买Mccoy的切达薯片”
端接头
私有子按钮9\u单击(发件人作为System.Object,e作为System.EventArgs)处理按钮9。单击
RichTextBox1.Text=“您已选择百事Max,请插入1.10英镑”
如果信用>=110,则RichTextBox1.Text=“感谢您购买百事Max”
信用=信用-110
TextBox1.Text=信用卡
如果信用<110,则RichTextBox1.Text=“您没有足够的信用购买百事Max”
端接头
私有子按钮12\u单击(发件人作为System.Object,e作为System.EventArgs)处理按钮12。单击
RichTextBox1.Text=“您已选择Mountain Dew,请插入99p”
如果信用>=99,则RichTextBox1.Text=“感谢您购买Mountain Dew”
信用=信用-99
TextBox1.Text=信用卡
如果信用<99,则RichTextBox1.Text=“您没有足够的信用购买Mountain Dew”
端接头
私有子按钮10\u单击(发件人作为System.Object,e作为System.EventArgs)处理按钮10。单击
RichTextBox1.Text=“您已选择芬达,请插入1.05英镑”
如果信用>=105,则RichTextBox1.Text=“感谢您购买芬达”
信用=信用-105
TextBox1.Text=信用卡
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
RichTextBox1.Text = "You Have Selected Galaxy Caramel, Please Insert 60p"
If credit >= 60 Then
RichTextBox1.Text = "Thank you for purchasing a Galaxy Caramel bar"
credit = credit - 60
TextBox1.Text = credit
Else
RichTextBox1.Text = "You do not have enough credit to purchase a Galaxy Caramel Bar"
End If
End Sub
/// <summary>
/// This method attempts to calculate required change from a given coin inventory
/// </summary>
/// <param name="coinInventory">Key is the denomination of coin and value is the available number </param>
/// <param name="changeRequired">The change to count and return</param>
/// <param name="updatedCoinInventory"></param>
/// <param name="coinsToReturn"></param>
/// <returns>True if changeRequired could be calculated, False if coinsToReturn represent less than changeRequired </returns>
public bool GetChange(IDictionary<int, int> coinInventory, int changeRequired,
ref IDictionary<int, int> updatedCoinInventory,
ref IDictionary<int, int> coinsToReturn,
ref int amountDue)
{
int changeOutstanding = changeRequired;
updatedCoinInventory = coinInventory.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
coinsToReturn = new Dictionary<int, int>();
foreach (KeyValuePair<int, int> coin in coinInventory)
{
if (changeOutstanding == 0) break;
int numberOfAvailableCoins = coin.Value;
while (changeOutstanding >= coin.Key && numberOfAvailableCoins >= 1)
{
if (!coinsToReturn.ContainsKey(coin.Key))
{
coinsToReturn.Add(coin.Key, 1);
}
else
{
++ coinsToReturn[coin.Key];
}
changeOutstanding = changeOutstanding - coin.Key;
-- updatedCoinInventory[coin.Key];
-- numberOfAvailableCoins;
}
}
amountDue = changeOutstanding;
return changeOutstanding == 0;
}
}