By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,530 Members | 891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,530 IT Pros & Developers. It's quick & easy.

Calculations

P: 4
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,
Jan 18 '08 #1
Share this Question
Share on Google+
7 Replies


debasisdas
Expert 5K+
P: 8,127
Great idea.

What you have done sofar to complete the task ?
Jan 18 '08 #2

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


Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     'Project:  McDonald's Register
  3.  
  4.     ' Description:  This project is to make change for transactions in the proper monetary denominations.
  5.     Dim x As Decimal
  6.     'Declare variables
  7.     Dim amtOfsale As Decimal
  8.     Dim totalTendered As Decimal
  9.     Dim changeDue As Decimal
  10.     Dim coin As Decimal
  11.     Dim quarter As Decimal
  12.     Dim dime As Decimal
  13.     Dim nickel As Decimal
  14.     Dim penny As Decimal
  15.     Dim dollarChange As Decimal
  16.     Dim quarterChange As Decimal
  17.     Dim dimeChange As Decimal
  18.     Dim nickelChange As Decimal
  19.     Dim pennyChange As Decimal
  20.  
  21.  
  22.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  23.  
  24.     End Sub
  25.  
  26.     Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
  27.         'Sets up Currency
  28.         'Transfers the amount to the label
  29.         Label1.Text = x.ToString("C")
  30.     End Sub
  31.  
  32.     Private Sub AmtOfSaleTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AmtOfSaleTextBox.TextChanged
  33.         'Convert to Decimal
  34.         amtOfsale = Val(AmtOfSaleTextBox.Text)
  35.  
  36.     End Sub
  37.  
  38.     Private Sub TotalTenderedTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TotalTenderedTextBox.TextChanged
  39.         'Convert to Decimal
  40.         totalTendered = Val(TotalTenderedTextBox.Text)
  41.     End Sub
  42.  
  43.     Private Sub ChangeDueTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChangeDueTextBox.TextChanged
  44.         'Assign value to variable
  45.         totalTendered = Decimal.Parse(Me.TotalTenderedTextBox.Text)
  46.         changeDue = Decimal.Parse(Me.ChangeDueTextBox.Text)
  47.  
  48.     End Sub
  49.  
  50.     Private Sub TwentyTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TwentyTextBox.TextChanged
  51.  
  52.     End Sub
  53.  
  54.     Private Sub TenTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TenTextBox.TextChanged
  55.  
  56.     End Sub
  57.  
  58.     Private Sub FiveTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FiveTextBox.TextChanged
  59.  
  60.     End Sub
  61.  
  62.     Private Sub OneTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OneTextBox.TextChanged
  63.  
  64.     End Sub
  65.  
  66.     Private Sub QuartersTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuartersTextBox.TextChanged
  67.         If coin > 75 Then
  68.             quarter = 3
  69.             coin = coin - 75
  70.         ElseIf coin > 50 Then
  71.             quarter = 2
  72.             coin = coin - 50
  73.         ElseIf coin > 25 Then
  74.             quarter = 1
  75.             coin = coin - 25
  76.         Else
  77.             quarter = 0
  78.  
  79.         End If
  80.         QuartersTextBox.Text = quarter
  81.     End Sub
  82.  
  83.     Private Sub DimesTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DimesTextBox.TextChanged
  84.         If coin > 20 Then
  85.             dime = 2
  86.             coin = coin - 20
  87.         ElseIf coin > 10 Then
  88.             dime = 1
  89.             coin = coin - 10
  90.         Else
  91.             dime = 0
  92.         End If
  93.     End Sub
  94.  
  95.     Private Sub NickelsTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NickelsTextBox.TextChanged
  96.         If coin > 5 Then
  97.             nickel = 1
  98.             coin = coin - 5
  99.         End If
  100.     End Sub
  101.  
  102.     Private Sub PenniesTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PenniesTextBox.TextChanged
  103.         If coin = 4 Then
  104.             penny = 4
  105.             coin = coin - 4
  106.         ElseIf coin = 3 Then
  107.             penny = 3
  108.             coin = coin - 3
  109.         ElseIf coin = 2 Then
  110.             penny = 2
  111.             coin = coin - 2
  112.         ElseIf coin = 1 Then
  113.             penny = 1
  114.             coin = coin - 1
  115.         End If
  116.  
  117.     End Sub
  118.  
  119.     Private Sub ClearSalesButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearSalesButton.Click
  120.  
  121.     End Sub
  122.  
  123.     Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
  124.         'Calculate a total.
  125.         changeDue = (TotalTenderedTextBox.Text - AmtOfSaleTextBox.Text)
  126.         ChangeDueTextBox.Text = amtOfsale.ToString
  127.     End Sub
  128. End Class
Jan 21 '08 #3

Expert 5K+
P: 8,434
Can you give us some idea of what problem you have, or what you want help with? I mean something a bit more specific?
Jan 22 '08 #4

P: 4
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,
Jan 22 '08 #5

QVeen72
Expert 100+
P: 1,445
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:

Expand|Select|Wrap|Line Numbers
  1. Private Sub AmtOfSaleTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AmtOfSaleTextBox.TextChanged
  2.         'Convert to Decimal
  3.         amtOfsale = Val(AmtOfSaleTextBox.Text)
  4.         ChangeDue = TotalTendered - amtOfSale 
  5.         ChangeDueTextBox = Format(ChangeDue,"0.00")
  6.     End Sub
  7.  
  8.     Private Sub TotalTenderedTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TotalTenderedTextBox.TextChanged
  9.         'Convert to Decimal
  10.         totalTendered = Val(TotalTenderedTextBox.Text)
  11.         ChangeDue = TotalTendered - amtOfSale 
  12.         ChangeDueTextBox = Format(ChangeDue,"0.00")
  13.     End Sub
  14.  
  15.  

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
Jan 22 '08 #6

P: 4
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.EventArgs) Handles CalculateButton.Click
'Calculate a total.
changeDue = (TotalTenderedTextBox.Text - AmtOfSaleTextBox.Text)
ChangeDueTextBox.Text = amtOfsale.ToString

thanks,
Jan 26 '08 #7

Expert 5K+
P: 8,434
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.
Jan 27 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.