Answered by:
How to update running totals for my program

Question
-
Ok so everything in my program is working correctly so far. The only problem i have is trying to update my totals for the program. I need to show total sales, total pay, and total commission for the program by using the file and then summary key. How the program works is that you get a base pay of $250 and you get a commission rate of .15 if you reach 1000 sales. I have done running totals before but not for one with multiple functions, please help!
Option Strict On Public Class CommissionForm Public GrandTotalSalesAmountDecimal As Decimal Public GrandTotalCommissionAmountDecimal As Decimal Public GrandTotalPayAmountDecimal As Decimal Const QuotaInteger As Integer = 1000 Const BASEPAYInteger As Integer = 250 Const COMMISSIONRATEDecimal As Decimal = 0.15D Private Sub GetInput(ByRef NameString As String, ByRef SalesAmountDecimal As Decimal) 'Input validation With Me If .EmployeeNameTextBox.Text <> "" Then NameString = .EmployeeNameTextBox.Text Try SalesAmountDecimal = Decimal.Parse(.WeeklyAMountTextbox.Text) Catch quantitiesException As FormatException MessageBox.Show("Enter Numeric Data for amount of sales.", "Data Entry Error") End Try Else MessageBox.Show("Please Enter employee name.") End If End With End Sub Private Function SalesPay(ByVal SalesAmountDecimal As Decimal) As Decimal 'Calculates the sales commission If SalesAmountDecimal >= QuotaInteger Then Return (COMMISSIONRATEDecimal * SalesAmountDecimal) + BASEPAYInteger Else Return BASEPAYInteger End If End Function Private Sub PayToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PayToolStripMenuItem.Click 'Displays the information Try Dim nameString As String Dim salesamountdecimal As Decimal Dim commissionamountDecimal As Decimal Dim totalPayString As Decimal Dim totalCommissionAmountString As String GetInput(nameString, salesamountdecimal) If nameString <> "" And salesamountdecimal <> 0 Then totalPayString = SalesPay(salesamountdecimal) commissionamountDecimal = COMMISSIONRATEDecimal * salesamountdecimal MessageBox.Show("Total amount of sales are" & totalPayString & "Total Commission earned is" & totalCommissionAmountString) End If Catch MessageBox.Show("Please enter a number.") End Try End Sub Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click 'exits the program Me.Close() End Sub
Tuesday, March 29, 2011 11:40 PM
Answers
-
To keep track of the different numbers that are associated with one employees sales results, I suggest creating a SalesResults class that contains the data for one employee and calculates commission and total pay when required. Your code (including the running totals) would become something like this:
Public Class CommissionForm Private GrandTotalSalesAmount As Decimal Private GrandTotalCommissionAmount As Decimal Private GrandTotalPayAmount As Decimal Private Function GetSales() As SalesResult Dim salesAmount As Decimal If Me.EmployeeNameTextBox.Text = "" Then MessageBox.Show("Please Enter employee name.") Exit Function End If If Not Decimal.TryParse(Me.WeeklyAMountTextbox.Text, salesAmount) Then MessageBox.Show("Enter Numeric Data for amount of sales.", "Data Entry Error") Exit Function End If Return New SalesResult(Me.EmployeeNameTextBox.Text, salesAmount) End Function Private Sub PayToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PayToolStripMenuItem.Click Dim sales As SalesResult = GetSales() GrandTotalSalesAmount += sales.Sales GrandTotalCommissionAmount += sales.Commission GrandTotalPayAmount += sales.Pay MessageBox.Show("Amount of sales are" & sales.Sales.ToString() & _ "Commission earned is" & sales.Commission.ToString() & _ "Pay including commission is " & sales.Pay.ToString(), _ "Sales for " & sales.Name) MessageBox.Show("Amount of sales are" & GrandTotalSalesAmount.ToString() & _ "Commission earned is" & GrandTotalCommissionAmount.ToString() & _ "Pay including commission is " & GrandTotalPayAmount.ToString(), _ "Running Totals") End Sub End Class Public Class SalesResult Private Const quota As Integer = 1000 Private Const basePay As Integer = 250 Private Const commissionRate As Decimal = 0.15D Private myName As String, mySales As Decimal Sub New(inputName As String, inputSales As Decimal) myName = inputName mySales = inputSales End Sub ReadOnly Property Name As String Get Return myName End Get End Property ReadOnly Property Sales As Decimal Get Return mySales End Get End Property ReadOnly Property Commission As Decimal Get If Sales < quota Then Return 0 Return Sales * commissionRate 'I suggest that (Sales - quota) * commissionRate would be more usual in the real world End Get End Property ReadOnly Property Pay As Decimal Get Return basePay + Commission End Get End Property End Class
- Proposed as answer by Cor Ligthert Wednesday, March 30, 2011 6:26 AM
- Marked as answer by Mike Feng Thursday, April 7, 2011 5:12 AM
Wednesday, March 30, 2011 1:39 AM
All replies
-
To maintain the running total, you can use the following right before or after the MessageBox that displays pay and commission for each sales person.
However, there are a couple of things you might want to check.GrandTotalSalesAmountDecimal += salesamountdecimal GrandTotalCommissionAmountDecimal += commissionamountDecimal GrandTotalPayAmountDecimal += totalPayString
- In your SalesPay method, if sales are above quota, the commission is calculated as total sales * commission rate. Is that what is supposed to happen? I think it would be more likely that the commission is calculated only on the sales that exceed the quota.
- Before displaying the MessageBox, you call SalesPay to calculate total pay and then recalculate the commission as sales * commissionrate. Even if I am wrong in point 1 above, you are still not accounting for the possibility that commission is zero because sales did not exceed the quota. I suggest replacing SalesPay with a method that returns commission. Then you will have sales, commission and pay ready to use in the messagebox and to calculate the running totals.
Wednesday, March 30, 2011 12:03 AM -
the employee gets a commisssion when they are AT or ABOVE the quota, sorry for the confusion. They in turn get the commission rate + base pay of 250.
I am having trouble figuring out which variables to use as the totals..i feel like i am missing one because i think your right i am not accounting for the zero commissions...i think i need to re write some of my calculations but i dont know where to start really. It works on it's own now but i think i am missing a calculation for all of the running totals.
Wednesday, March 30, 2011 12:36 AM -
thank you for your continued help btw.Wednesday, March 30, 2011 12:36 AM
-
also if they do not reach the quota they still get the basepay of 250Wednesday, March 30, 2011 12:42 AM
-
To keep track of the different numbers that are associated with one employees sales results, I suggest creating a SalesResults class that contains the data for one employee and calculates commission and total pay when required. Your code (including the running totals) would become something like this:
Public Class CommissionForm Private GrandTotalSalesAmount As Decimal Private GrandTotalCommissionAmount As Decimal Private GrandTotalPayAmount As Decimal Private Function GetSales() As SalesResult Dim salesAmount As Decimal If Me.EmployeeNameTextBox.Text = "" Then MessageBox.Show("Please Enter employee name.") Exit Function End If If Not Decimal.TryParse(Me.WeeklyAMountTextbox.Text, salesAmount) Then MessageBox.Show("Enter Numeric Data for amount of sales.", "Data Entry Error") Exit Function End If Return New SalesResult(Me.EmployeeNameTextBox.Text, salesAmount) End Function Private Sub PayToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PayToolStripMenuItem.Click Dim sales As SalesResult = GetSales() GrandTotalSalesAmount += sales.Sales GrandTotalCommissionAmount += sales.Commission GrandTotalPayAmount += sales.Pay MessageBox.Show("Amount of sales are" & sales.Sales.ToString() & _ "Commission earned is" & sales.Commission.ToString() & _ "Pay including commission is " & sales.Pay.ToString(), _ "Sales for " & sales.Name) MessageBox.Show("Amount of sales are" & GrandTotalSalesAmount.ToString() & _ "Commission earned is" & GrandTotalCommissionAmount.ToString() & _ "Pay including commission is " & GrandTotalPayAmount.ToString(), _ "Running Totals") End Sub End Class Public Class SalesResult Private Const quota As Integer = 1000 Private Const basePay As Integer = 250 Private Const commissionRate As Decimal = 0.15D Private myName As String, mySales As Decimal Sub New(inputName As String, inputSales As Decimal) myName = inputName mySales = inputSales End Sub ReadOnly Property Name As String Get Return myName End Get End Property ReadOnly Property Sales As Decimal Get Return mySales End Get End Property ReadOnly Property Commission As Decimal Get If Sales < quota Then Return 0 Return Sales * commissionRate 'I suggest that (Sales - quota) * commissionRate would be more usual in the real world End Get End Property ReadOnly Property Pay As Decimal Get Return basePay + Commission End Get End Property End Class
- Proposed as answer by Cor Ligthert Wednesday, March 30, 2011 6:26 AM
- Marked as answer by Mike Feng Thursday, April 7, 2011 5:12 AM
Wednesday, March 30, 2011 1:39 AM -
Hi Hallor,
Thanks for posting in the MSDN Forum.
Any update? I have marked Blackwood's reply as answer, if you think it provides no help, please unmark it, and feel free to let me know your any concerns.
Thank you for your understanding and support.
Mike Feng [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Thursday, April 7, 2011 5:12 AM