-
Jan 5th, 2018, 01:36 PM
#1
Thread Starter
Junior Member
Implementing money change in a vending machine.
Hello i have a problem implementing a change feature in my program. I want it to work like this: The machine is able to keep count of all the different coins that it has. The user can insert money by clicking a button which also gives + to the amount of coins. The amount of money stays in machine unless the user has spend all of his money or clicked the change button.
I am have a problem with the change function. I use labels to keep track of coins and convert them into a value by using the commend CDbl(xxx.Text). The point with the refund button is to refund in coins starting with the highest value coins, when you refund a coin the count of the coin in the machine should adjust itself to the correct value. I would like a little help.
The problem is that it just doesn't work, when i click refund button it removes all the coins from the counters.
The * 20,10,5... is value of coin
Code:
Private Sub RefundBtn_Click(sender As Object, e As EventArgs) Handles RefundBtn.Click
Dim x As Integer
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim y As Integer
Amount = CDbl(Displaylbl.Text)
Amount = Amount - Amount
x = CDbl(a20lbl.Text)
a = CDbl(a10lbl.Text)
b = CDbl(a5lbl.Text)
c = CDbl(a2lbl.Text)
d = CDbl(a1lbl.Text)
If a20lbl.Text > 0 And 20 <= CDbl(Displaylbl.Text) Then
Amount = Amount - Amount
y = Amount / x * 20
a20lbl.Text = y
Displaylbl.Text = Amount
refundlbl.Text = "✓"
Else
End If
If a10lbl.Text > 0 And 10 <= CDbl(Displaylbl.Text) Then
Amount = Amount - Amount
y = Amount / a * 10
a10lbl.Text = y
Displaylbl.Text = Amount
refundlbl.Text = "✓"
Else
End If
If a5lbl.Text > 0 And 5 <= CDbl(Displaylbl.Text) Then
Amount = Amount - Amount
y = Amount / b * 5
a5lbl.Text = y
Displaylbl.Text = Amount
refundlbl.Text = "✓"
End If
If a2lbl.Text > 0 And 2 <= CDbl(Displaylbl.Text) Then
Amount = Amount - Amount
y = Amount / c * 2
a2lbl.Text = y
Displaylbl.Text = Amount
refundlbl.Text = "✓"
End If
If a1lbl.Text > 0 And 1 <= CDbl(Displaylbl.Text) Then
Amount = Amount - Amount
y = Amount / d
a1lbl.Text = y
Displaylbl.Text = Amount
refundlbl.Text = "✓"
End If
gold.Visible = True
Wait(2)
gold.Visible = False
End Sub
I am a beginner.
Edit: I have spotted a mistake, using the variables instead of values of coin If x, If a, If b... instead of If 20 If 10 If 5... but it still doesn't really work.
Edit2: I have fixed it a bit now the when you click refund button it takes it usses the coins correctly, as in first 20, if amount is smaller then it skips 20 and goes to 10 etc. however it still uses all coins at once, so if change is able to go out in 10s it will take all the 10s even if the amount is only like 20. code updated in thread.
Last edited by michalodzien; Jan 5th, 2018 at 02:02 PM.
-
Jan 5th, 2018, 01:58 PM
#2
Re: Implementing money change in a vending machine.
I didn't look at all your code yet but this stuck out as an obvious problem.
Code:
Amount = CDbl(Displaylbl.Text)
Amount = Amount - Amount
Amount will always be 0. I think if you look at it work a minute you'll see why.
-
Jan 5th, 2018, 02:01 PM
#3
Re: Implementing money change in a vending machine.
You shouldn't keep track of the amount of money via a Label; a Label is simply a GUI control to display information, it is not intended to manipulate data.
With that being said, what you should do is store your increments (20, 10, 5, 2, and 1) in a collection. Then when the user is ready to receive change, loop through each increment amount from largest (20) to smallest (1) and check if any change with that amount can be made.
While I'm not going to rewrite your windows form application project, here is the same concept using a console application project:
Code:
Imports System
Public Module Module1
Public Sub Main()
'Variables
Dim purse As Decimal = 2.0D 'Assume the user starts with $2.00
Dim received As Decimal = 0D 'So far the vending machine hasn't received any money
Dim increments() As Integer = {20, 10, 5, 2, 1} 'Store all the change increments
Dim strInput As String = String.Empty 'Placeholder for upcoming Do/Loop
Dim intInput As Integer = 0 'Placeholder for upcoming Do/Loop
Dim change As Integer = 0 'Placeholder for upcoming For/Each loop
'Prompt for the user to input some change, once a blank line is received then it assumes the user's ready to get change (or's run out of money)
Do
'Prompt the user
Console.WriteLine("Which coin do you want to insert: {0}", String.Join(", ", increments))
strInput = Console.ReadLine()
'Attempt to convert the String to a Decimal and check that it is a valid coin increment
If Integer.TryParse(strInput, intInput) AndAlso Array.IndexOf(increments, intInput) > -1 Then
'Since the coin is represented as a whole number, we need to convert it to a decimal before subtracting it from the purse
purse -= intInput / 100
received += intInput
ElseIf Not String.IsNullOrWhiteSpace(strInput) Then
'Inform the user that they cannot follow instructions
Console.WriteLine("Please enter a valid coin ({0})", String.Join(", ", increments))
End If
Console.WriteLine()
Loop Until String.IsNullOrWhiteSpace(strInput) OrElse purse = 0D
'Now that the user's ready to receive change, loop through each increment
For Each increment As Integer In increments
'Check how many times the increment can go into the received (rounding down)
change = Math.Floor(received / increment)
'If change can be made, then subtract the number of times that the increment can be made
received -= change * increment
'Prompt the user of the number of times that they'll receive the coin back
If change > 0 Then
Console.WriteLine("Giving back {0} coin(s) of {1}", change, increment)
End If
Next
End Sub
End Module
Fiddle: Live Demo
Last edited by dday9; Jan 5th, 2018 at 02:04 PM.
Reason: Fixed Indents
-
Jan 5th, 2018, 07:02 PM
#4
Re: Implementing money change in a vending machine.
Not quite the same concept, but I assume you didn't want to just give the lesson away, i.e. I put in three 20 piece coins and in the end it gave me seven 20 piece coins back, but the machine should only have had three to return.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|