Results 1 to 9 of 9

Thread: Trying to write a function to calculate commission

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    12

    Trying to write a function to calculate commission

    Ok here is what I am doing. I have to develop a program that determines how much a salesperson makes. Here is the following instructions:

    A salesperson earns a weekly base salary plus a commission when sales are at or above quota. Create a project that allows the user to input the weekly sales, and teh salesperson name, calculates the commission, and displays summary information.

    Write a function procedure to calculate the commission. The function must compare sales to the quota. When the sales are equal to or greater than the quota, calculate the commission by multiplying sales by the commission rae. Each salesperons receives the base pay plus the commission (if one has been earned).

    I am having trouble developing a function. I have most of the code for the calculate button coded. Here is what I have so far. When the calculate button is pushed it must output the commission(if one is earned), total sales adn the total pay earned.

    VB Code:
    1. 'Program Name       Chapter5ExtraCredit
    2. 'Programmer         Chad R. Bruckman
    3. 'Date               10/15/2006
    4. 'Description        A salesperson earns a weekly base salary
    5. '                   plus a commission when sales weekly sales
    6. '                   are at or above quota.
    7. Public Class salesForm
    8.     'Declare module-level variables for summary information.
    9.     Dim totalCommissionDecimal, totalsalesDecimal, totalpayDecimal As Decimal
    10.     Dim salesCountInteger As Integer
    11.  
    12.     'Declare constants
    13.  
    14.     Const QUOTA_Decimal = 1000
    15.     Const COMMISSION_RATE_Decimal = 0.15F
    16.     Const BASE_PAY_Decimal = 250D
    17.  
    18.     Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
    19.         'Calculate and display the current amounts and add to totals.
    20.         Dim totalPayDecimal, totalAmountDecimal As Decimal
    21.  
    22.         With Me
    23.             'Calculate the total pay.
    24.             With Me
    25.                 Try
    26.                     salesCountInteger = Integer.Parse(.weeklySalesTextBox.Text)
    27.  
    28.                     totalPayDecimal = FindCommission(BASE_PAY_Decimal)
    29.  
    30.  
    31.                     .commissionTextBox.Text = totalCommissionDecimal.ToString("N")
    32.                     .totalSalesTextBox.Text = salesCountInteger.ToString("D")
    33.                     .totalPayTextBox.Text = totalPayDecimal.ToString("C")
    34.                 Catch quantityException As FormatException
    35.                     MessageBox.Show("Input must be numeric.", "Data Entry Error", _
    36.                     MessageBoxButtons.OK, MessageBoxIcon.Information)
    37.                     With .weeklySalesTextBox
    38.                         .Focus()
    39.                         .SelectAll()
    40.                     End With
    41.                 End Try
    42.             End With
    43.         End With
    44.     End Sub
    45.  
    46.     Private Function FindCommission(ByVal totalCommissionDecimal As Decimal) As Decimal
    47.         If QUOTA_Decimal >= salesCountInteger Then
    48.             Return salesCountInteger * COMMISSION_RATE_Decimal
    49.         Else
    50.             Return BASE_PAY_Decimal
    51.         End If
    52.     End Function
    53.  
    54.  
    55. End Class

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Trying to write a function to calculate commission

    This is unnecessary:
    VB Code:
    1. With Me
    2.             'Calculate the total pay.
    3.             With Me

    Also, if you are checking if the sales count is greater than or equal to the quota, it would look like this:
    VB Code:
    1. If QUOTA_Decimal <= salesCountInteger Then

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    12

    Smile Re: Trying to write a function to calculate commission

    Thanks I understand that part. I just can't get the function right to send the commission earned back to the calculate function.

  4. #4
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Trying to write a function to calculate commission

    Use
    VB Code:
    1. Option Strict On
    2. Option Explicit On
    To prevent the use of variants and implicit conversion.

    Then
    VB Code:
    1. Const QUOTA_Decimal As Decimal = 1000
    2.     Const COMMISSION_RATE_Decimal As Decimal = 0.15D
    3.     Const BASE_PAY_Decimal As Decimal = 250D

  5. #5
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Trying to write a function to calculate commission

    I can't quite understand some of the code but I think you said they get commission on top of base pay if they have exceeded their targets. Does your current code not give them either just commission (no base pay) or just the pay without the commission? I don't see where you have

    TotalPay = BaseLinePay + Calculated Commission

    Or have I missed it?

    Stim
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    12

    Re: Trying to write a function to calculate commission

    I am sorry I didn't label my code. The way the program is suppose to work is that if the salesperson gets 1000 or more in sales they get 15% commission on that amount plus their base pay. If they dont' make more that 1000 in sales they just get their base pay. The problem i am having is writing the function to calculate the commission of:


    If they make 1000(QUOTA) or more

    Quota * Commission

    If Not

    they just get basPay

    I can't get this to work in the fucntion.


    Thanks

  7. #7
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Trying to write a function to calculate commission

    I've adopted a slightly different method just to try and get things a little straighter. I'm sure you have too many variables declared...

    Place 4 text boxes and a button on a form:
    Call the text boxes:

    txtWeeklySales
    txtTotalCommission
    txtBasePay
    txtTotalPay

    Then add the following code:
    VB Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. Public Class Form1
    5.     Dim intSales_Count As Integer
    6.     Dim dmlTotalCommission, dmlTotalPay As Decimal
    7.  
    8.     Const QUOTA_Decimal As Decimal = 1000
    9.     Const COMMISSION_RATE_Decimal As Decimal = 0.15D
    10.     Const BASE_PAY_Decimal As Decimal = 250
    11.    
    12.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    13.  
    14.         intSales_Count = Integer.Parse(Me.txtWeeklySales.Text) 'Find Weekly Sales Made
    15.         dmlTotalCommission = CalculateCommission(intSales_Count)  'Calculate Commission
    16.         Me.txtTotalCommission.Text = CStr(dmlTotalCommission)    'Show the commission they have
    17.         Me.txtBasePay.Text = CStr(BASE_PAY_Decimal)              'I added a text box to display base line pay
    18.         dmlTotalPay = BASE_PAY_Decimal + dmlTotalCommission     'Add the base pay + any commission
    19.         Me.txtTotalPay.Text = CStr(dmlTotalPay)                 'Display total pay
    20.  
    21.     End Sub
    22.  
    23.     Private Function CalculateCommission(ByVal intSales_Count As Integer) As Decimal
    24.  
    25.         If QUOTA_Decimal <= intSales_Count Then
    26.             Return intSales_Count * COMMISSION_RATE_Decimal    'This is just commission, base pay has to be added too
    27.         Else
    28.             Return 0                'Don't return the base line pay as the commission!
    29.         End If                      'That's what I was trying to say in my previous post
    30.     End Function                    'You didn't separate them out
    31.  
    32. End Class

    I hope this code is correct, if people disagree with it please let me know and I'll change it.

    You will want to put in your Try statements and so on. You should check out this forum for the IsNumeric command as well to help prevent wrong input from getting into the text boxes.

    Stim
    Last edited by stimbo; Nov 7th, 2006 at 10:32 AM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    12

    Talking Re: Trying to write a function to calculate commission

    Thank you very much. I understand exactly what you are talking about now. This can be very confusing. LOL Thanks!

  9. #9
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Trying to write a function to calculate commission

    Remember to mark this thread as Resolved if your question has been answered (see the tools options).

    Stim
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width