Hello.
As the title states, i am unsure of which sort of loop to use in this case.
my little program asks for the users input (into textbox's) the cost of a purchase and what they paid.
the program is then intended to generate what change they would recieve be it either 1x $50 note, 1x $5 note, 1x $0.50 Coin. etc.
but i cant seem to get the loop right, which would generate this string..
Any help? :)
code is:
Public Class Change
Private Sub txtCost_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCost.KeyPress
Dim allowedchars As String = "0123456789."
If allowedchars.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If
End Sub
Private Sub txtMoney_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMoney.KeyPress
Dim allowedchars As String = "0123456789."
If allowedchars.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If
End Sub
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
If rdbNo.Checked = True Then
MsgBox(DontGetExactChange(), MsgBoxStyle.OkOnly, "Change..")
ElseIf rdbYes.Checked = True Then
MsgBox(GetExactChange(), MsgBoxStyle.OkOnly, "Change..")
Else
MsgBox(DontGetExactChange(), MsgBoxStyle.OkOnly, "Change..")
End If
End Sub
Private Function GetChange(ByVal x)
Dim changeReturned As String = ""
Dim change As Integer = x
Do Until change = 0
If x >= 50 Then
changeReturned = changeReturned + "$50 Note" + vbCrLf
change = change - 50
ElseIf x >= 20 Then
changeReturned = changeReturned + "$20 Note" + vbCrLf
ElseIf x >= 10 Then
changeReturned = changeReturned + "$10 Note" + vbCrLf
ElseIf x >= 5 Then
changeReturned = changeReturned + "$5 Note" + vbCrLf
ElseIf x >= 2 Then
changeReturned = changeReturned + "$2 Coin" + vbCrLf
ElseIf x >= 1 Then
changeReturned = changeReturned + "$1 Coin" + vbCrLf
ElseIf x >= 0.5 Then
changeReturned = changeReturned + "50c Coin" + vbCrLf
ElseIf x >= 0.2 Then
changeReturned = changeReturned + "20c Coin" + vbCrLf
ElseIf x >= 0.1 Then
changeReturned = changeReturned + "10c Coin" + vbCrLf
ElseIf x >= 0.05 Then
changeReturned = changeReturned + "5c Coin" + vbCrLf
End If
Loop
Return changeReturned
End Function
Private Function GetExactChange()
Dim cost As Decimal
Dim money As Decimal
Dim change As Decimal
Dim text As String = ""
cost = txtCost.Text
money = txtMoney.Text
change = money - cost
text = "Purchasing something with a value of $" & cost & " and paying $" & money & " will result in a total of $" & change & " change." & vbCrLf & GetChange(change)
Return text
End Function
Private Function DontGetExactChange()
Dim cost As Decimal
Dim money As Decimal
Dim change As Decimal
Dim text As String = ""
cost = txtCost.Text
money = txtMoney.Text
change = money - cost
text = "Purchasing something with a value of $" & cost & " and paying $" & money & " will result in a total of $" & change & " change."
Return text
End Function
End Class