2013-10-29 59 views
-1

對於一個學校項目,我被要求在使用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 

末級

+0

你爲什麼要出門?無需在所有上限中編寫標題。 –

+0

對不起:)我是新來的stackoverflow,建議我使標題脫穎而出,讓所有想到的 – lickenchicken123

+0

如果我們將爲你做你的學校工作,你沒有機會成爲一名程序員。你應該想,自動售貨機的工作原理。一點一點地畫出你的設計,所有單位等等。 –

回答

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都具有可以設置爲True的ReadOnly()屬性,以便用戶不能更改它們。但是由於他們只使用按鈕,並且根本不需要輸入,爲什麼不使用像標籤這樣的其他控件呢?

+0

嗨,感謝您的幫助,非常感謝:),我的第二個問題的含義是,我不知道如何編寫一個系統,給自動售貨機發出變更,而是作爲臨時解決方案的用戶必須在購買後才能獲得信貸回報以獲得資金。另外,最後一個問題是,如何將Windows窗體應用程序項目傳輸到USB棒,因爲當我嘗試從USB棒打開時它不起作用,並顯示錯誤消息。謝謝! :) – lickenchicken123

+0

哎呀。您可以簡單地使用另一個控件來接收購買後剩下的任何內容。 –

+0

你收到什麼錯誤信息? –

0

您可以在這種情況下使用狀態機。 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; 
    } 

} 

希望這會有所幫助。

相關問題