#1
  1. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jan 2014
    Location
    Los Angeles
    Posts
    3
    Rep Power
    0

    Vending Machine Problem


    Can anybody please help me with some code I have? I'm trying to write a vending machine program where we have to input the cost of the item in cents (no more than 200 cents, no less than 1 cent) and the amount of money we're going to use to pay for that item (no more than 500 cents, no less than 1 cent).

    I've got to show the amount of change I'm going to receive in dollars, quarters, dimes, nickels, and pennies.
    I've also got to make sure it displays the least possible number of coins so that:

    1 quarter
    1 dime
    2 pennies


    is okay but

    2 dimes
    3 nickels
    2 pennies


    is not.

    I think I've got everything figured out, except that I can't get it to display the change in the label it's supposed to display in, so I can't know if my code works otherwise.

    Here's my code, and I guess my problem is under the catch statement:

    Code:
    Imports System.Text
    
    Public Class Form1
    
        Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChange.Click
            Try
                'DISPLAYS AN ERROR IF THE ITEM COST TEXT BOX CONTAINS A BLANK OR SOME GARBAGE.
                If txtCostInCents.Text = "" Then
                    MessageBox.Show("Please enter a number for the cost of the item.")
                    txtCostInCents.Text = ""
                    txtCostInCents.Focus()
                ElseIf IsNumeric(txtCostInCents.Text) = False Then
                    MessageBox.Show("Please enter a number for the cost of the item.")
                    txtCostInCents.Text = ""
                    txtCostInCents.Focus()
                End If
    
                'DISPLAYS AN ERROR IF THE COST ENTERED IS GREATER THAN 200 CENTS.
                If txtCostInCents.Text > 200 Then
                    MessageBox.Show("The highest cost of an item is $2.")
                    txtCostInCents.Text = ""
                    txtCostInCents.Focus()
                End If
    
                'DISPLAYS AN ERROR IF THE COST ENTERED IS LESS THAN 1 CENT.
                If txtCostInCents.Text < 1 Then
                    MessageBox.Show("Enter a positive number greater than zero for the cost of the item.")
                    txtCostInCents.Text = ""
                    txtCostInCents.Focus()
                End If
    
                'DISPLAYS AN ERROR IF THE AMOUNT PAID TEXT BOX CONTAINS A BLANK OR SOME GARBAGE.
                If txtAmountPaid.Text = "" Then
                    MessageBox.Show("Please enter a number for the amount paid.")
                    txtAmountPaid.Text = ""
                    txtAmountPaid.Focus()
                ElseIf IsNumeric(txtAmountPaid.Text) = False Then
                    MessageBox.Show("Please enter a number for the amount paid.")
                    txtAmountPaid.Text = ""
                    txtAmountPaid.Focus()
                End If
    
                'DISPLAYS AN ERROR IF THE AMOUNT PAID IS GREATER THAN 500 CENTS.
                If txtAmountPaid.Text > 500 Then
                    MessageBox.Show("The largest bill this vending machine accepts is $5.")
                    txtAmountPaid.Text = ""
                    txtAmountPaid.Focus()
                End If
    
                'DISPLAYS AN ERROR IF THE AMOUNT PAID IS LESS THAN 1 CENT.
                If txtAmountPaid.Text < 1 Then
                    MessageBox.Show("Enter a positive number greater than zero for the amount paid.")
                    txtAmountPaid.Text = ""
                    txtAmountPaid.Focus()
                End If
    
            Catch ex As Exception
                Dim Cost, Amount, Change As Decimal
                Dim Dollars, Quarters, Dimes, Nickels, Pennies As Integer
                Dim vChange As StringBuilder
    
                Cost = txtCostInCents.Text
                Amount = txtAmountPaid.Text
                Change = (Amount - Cost) * 0.01
    
                Dollars = Change / 1
                Quarters = Change / 0.25
                Dimes = Change / 0.1
                Nickels = Change / 0.05
                Pennies = Change / 0.01
    
                'DOLLARS VS. DOLLAR
                vChange.Append("Your Change:")
                If Dollars > 1 Then
                    vChange.Append(Dollars & " dollars")
                ElseIf Dollars = 1 Then
                    vChange.Append(Dollars & " dollar")
                End If
                'QUARTERS VS. QUARTER
                If Quarters > 1 Then
                    vChange.Append(Quarters & " quarters")
                ElseIf Quarters = 1 Then
                    vChange.Append(Quarters & " quarter")
                End If
                'DIMES VS. DIME
                If Dimes > 1 Then
                    vChange.Append(Dimes & " dimes")
                ElseIf Dimes = 1 Then
                    vChange.Append(Dimes & " dime")
                End If
                'NICKELS VS. NICKEL
                If Nickels > 1 Then
                    vChange.Append(Nickels & " nickels")
                ElseIf Nickels = 1 Then
                    vChange.Append(Nickels & " nickel")
                End If
                'PENNIES VS. PENNY
                If Pennies > 1 Then
                    vChange.Append(Pennies & " pennies")
                ElseIf Pennies = 1 Then
                    vChange.Append(Pennies & " penny")
                End If
    
                lblChange.Text = vChange.ToString
            End Try
        End Sub
  2. #2
  3. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jan 2014
    Location
    Los Angeles
    Posts
    3
    Rep Power
    0
    Is there an edit button to our posts here?
    Sorry for the double post.
    I forgot to mention that since we have to display the change in the least possible number of coins, there can be no "0 dimes" or 0 "nickels". Instead, the coins with a zero amount get omitted.
  4. #3
  5. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jan 2014
    Location
    Los Angeles
    Posts
    3
    Rep Power
    0
    Sorry! Last post for now (hopefully).
    I figured how to display everything in the label but I just have a problem with the change showing as "3 dimes, 6 nickels, 30 pennies", which is basically the same thing, so it should only show "3 dimes". Period. Hopefully someone can help me within 45 minutes when this is due.

    Here is my revised code:
    Code:
    Imports System.Text
    
    Public Class Form1
    
        Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChange.Click
            Try
                Dim Cost, Amount, Change As Decimal
                Dim Dollars, Quarters, Dimes, Nickels, Pennies As Integer
                Dim vChange As New StringBuilder
    
                Cost = txtCostInCents.Text
                Amount = txtAmountPaid.Text
                Change = (Amount - Cost) * 0.01
    
                Dollars = Change / 1
                Quarters = Change / 0.25
                Dimes = Change / 0.1
                Nickels = Change / 0.05
                Pennies = Change / 0.01
    
                'DOLLARS VS. DOLLAR
                vChange.Append("Your Change:" & vbCrLf)
                If Dollars > 1 Then
                    vChange.Append(Dollars & " dollars" & vbCrLf)
                ElseIf Dollars = 1 Then
                    vChange.Append(Dollars & " dollar" & vbCrLf)
                End If
                'QUARTERS VS. QUARTER
                If Quarters > 1 Then
                    vChange.Append(Quarters & " quarters" & vbCrLf)
                ElseIf Quarters = 1 Then
                    vChange.Append(Quarters & " quarter" & vbCrLf)
                End If
                'DIMES VS. DIME
                If Dimes > 1 Then
                    vChange.Append(Dimes & " dimes" & vbCrLf)
                ElseIf Dimes = 1 Then
                    vChange.Append(Dimes & " dime" & vbCrLf)
                End If
                'NICKELS VS. NICKEL
                If Nickels > 1 Then
                    vChange.Append(Nickels & " nickels" & vbCrLf)
                ElseIf Nickels = 1 Then
                    vChange.Append(Nickels & " nickel" & vbCrLf)
                End If
                'PENNIES VS. PENNY
                If Pennies > 1 Then
                    vChange.Append(Pennies & " pennies")
                ElseIf Pennies = 1 Then
                    vChange.Append(Pennies & " penny")
                End If
    
                lblChange.Text = vChange.ToString
    
            Catch ex As Exception
               [...]
            End Try
        End Sub

Similar Threads

  1. Replies: 0
    Last Post: June 13th, 2012, 07:00 PM
  2. Hug a Vending Machine, Get a Free Coke [VIDEO]
    By RSS_News_User in forum Technology News
    Replies: 0
    Last Post: April 9th, 2012, 12:00 PM
  3. Summary Box: Cupcake vending machine
    By RSS_News_User in forum Technology News
    Replies: 0
    Last Post: March 6th, 2012, 10:00 AM
  4. Replies: 0
    Last Post: December 26th, 2011, 11:00 PM
  5. Application counting and displaying change for vending machine
    By jurdan01 in forum Visual Basic Programming
    Replies: 6
    Last Post: December 13th, 2009, 01:28 AM

IMN logo majestic logo threadwatch logo seochat tools logo