【问题标题】:How to implement a vending machine?如何实现自动售货机?
【发布时间】:2013-10-29 15:14:32
【问题描述】:

对于一个学校项目,我被要求使用 Visual Basic 在 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

结束类

【问题讨论】:

  • 你为什么大喊大叫?无需全部大写标题。
  • 对不起 :) 我是 stackoverflow 的新手,建议我突出标题,这样我就想到了
  • 如果我们为你做你的学业,你就没有机会成为一名程序员。你应该想想,自动售货机是如何工作的。一点一点地画出你的设计、所有的单元等和代码。
  • 我只是需要一些帮助,我没有要求创建整个项目。

标签: winforms visual-studio-2010 visual-studio


【解决方案1】:

我怎么做才能让它只在没有信用的情况下显示该消息 足以预先购买产品,而不是在购买后立即购买 购买?

首先,您应该始终使用End If 来关闭您的If 语句,即使它们只有一行。不这样做会导致细微的错误,当If 语句为真时,您认为代码“块”正在运行,但只有第一行受到影响,其余行始终运行。

对于您的特定代码,您非常接近。通过添加 Else 块、End If 并重新格式化代码,它变得如此简单:

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

显然您需要更改所有项目以遵循上述模式。

其次,我将如何为剩余的信用建立一个系统 购买后结束,因为目前我只有一个信用 用户将用于提取剩余信用的返回按钮 购买后留下的,而不是赠送的系统 改变。

不知道你在这里的意思。你能更详细地解释你想要发生的事情吗?

最后,我该怎么做才能让用户不能输入文字 进入一个文本框,因为自动售货机使用两个文本框,一个 显示信用和其他显示其他一般消息,例如 澄清您购买了某种产品或您没有购买 有足够的信用来购买某种产品。

TextBox 和 RichTextBox 都有一个 ReadOnly() 属性,您可以将其设置为 True,以便用户无法更改它们。但既然他们只使用按钮,根本不需要输入,为什么不使用不同的控件,比如 Labels 呢?

【讨论】:

  • 嗨,感谢您的帮助,非常感谢 :),我所说的第二个问题是我不知道如何编写一个为自动售货机提供零钱的系统,而且相反,作为一种临时解决方案,用户必须在购买后按信用退还以获得他们的钱。此外,最后一个问题如何将 Windows 窗体应用程序项目传输到 U 盘,因为当我尝试从 U 盘打开它时它不起作用并显示错误消息。谢谢! :)
  • 明白了。您可以简单地使用另一个控件来接收购买后剩余的任何东西。
  • (只是为了让你知道我直接从名为 Visual Studio 2010 的文件夹中的项目文件夹中复制项目)它说“找不到文件 'F:\Vending Machine.vb 它可能是删除或删除”
  • 对于可执行文件,复制“bin\debug”或“bin\release”文件夹中的文件。所有其他文件都用于在 Visual Studio 中编辑应用程序本身。
  • 我已经尝试过了,但我意识到我的老师可能需要查看应用程序的实际代码,只是为了检查我没有从网上下载它,也许他还需要编辑代码自己去改进
【解决方案2】:

您可以在这种情况下使用状态机。 Head First Design Patterns 有一个很好的使用状态设计模式的自动售货机示例。尽管本书提供了 Java 示例,但您仍然可以理解这些概念。

为了计算变化,我在下面写了这个块。它接受可用的硬币库存和所需的零钱,并返回一个布尔值以指示是否可以退回所有零钱。该方法将尝试返回给定库存可能的最大更改。

    /// <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;
    }

}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多