12 Replies - 993 Views - Last Post: 22 April 2013 - 08:37 AM Rate Topic: -----

#1 chancock5005  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 04-March 13

Change Program

Posted 20 April 2013 - 02:25 PM

hello I have a question about a change algorithm. My assignment ( and yes this is a school assignment ) is a virtual drink machine to be coded in vb. I have 90% of this finished and can continue the way i am coding now. My question is is there a formula or code snippet i can use that will allow me to give change whether from pressing the return button or change from selecting a drink that will return the smallest number of coins out of my coin inventory, without having to right 200+ lines of nested if statements.
here is my code

i have nickel, dime, and quarter dimmed as the money coming in, the amount stores the total of money in, and numnickel, numdime, and numquarter store the number of coins to be displayed in a listbox

i can use all coins put in machine to give my refund or to use to make change

any help with this would be appeciated

here is my code so far and yes I know there is a broken if statement (I copied it while i was in the middle of coding)

Public Class DrinkMachine
    Dim nickel As Decimal = 0.05
    Dim dime As Decimal = 0.1
    Dim quarter As Decimal = 0.25
    Dim dollar As Decimal = 1.0
    Dim amount As Decimal
    Dim numNickel As Integer = 8
    Dim numDime As Integer = 8
    Dim numQuarter As Integer = 8

    Private Sub DrinkMachine_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        inventoryListBox.Items.Add("Quarters" & ControlChars.Tab & "Dimes" & ControlChars.Tab & "Nickels")
        inventoryListBox.Items.Add(numQuarter.ToString & ControlChars.Tab & numDime.ToString & ControlChars.Tab & numNickel.ToString)
    End Sub


    Private Sub dPepsiPictureBox_MouseHover(sender As Object, e As System.EventArgs) Handles dPepsiPictureBox.MouseHover
        amountTextBox.Text = 0.55
        returnTextBox.Text = ""

    End Sub

    Private Sub dpPictureBox_MouseHover(sender As Object, e As System.EventArgs) Handles dpPictureBox.MouseHover
        amountTextBox.Text = 0.55
        returnTextBox.Text = ""

    End Sub

    Private Sub mdPictureBox_MouseHover(sender As Object, e As System.EventArgs) Handles mdPictureBox.MouseHover
        amountTextBox.Text = 0.55
        returnTextBox.Text = ""

    End Sub

    Private Sub pepsiPictureBox_MouseHover(sender As Object, e As System.EventArgs) Handles pepsiPictureBox.MouseHover
        amountTextBox.Text = 0.55
        returnTextBox.Text = ""

    End Sub

    Private Sub dPepsiPictureBox_MouseLeave(sender As Object, e As System.EventArgs) Handles dPepsiPictureBox.MouseLeave
        If amount = 0 Then
            amountTextBox.Text = "0.00"
        Else : amountTextBox.Text = String.Format("{0:F2}", amount)
        End If

    End Sub

    Private Sub dpPictureBox_MouseLeave(sender As Object, e As System.EventArgs) Handles dpPictureBox.MouseLeave
        If amount = 0 Then
            amountTextBox.Text = "0.00"
        Else : amountTextBox.Text = String.Format("{0:F2}", amount)
        End If
    End Sub

    Private Sub mdPictureBox_MouseLeave(sender As Object, e As System.EventArgs) Handles mdPictureBox.MouseLeave
        If amount = 0 Then
            amountTextBox.Text = "0.00"
        Else : amountTextBox.Text = String.Format("{0:F2}", amount)
        End If
    End Sub

    Private Sub pepsiPictureBox_MouseLeave(sender As Object, e As System.EventArgs) Handles pepsiPictureBox.MouseLeave
        If amount = 0 Then
            amountTextBox.Text = "0.00"
        Else : amountTextBox.Text = String.Format("{0:F2}", amount)
        End If
    End Sub

    Private Sub nickelButton_Click(sender As System.Object, e As System.EventArgs) Handles nickelButton.Click

        amount += nickel
        numNickel += 1
        inventoryListBox.Items.Clear()
        inventoryListBox.Items.Add("Quarters" & ControlChars.Tab & "Dimes" & ControlChars.Tab & "Nickels")
        inventoryListBox.Items.Add(numQuarter.ToString & ControlChars.Tab & numDime.ToString & ControlChars.Tab & numNickel.ToString)

        amountTextBox.Text = String.Format("{0:F2}", amount)
        returnTextBox.Text = ""

        If amount >= 0.55 Then
            nickelButton.Enabled = False
            dimeButton.Enabled = False
            quarterButton.Enabled = False
        End If
    End Sub

    Private Sub dimeButton_Click(sender As System.Object, e As System.EventArgs) Handles dimeButton.Click

        amount += dime
        numDime += 1
        inventoryListBox.Items.Clear()
        inventoryListBox.Items.Add("Quarters" & ControlChars.Tab & "Dimes" & ControlChars.Tab & "Nickels")
        inventoryListBox.Items.Add(numQuarter.ToString & ControlChars.Tab & numDime.ToString & ControlChars.Tab & numNickel.ToString)

        amountTextBox.Text = String.Format("{0:F2}", amount)
        returnTextBox.Text = ""

        If amount >= 0.55 Then
            nickelButton.Enabled = False
            dimeButton.Enabled = False
            quarterButton.Enabled = False
        End If
    End Sub

    Private Sub quarterButton_Click(sender As System.Object, e As System.EventArgs) Handles quarterButton.Click

        amount += quarter
        numQuarter += 1
        inventoryListBox.Items.Clear()
        inventoryListBox.Items.Add("Quarters" & ControlChars.Tab & "Dimes" & ControlChars.Tab & "Nickels")
        inventoryListBox.Items.Add(numQuarter.ToString & ControlChars.Tab & numDime.ToString & ControlChars.Tab & numNickel.ToString)

        amountTextBox.Text = String.Format("{0:F2}", amount)
        returnTextBox.Text = ""

        If amount >= 0.55 Then
            nickelButton.Enabled = False
            dimeButton.Enabled = False
            quarterButton.Enabled = False
        End If
    End Sub


    Private Sub dpPictureBox_Click(sender As System.Object, e As System.EventArgs) Handles dpPictureBox.Click
        If amount < 0.55 Then
            amountTextBox.Text = "Insufficient Funds"
            returnTextBox.Text = ""
        ElseIf amount >= 0.55 Then
            dispenserPanel.BackgroundImage = My.Resources.dr_pepper
        End If


    End Sub

    Private Sub dispenserPanel_BackgroundImageChanged(sender As Object, e As System.EventArgs) Handles dispenserPanel.BackgroundImageChanged
        amount = 0.0
    End Sub

    Private Sub mdPictureBox_Click(sender As System.Object, e As System.EventArgs) Handles mdPictureBox.Click
        If amount < 0.55 Then
            amountTextBox.Text = "Insufficient Funds"
            returnTextBox.Text = ""
        ElseIf amount >= 0.55 Then
            dispenserPanel.BackgroundImage = My.Resources.md
        End If

    End Sub

    Private Sub pepsiPictureBox_Click(sender As System.Object, e As System.EventArgs) Handles pepsiPictureBox.Click
        If amount < 0.55 Then
            amountTextBox.Text = "Insufficient Funds"
            returnTextBox.Text = ""
        ElseIf amount >= 0.55 Then
            dispenserPanel.BackgroundImage = My.Resources.PepsiButton
        End If

    End Sub

    Private Sub dPepsiPictureBox_Click(sender As System.Object, e As System.EventArgs) Handles dPepsiPictureBox.Click
        If amount < 0.55 Then
            amountTextBox.Text = "Insufficient Funds"
            returnTextBox.Text = ""
        ElseIf amount >= 0.55 Then
            dispenserPanel.BackgroundImage = My.Resources.dietpepsi
        End If

    End Sub

    Private Sub dispenserPanel_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles dispenserPanel.MouseClick
        dispenserPanel.BackgroundImage = My.Resources.back_color
        nickelButton.Enabled = True
        dimeButton.Enabled = True
        quarterButton.Enabled = True
    End Sub

    Private Sub returnButton_Click(sender As System.Object, e As System.EventArgs) Handles returnButton.Click
        amountTextBox.Text = "0.00"
        nickelButton.Enabled = True
        dimeButton.Enabled = True
        quarterButton.Enabled = True

        If amount = 0.75 Then
            If numQuarter >= 3 Then
                returnTextBox.Text = "3 Quarter"
                amount = 0.0
                numQuarter -= 3
            ElseIf numQuarter = 2 And numDime >= 2 And numNickel >= 1 Then
                returnTextBox.Text = "2 Quarter" & vbCrLf & "2 Dime" & "1 Nickel"
                amount = 0.0
                numQuarter -= 2
                numDime -= 2
                numNickel -= 1
            ElseIf numQuarter = 2 And numDime = 1 And numNickel >= 3 Then
                returnTextBox.Text = "2 Quarter" & vbCrLf & "1 Dime" & vbCrLf & "3 Nickel"
                amount = 0.0
                numQuarter -= 2
                numDime -= 1
                numNickel -= 3
            ElseIf numQuarter = 2 And numDime < 1 And numNickel >= 2 Then
                returnTextBox.Text = "2 Quarter" & vbCrLf & "5 Nickel"
                amount = 0.0
                numQuarter -= 2
                numNickel -= 5
            ElseIf numQuarter = 1 And numDime >= 5 Then
                returnTextBox.Text = "1 Quarter" & vbCrLf & "5 Dime"
                amount = 0.0
                numQuarter -= 1
                numDime -= 5
            ElseIf numQuarter = 1 And numDime = 4 And numNickel >= 2 Then
                returnTextBox.Text = "1 Quarter" & vbCrLf & "4 Dime" & vbCrLf & "2 Nickel"
                amount = 0.0
                numQuarter -= 1
                numDime -= 4
                numNickel -= 2
            ElseIf numQuarter = 1 And numDime = 3 And numNickel >= 4 Then
                returnTextBox.Text = "1 Quarter" & vbCrLf & "3 Dime" & vbCrLf & "4 Nickel"
                amount = 0.0
                numQuarter -= 1
                numDime -= 3
                numNickel -= 4
                ElseIf 
            ElseIf amount = 0.6 Then
                returnTextBox.Text = "2 Quarter" & vbCrLf & "1 Dime"
                amount = 0.0
                numQuarter -= 2
                numDime -= 1
            ElseIf amount = 0.55 Then
                returnTextBox.Text = "2 Quarter" & vbCrLf & "1 Nickel"
                amount = 0.0
                numQuarter -= 2
                numNickel -= 1
            ElseIf amount = 0.5 Then
                returnTextBox.Text = "2 Quarter"
                amount = 0.0
                numQuarter -= 2
            ElseIf amount = 0.45 Then
                returnTextBox.Text = "1 Quarter" & vbCrLf & "2 Dime"
                amount = 0.0
                numQuarter -= 1
                numDime -= 2
            ElseIf amount = 0.4 Then
                returnTextBox.Text = "1 Quarter" & vbCrLf & "1 Dime" & vbCrLf & "1 Nickel"
                amount = 0.0
                numQuarter -= 1
                numDime -= 1
                numNickel -= 1
            ElseIf amount = 0.35 Then
                returnTextBox.Text = "1 Quarter" & vbCrLf & "1 Dime"
                amount = 0.0
                numQuarter -= 1
                numDime -= 1
            ElseIf amount = 0.3 Then
                returnTextBox.Text = "1 Quarter" & vbCrLf & "1 Nickel"
                amount = 0.0
                numQuarter -= 1
                numNickel -= 1
            ElseIf amount = 0.25 Then
                returnTextBox.Text = "1 Quarter"
                amount = 0.0
                numQuarter -= 1
            ElseIf amount = 0.2 Then
                returnTextBox.Text = "2 Dime"
                amount = 0.0
                numDime -= 2
            ElseIf amount = 0.15 Then
                returnTextBox.Text = "1 Dime" & vbCrLf & " 1 Nickel"
                amount = 0.0
                numDime -= 1
                numNickel -= 1
            ElseIf amount = 0.1 Then
                returnTextBox.Text = "1 Dime"
                amount = 0.0
                numDime -= 1
            ElseIf amount = 0.05 Then
                returnTextBox.Text = "1 Nickel"
                amount = 0.0
                numNickel -= 1
            End If
            inventoryListBox.Items.Clear()
            inventoryListBox.Items.Add("Quarters" & ControlChars.Tab & "Dimes" & ControlChars.Tab & "Nickels")
            inventoryListBox.Items.Add(numQuarter.ToString & ControlChars.Tab & numDime.ToString & ControlChars.Tab & numNickel.ToString)
    End Sub

End Class


This post has been edited by macosxnerd101: 20 April 2013 - 04:42 PM
Reason for edit:: Please use a descriptive title


Is This A Good Question/Topic? 0
  • +

Replies To: Change Program

#2 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6504
  • View blog
  • Posts: 14,355
  • Joined: 02-June 10

Re: Change Program

Posted 20 April 2013 - 03:11 PM

This is a well known homework problem. So other than "is there a piece of code I can copy/paste and call it my own work", what can we do to actually help you with understanding the problem better?


Quote

My question is is there a formula or code snippet i can use that will allow me to give change whether from pressing the return button or change from selecting a drink that will return the smallest number of coins out of my coin inventory, without having to right 200+ lines of nested if statements.

Was This Post Helpful? 0
  • +
  • -

#3 chancock5005  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 04-March 13

Re: Change Program

Posted 20 April 2013 - 04:40 PM

View PosttlhIn`toq, on 20 April 2013 - 03:11 PM, said:

This is a well known homework problem. So other than "is there a piece of code I can copy/paste and call it my own work", what can we do to actually help you with understanding the problem better?


Quote

My question is is there a formula or code snippet i can use that will allow me to give change whether from pressing the return button or change from selecting a drink that will return the smallest number of coins out of my coin inventory, without having to right 200+ lines of nested if statements.



For one thing i dont see anything wrong with asking for help writing an algorithm. and two if i ever "copy/paste" anyones code i cite where and who i received it from. I also never claimed this was not a homework assignment, I also mentioned that I could do my program myself using nested ifs as I also proved in the code i did provide. All I asked is

" is there a formula or code snippet i can use that will allow me to give change whether from pressing the return button or change from selecting a drink that will return the smallest number of coins out of my coin inventory, without having to right 200+ lines of nested if statements "

If so I would like advice on how abouts to write such a formula or code.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12134
  • View blog
  • Posts: 45,114
  • Joined: 27-December 08

Re: Change Program

Posted 20 April 2013 - 04:45 PM

Looking at the return amount in pennies, the number of quarters to return is amount/25. Now the remaining amount is amount mod 25. Now think about this for dimes, then nickels, and the final remaining amount is the number of pennies.
Was This Post Helpful? 0
  • +
  • -

#5 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 117
  • View blog
  • Posts: 646
  • Joined: 01-October 08

Re: Change Program

Posted 20 April 2013 - 05:01 PM

It might be helpful to use arrays

        'change denominations
        Dim amts() As Decimal = New Decimal() {1D, 0.5D, 0.25D, 0.1D, 0.05D, 0.01D}
        'how many of each coin on hand
        Dim amtsOH() As Integer = New Integer() {0, 1, 4, 8, 8, 10}
        'coins dispersed
        Dim coinsout(amtsOH.Length - 1) As Integer



These arrays should be the same length. In the example posted there is 0 dollars, 1 fifty cent piece, 4 quarters, 8 dimes, 8 nickels, and 10 pennies. The order is a hint to the algorithm.
Was This Post Helpful? 0
  • +
  • -

#6 chancock5005  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 04-March 13

Re: Change Program

Posted 21 April 2013 - 02:52 PM

i have it completed i decided to go with nested if statements. 1300 lines of code but its done.
thanks though.
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13389
  • View blog
  • Posts: 53,429
  • Joined: 12-June 08

Re: Change Program

Posted 21 April 2013 - 03:04 PM

Ah.. 1300 lines of nested ifs? I am certain that's (probably) not the direction your teacher wanted you to go. I would figure using the 'modulus' and some loops would be more the lesson learned.
Was This Post Helpful? 0
  • +
  • -

#8 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: Change Program

Posted 21 April 2013 - 10:11 PM

View Postchancock5005, on 21 April 2013 - 03:52 PM, said:

i have it completed i decided to go with nested if statements. 1300 lines of code but its done.
thanks though.

It's usually helpful to figure out how you would do it if you were behind the counter and making change yourself.

Let's say you were given 1 dollar for a 35 cent item.

You know the change will be 65 cents,

How many quarters will add up to less than 65 cents? What's left over?
How many dimes will add up to less than what's left over? Then what's left over?
How many nickels will add up to less than what's left over?

You've been given some good hints. The calculations required don't even need a loop, and can be done in 5 lines of code.
Was This Post Helpful? 0
  • +
  • -

#9 chancock5005  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 04-March 13

Re: Change Program

Posted 22 April 2013 - 12:38 AM

there were more items involved though
you start with 8 nickels 5 dimes and 3 quarters
drinks are 55 cents
if drink is selected you have to give correct change in fewest coin
if not enough change then you have to say use correct change and also not allow money to go in that will not allow change to be made
Was This Post Helpful? 0
  • +
  • -

#10 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: Change Program

Posted 22 April 2013 - 05:42 AM

View Postchancock5005, on 22 April 2013 - 01:38 AM, said:

there were more items involved though
you start with 8 nickels 5 dimes and 3 quarters
drinks are 55 cents
if drink is selected you have to give correct change in fewest coin
if not enough change then you have to say use correct change and also not allow money to go in that will not allow change to be made

Sure, but the principle is the same.

You calcuate the number of quarters needed for the optimum solution.
If you don't have enough, use what you have, and calculate what's left.
Carry on to dimes, using same calculation.
Carry on to nickels, same calculation.
If you can't make change, return all coins just entered.

Again, the calculations for the optimum solution (you have enough coins) can be done in 5 lines of code. The extra conditionals for paying out the remaining coins of any given denomination will add 4 lines of code for each coin.

Why not first try doing the solution for giving change when you have an unlimited supply of coins? Get that down to a small routine, then start thinking about how to make it do the conditional stuff. Forget all the fancy PictureBoxes until you have the basic algorithm figured out.

Make a new project. add 1 button, 1 textbox, and three labels.
Write the subroutine to give change for the amount placed in the textbox
Display the number of each coin in the labels.

This post has been edited by lar3ry: 22 April 2013 - 05:44 AM

Was This Post Helpful? 1
  • +
  • -

#11 chancock5005  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 04-March 13

Re: Change Program

Posted 22 April 2013 - 08:33 AM

View Postlar3ry, on 22 April 2013 - 05:42 AM, said:

View Postchancock5005, on 22 April 2013 - 01:38 AM, said:

there were more items involved though
you start with 8 nickels 5 dimes and 3 quarters
drinks are 55 cents
if drink is selected you have to give correct change in fewest coin
if not enough change then you have to say use correct change and also not allow money to go in that will not allow change to be made

Sure, but the principle is the same.

You calcuate the number of quarters needed for the optimum solution.
If you don't have enough, use what you have, and calculate what's left.
Carry on to dimes, using same calculation.
Carry on to nickels, same calculation.
If you can't make change, return all coins just entered.

Again, the calculations for the optimum solution (you have enough coins) can be done in 5 lines of code. The extra conditionals for paying out the remaining coins of any given denomination will add 4 lines of code for each coin.

Why not first try doing the solution for giving change when you have an unlimited supply of coins? Get that down to a small routine, then start thinking about how to make it do the conditional stuff. Forget all the fancy PictureBoxes until you have the basic algorithm figured out.

Make a new project. add 1 button, 1 textbox, and three labels.
Write the subroutine to give change for the amount placed in the textbox
Display the number of each coin in the labels.

i tossed and turned all night thinking about it and i think i am going to try and do it that way, i do have a week and a half before it is do, plus i also have the one i finished as backup.

just another question though:
can this algorithm be done as a function or does it need to be added to each of my drink buttons and my return button.
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13389
  • View blog
  • Posts: 53,429
  • Joined: 12-June 08

Re: Change Program

Posted 22 April 2013 - 08:35 AM

I would assume one function to take in value you want to make change for would suffice.
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12134
  • View blog
  • Posts: 45,114
  • Joined: 27-December 08

Re: Change Program

Posted 22 April 2013 - 08:37 AM

It's really easy to determine if change can't be made. Given the linear equation: 5a + 10b + 25c = d, where the coefficients of a, b, and c are the change amonuts; change can be made iff gcd(5, 10, 25)|d.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1