Calculations

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmathews
    New Member
    • Jan 2008
    • 4

    Calculations

    Hi,

    I'm a student and new to the world of programming in Visual Basic. Can someone be so kind to assist me on setting up a program to simulate a cash register where change will be given in denominations? Or is there one out there somewhere I can use as a guide?

    Your help is greatly appreciated.

    thank you,
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Great idea.

    What you have done sofar to complete the task ?

    Comment

    • dmathews
      New Member
      • Jan 2008
      • 4

      #3
      This is what I have coded so far, any help you can give will be much appreciated.
      Thanks


      [CODE=vbnet]Public Class Form1
      'Project: McDonald's Register

      ' Description: This project is to make change for transactions in the proper monetary denominations.
      Dim x As Decimal
      'Declare variables
      Dim amtOfsale As Decimal
      Dim totalTendered As Decimal
      Dim changeDue As Decimal
      Dim coin As Decimal
      Dim quarter As Decimal
      Dim dime As Decimal
      Dim nickel As Decimal
      Dim penny As Decimal
      Dim dollarChange As Decimal
      Dim quarterChange As Decimal
      Dim dimeChange As Decimal
      Dim nickelChange As Decimal
      Dim pennyChange As Decimal


      Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s)

      End Sub

      Private Sub Button1_Click_1 (ByVal sender As System.Object, ByVal e As System.EventArg s)
      'Sets up Currency
      'Transfers the amount to the label
      Label1.Text = x.ToString("C")
      End Sub

      Private Sub AmtOfSaleTextBo x_TextChanged(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles AmtOfSaleTextBo x.TextChanged
      'Convert to Decimal
      amtOfsale = Val(AmtOfSaleTe xtBox.Text)

      End Sub

      Private Sub TotalTenderedTe xtBox_TextChang ed(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles TotalTenderedTe xtBox.TextChang ed
      'Convert to Decimal
      totalTendered = Val(TotalTender edTextBox.Text)
      End Sub

      Private Sub ChangeDueTextBo x_TextChanged(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles ChangeDueTextBo x.TextChanged
      'Assign value to variable
      totalTendered = Decimal.Parse(M e.TotalTendered TextBox.Text)
      changeDue = Decimal.Parse(M e.ChangeDueText Box.Text)

      End Sub

      Private Sub TwentyTextBox_T extChanged(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles TwentyTextBox.T extChanged

      End Sub

      Private Sub TenTextBox_Text Changed(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles TenTextBox.Text Changed

      End Sub

      Private Sub FiveTextBox_Tex tChanged(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles FiveTextBox.Tex tChanged

      End Sub

      Private Sub OneTextBox_Text Changed(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles OneTextBox.Text Changed

      End Sub

      Private Sub QuartersTextBox _TextChanged(By Val sender As System.Object, ByVal e As System.EventArg s) Handles QuartersTextBox .TextChanged
      If coin > 75 Then
      quarter = 3
      coin = coin - 75
      ElseIf coin > 50 Then
      quarter = 2
      coin = coin - 50
      ElseIf coin > 25 Then
      quarter = 1
      coin = coin - 25
      Else
      quarter = 0

      End If
      QuartersTextBox .Text = quarter
      End Sub

      Private Sub DimesTextBox_Te xtChanged(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles DimesTextBox.Te xtChanged
      If coin > 20 Then
      dime = 2
      coin = coin - 20
      ElseIf coin > 10 Then
      dime = 1
      coin = coin - 10
      Else
      dime = 0
      End If
      End Sub

      Private Sub NickelsTextBox_ TextChanged(ByV al sender As System.Object, ByVal e As System.EventArg s) Handles NickelsTextBox. TextChanged
      If coin > 5 Then
      nickel = 1
      coin = coin - 5
      End If
      End Sub

      Private Sub PenniesTextBox_ TextChanged(ByV al sender As System.Object, ByVal e As System.EventArg s) Handles PenniesTextBox. TextChanged
      If coin = 4 Then
      penny = 4
      coin = coin - 4
      ElseIf coin = 3 Then
      penny = 3
      coin = coin - 3
      ElseIf coin = 2 Then
      penny = 2
      coin = coin - 2
      ElseIf coin = 1 Then
      penny = 1
      coin = coin - 1
      End If

      End Sub

      Private Sub ClearSalesButto n_Click(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles ClearSalesButto n.Click

      End Sub

      Private Sub CalculateButton _Click(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles CalculateButton .Click
      'Calculate a total.
      changeDue = (TotalTenderedT extBox.Text - AmtOfSaleTextBo x.Text)
      ChangeDueTextBo x.Text = amtOfsale.ToStr ing
      End Sub
      End Class[/CODE]
      Last edited by Killer42; Jan 22 '08, 02:03 AM. Reason: Added CODE=vbnet tag

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Can you give us some idea of what problem you have, or what you want help with? I mean something a bit more specific?

        Comment

        • dmathews
          New Member
          • Jan 2008
          • 4

          #5
          In lines 32 thru 46 I should be able to enter a dollar amount into the amount of total sales text box and have it subtracted from the total tendered text box and the value of the difference should be able to go to the change due text box. It doesn't work. Also, I am not sure that my calculation button is in the proper place or is it coded sufficiently to make the calculations work.

          Can you help?

          thanks,

          Comment

          • QVeen72
            Recognized Expert Top Contributor
            • Oct 2006
            • 1445

            #6
            Originally posted by dmathews
            In lines 32 thru 46 I should be able to enter a dollar amount into the amount of total sales text box and have it subtracted from the total tendered text box and the value of the difference should be able to go to the change due text box. It doesn't work. Also, I am not sure that my calculation button is in the proper place or is it coded sufficiently to make the calculations work.

            Can you help?

            thanks,
            Hi,

            Remove the Code From Change Due Text Box's Change Event, and check this code:

            [code=vbnet]
            Private Sub AmtOfSaleTextBo x_TextChanged(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles AmtOfSaleTextBo x.TextChanged
            'Convert to Decimal
            amtOfsale = Val(AmtOfSaleTe xtBox.Text)
            ChangeDue = TotalTendered - amtOfSale
            ChangeDueTextBo x = Format(ChangeDu e,"0.00")
            End Sub

            Private Sub TotalTenderedTe xtBox_TextChang ed(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles TotalTenderedTe xtBox.TextChang ed
            'Convert to Decimal
            totalTendered = Val(TotalTender edTextBox.Text)
            ChangeDue = TotalTendered - amtOfSale
            ChangeDueTextBo x = Format(ChangeDu e,"0.00")
            End Sub

            [/code]


            It is always a better programming practice, if you Write the Calculation Part in some Sub Procedure, and call the Proc wherever required..

            Regards
            Veena

            Comment

            • dmathews
              New Member
              • Jan 2008
              • 4

              #7
              Can you give me a detailed suggestion on how I should set up my calculation button to show the answers in the denominations of 20's, 10's, 5's, 1's and currency change? This is what I have so far, I'm unsure how to set it up.

              Private Sub CalculateButton _Click(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles CalculateButton .Click
              'Calculate a total.
              changeDue = (TotalTenderedT extBox.Text - AmtOfSaleTextBo x.Text)
              ChangeDueTextBo x.Text = amtOfsale.ToStr ing

              thanks,

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                It looks to me as though you have already done the work to break up the change into specific numbers of specific coins. It's just that you have tried to do it in pieces, in the TextChanged events for the textboxes which (I think) are intended to display the resulting values.

                I believe you need to do the calculation in a dedicated Sub or Function routine as Veena suggested. And after calculating the amount, proceed to break it down into the different coins.

                Once you've done that, display the values of the variables quarter, dime and so on in their appropriate places.

                Comment

                Working...