Help With Calculation Logic

netasp

Member
Joined
Feb 27, 2006
Messages
5
Programming Experience
1-3
hi all,
I have a form where user enters productID and price and it will sum the total in a textbox txtTotal.text, then the user will select type of payments (cash, master, visa, ..etc) and will input tendered amount in txtTendered.text and the Balance amount will show in a textbox txtBalance.text. Now if the total amount is 45.99 and the user enters 50.00 in txtTendered.text then txtBlance will show 4.01 (thats easy), if user decides to pay 5.00 cash and 5 visa, 5 master, ...etc what is the most effecient way to calculate this? i mean i am using public variables and its getting crazy. when there txtBalance.text = 0 (no outstanding amount) then the sale form should be closed and the main form will appear.

I am using MSDE. should i just create a datatable to load values everytime the user enters amount? would it slow down the process this way? if multiuser were to be used?

i am using the following code:

VB.NET:
 Private Function updatePayment(ByVal myTotal As Double) As Boolean 
        ' This sub updates payment amount entered by the user Dim dTotal As Double 
        dTotal = CType(txtTotal.Text, Double) 
 
        ' Check if user payment is more or less than the total 
        If (myTotal >= dTotal) Then 
            ' Show balance due to customer 
            txtBalance.Text = returnBalanceDue(dTotal, CType(txtTendered.Text, Double)) ' basically it just subtract! 
            ' Finaliaze sale and update DataSet 
            ... 
 
            frmMain.lblChangeBack.Text = txtBalance.Text 
            Return True 
        Else 
            Return False 
        End If 
    End Function

then i am calling it in the following procedure:

VB.NET:
Private Sub txtTendered_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTendered.KeyPress 
                If (updatePaymentAmount(CType(txtTendered.Text, Double))) Then 
                    ' No outstanding balance 
                    ' Update change back label 
                    Me.Close() 
                    Me.Dispose() 
                Else 
                    ' There is an outstanding balance 
                    ' Send focus back to type of payment to select another type 
                    objSalesBalance.GetBalance = CType(txtTendered.Text, Double) ' public property check below 
                    ctrBalance = objSalesBalance.GetBalance 
                    txtPaymentType.Text = "" 
                    txtTendered.Text = "" 
                    txtPaymentType.Focus() 
                    txtBalance.Text = returnBalanceDue(CType(txtTotal.Text, Double), ctrBalance) 
                End If 
    End Sub


VB.NET:
Public Class TransactionClass 
    Public salesTotal As Double ' Stores sales balance 
 
    Public Property GetBalance() As Double 
        Get 
            Return salesTotal 
        End Get 
        Set(ByVal value As Double) 
            salesTotal = salesTotal + value 
        End Set 
    End Property 
End Class

I just feel there is a better way of accomplishing this.
if anyone knows how to do this, please let me know. thank you
 
I think you need to a little bit more object oriented here, because to me, Cash sounds like an object so should have it's own class, as should visa, cheque etc..

In the classes you can have methods and properties to hold the relavent info and finally a shared method to retrieve the outstanding balance. This way you'll have much more control over what happens
 
thanks vis781 for your reply,
very good point! but what do you think of using DataTable to store the payment types instead of creating an object for cash and object for visa,..etc? i am talking about performace wise? would it better or not?

I am trying to reach to best way to do it. :D

thanks again
 
Back
Top