428,530 Members | 891 Online
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
7 Replies

 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 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(ByVal sender As System.Object, ByVal e As System.EventArgs)       End Sub       Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)         'Sets up Currency         'Transfers the amount to the label         Label1.Text = x.ToString("C")     End Sub       Private Sub AmtOfSaleTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AmtOfSaleTextBox.TextChanged         'Convert to Decimal         amtOfsale = Val(AmtOfSaleTextBox.Text)       End Sub       Private Sub TotalTenderedTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TotalTenderedTextBox.TextChanged         'Convert to Decimal         totalTendered = Val(TotalTenderedTextBox.Text)     End Sub       Private Sub ChangeDueTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChangeDueTextBox.TextChanged         'Assign value to variable         totalTendered = Decimal.Parse(Me.TotalTenderedTextBox.Text)         changeDue = Decimal.Parse(Me.ChangeDueTextBox.Text)       End Sub       Private Sub TwentyTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TwentyTextBox.TextChanged       End Sub       Private Sub TenTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TenTextBox.TextChanged       End Sub       Private Sub FiveTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FiveTextBox.TextChanged       End Sub       Private Sub OneTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OneTextBox.TextChanged       End Sub       Private Sub QuartersTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 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_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DimesTextBox.TextChanged         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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NickelsTextBox.TextChanged         If coin > 5 Then             nickel = 1             coin = coin - 5         End If     End Sub       Private Sub PenniesTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 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 ClearSalesButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearSalesButton.Click       End Sub       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     End Sub 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

 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 Private Sub AmtOfSaleTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AmtOfSaleTextBox.TextChanged         'Convert to Decimal         amtOfsale = Val(AmtOfSaleTextBox.Text)         ChangeDue = TotalTendered - amtOfSale          ChangeDueTextBox = Format(ChangeDue,"0.00")     End Sub       Private Sub TotalTenderedTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TotalTenderedTextBox.TextChanged         'Convert to Decimal         totalTendered = Val(TotalTenderedTextBox.Text)         ChangeDue = TotalTendered - amtOfSale          ChangeDueTextBox = Format(ChangeDue,"0.00")     End Sub     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