Introduction to Programming Using Visual Basic 2005, An (6th Edition)

[Page 235 (continued)]

5.4. A Case Study: Weekly Payroll

This case study processes a weekly payroll using the 2005 Employer's Tax Guide. Table 5.9 shows typical data used by a company's payroll office. These data are processed to produce the information in Table 5.10 that is supplied to each employee along with his or her paycheck. The program should request the data from Table 5.9 for an individual as input and produce output similar to that in Table 5.10.


[Page 236]

Table 5.9. Employee data.
(This item is displayed on page 235 in the print version)

Name

Hourly Wage

Hours Worked

Withholding Exemptions

Marital Status

Previous Year-to-Date Earnings

Al Clark

$45.50

38

4

Married

$88,600.00

Ann Miller

$44.00

35

3

Married

$68,200.00

John Smith

$17.95

50

1

Single

$30,604.75

Sue Taylor

$25.50

43

2

Single

$36,295.50

Table 5.10. Payroll information.

Name

Current Earnings

Yr. to Date Earnings

FICA Taxes

Income Tax Wh.

Check Amount

Al Clark

$1,729.00

$90,329.00

$111.87

$206.26

$1,410.87

The items in Table 5.10 should be calculated as follows:

Current Earnings: hourly wage times hours worked (with time-and-a-half after 40 hours)

Year-to-Date Earnings: previous year-to-date earnings plus current earnings

FICA Taxes: sum of 6.2 percent of first $90,000 of earnings (Social Security benefits tax) and 1.45 percent of total wages (Medicare tax)

Federal Income Tax Withheld: subtract $61.54 from the current earnings for each withholding exemption and use Table 5.11 or Table 5.12, depending on marital status

Check Amount:[current earnings] - [FICA taxes] - [income tax withheld]

Table 5.11. 2005 Federal income tax withheld for a single person paid weekly.

Adjusted Weekly Income

Income Tax Withheld

$0 to $51

$0

Over $51 to $188

10% of amount over $51

Over $188 to $606

$13.70 + 15% of amount over $188

Over $606 to $1,341

$76.40 + 25% of amount over $606

Over $1,341 to $2,922

$260.15 + 28% of amount over $1,341

Over $2,922 to $6,313

$702.83 + 33% of amount over $2,922

Over $6,313

$1,821.86 + 35% of amount over $6,313

Table 5.12. 2005 Federal income tax withheld for a married person paid weekly.

Adjusted Weekly Income

Income Tax Withheld

$0 to $154

$0

Over $154 to $435

10% of amount over $154

Over $435 to $1,273

$28.10 + 15% of amount over $435

Over $1,273 to $2,322

$153.80 + 25% of amount over $1,273

Over $2,322 to $3,646

$416.05 + 28% of amount over $2,322

Over $3,646 to $6,409

$786.77 + 33% of amount over $3,646

Over $6,409

$1,698.56 + 35% of amount over $6,409


[Page 237]

Designing the Weekly Payroll Program

After the data for an employee have been gathered from the text boxes, the program must compute the five items appearing in Table 5.10 and then display the payroll information. The five computations form the basic tasks of the program:

1.

Compute current earnings.

2.

Compute year-to-date earnings.

3.

Compute FICA tax.

4.

Compute federal income tax withheld.

5.

Compute paycheck amount (that is, take-home pay).

Tasks 1, 2, 3, and 5 are fairly simple. Each involves applying a formula to given data. (For instance, if hours worked are at most 40, then [Current Earnings] = [Hourly Wage] times [Hours Worked].) Thus, we won't break down these tasks any further. Task 4 is more complicated, so we continue to divide it into smaller subtasks.

4. Compute Federal Income Tax Withheld. First, the employee's pay is adjusted for exemptions, and then the amount of income tax to be withheld is computed. The computation of the income tax withheld differs for married and single individuals. Task 4 is, therefore, divided into the following subtasks:

4.1. Compute pay adjusted by exemptions.

4.2. Compute income tax withheld for single employee.

4.3. Compute income tax withheld for married employee.

The hierarchy chart in Figure 5.6 shows the stepwise refinement of the problem.

Figure 5.6. Hierarchy chart for the weekly payroll program.

Pseudocode for the Display Payroll Event

INPUT employee data (Sub procedure InputData)COMPUTE CURRENT GROSS PAY (Function Gross_Pay)COMPUTE TOTAL EARNINGS TO DATE (Function Total_Pay)COMPUTE FICA TAX (Function FICA_Tax)

COMPUTE FEDERAL TAX (Function Fed_Tax)


[Page 238]

    Adjust pay for exemptions

    If employee is single Then

      COMPUTE INCOME TAX WITHHELD (Function TaxSingle)    Else

      COMPUTE INCOME TAX WITHHELD (Function TaxMarried)

    End If

      COMPUTE PAYCHECK AMOUNT (Function Net_Check)

DISPLAY PAYROLL INFORMATION (Sub procedure ShowPayroll)

Writing the Weekly Payroll Program

The btnDisplay_Click event procedure calls a sequence of seven procedures. Table 5.13 shows the tasks and the procedures that perform the tasks.

Table 5.13. Tasks and their procedures.

Task

Procedure

0. Input employee data.

InputData

1. Compute current earnings.

Gross_Pay

2. Compute year-to-date earnings.

Total_Pay

3. Compute FICA tax.

FICA_Tax

4. Compute federal income tax withheld.

Fed_Tax

4.1 Compute adjusted pay.

Fed_Tax

4.2 Compute amount withheld for single employee.

TaxSingle

4.3 Compute amount withheld for married employee.

TaxMarried

5. Compute paycheck amount.

Net_Check

6. Display payroll information.

ShowPayroll

The Program and the User Interface

Figure 5.7 and Table 5.14 define the user interface for the Weekly Payroll Program.

Figure 5.7. Template for entering payroll data.


[Page 239]

Table 5.14. Objects and initial properties for the weekly payroll program.

Object

Property

Setting

frmPayroll

Text

Weekly Payroll

lblName

Text

Employee Name:

txtName

  

lblWage

Text

Hourly Wage:

txtWage

  

lblHours

Text

Number of Hours Worked:

txtHours

  

lblExempts

Text

Number of Exemptions:

txtExempts

  

lblMarital

Text

Marital Status (M or S):

mtxtMarital

Mask

L

lblPriorPay

Text

Total Pay Prior to this Week:

txtPriorPay

  

btnDisplay

Text

Display Payroll

btnNext

Text

Next Employee

btnQuit

Text

Quit

lstResults

  

Figure 5.8. Sample run of weekly payroll program.


[Page 240]

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim empName As String = "" 'Name of employee Dim hrWage As Double 'Hourly wage Dim hrsWorked As Double 'Hours worked this week Dim exemptions As Integer 'Number of exemptions for employee Dim mStatus As String = "" 'Marital status: S - Single; M - Married Dim prevPay As Double 'Total pay for year excluding this week Dim pay As Double 'This week's pay before taxes Dim totalPay As Double 'Total pay for year including this week Dim ficaTax As Double 'FICA taxes for this week Dim fedTax As Double 'Federal income tax withheld this week Dim check As Double 'Paycheck this week (take-home pay) 'Obtain data, compute payroll, display results InputData(empName, hrWage, hrsWorked, exemptions, _ mStatus, prevPay) 'Task 0 pay = Gross_Pay(hrWage, hrsWorked) 'Task 1 totalPay = Total_Pay(prevPay, pay) 'Task 2 ficaTax = FICA_Tax(pay, prevPay, totalPay) 'Task 3 fedTax = Fed_Tax(pay, exemptions, mStatus) 'Task 4 check = Net_Check(pay, ficaTax, fedTax) 'Task 5 ShowPayroll(empName, pay, totalPay, ficaTax, fedTax, check) 'Task 6 End Sub Private Sub btnNext_Click(...) Handles btnNext.Click 'Clear all masked text boxes for next employee's data txtName.Clear() txtWage.Clear() txtHours.Clear() txtExempts.Clear() mtxtMarital.Clear() txtPriorPay.Clear() lstResults.Items.Clear() End Sub Private Sub btnQuit_Click(...) Handles btnQuit.Click End End Sub Sub InputData(ByRef empName As String, ByRef hrWage As Double, _ ByRef hrsWorked As Double, ByRef exemptions As Integer, _ ByRef mStatus As String, ByRef prevPay As Double) 'Task 0: Get payroll data for employee empName = txtName.Textd hrWage = CDbl(txtWage.Text) hrsWorked = CDbl(txtHours.Text) exemptions = CInt(txtExempts.Text) mStatus = mtxtMarital.Text.ToUpper.Substring(0, 1) 'M or S prevPay = CDbl(txtPriorPay.Text) End Sub Function Gross_Pay(ByVal hrWage As Double, ByVal hrsWorked As Double) _ As Double 'Task 1: Compute weekly pay before taxes If hrsWorked <= 40 Then Return hrsWorked * hrWage


[Page 241]
Else Return 40 * hrWage + (hrsWorked - 40) * 1.5 * hrWage End If End Function Function Total_Pay(ByVal prevPay As Double, _ ByVal pay As Double) As Double 'Task 2: Compute total pay before taxes Return prevPay + pay End Function Function FICA_Tax(ByVal pay As Double, ByVal prevPay As Double, _ ByVal totalPay As Double) As Double 'Task 3: Compute social security and medicare tax Dim socialSecurity As Double 'Social Security tax for this week Dim medicare As Double 'Medicare tax for this week Dim sum As Double 'Sum of above two taxes If totalPay <= 90000 Then socialSecurity = 0.062 * pay ElseIf prevPay < 90000 Then socialSecurity = 0.062 * (90000 - prevPay) End If medicare = 0.0145 * pay sum = socialSecurity + medicare Return Math.Round(sum, 2) 'Round to nearest cent End Function Function Fed_Tax(ByVal pay As Double, ByVal exemptions As Integer, _ ByVal mStatus As String) As Double 'Task 4.1: Compute federal income tax rounded to two decimal places Dim adjPay As Double Dim tax As Double 'Unrounded federal tax adjPay = pay - (61.54 * exemptions) If adjPay < 0 Then adjPay = 0 End If If mStatus = "S" Then tax = TaxSingle(adjPay) 'Task 4.2 Else tax = TaxMarried(adjPay) 'Task 4.3 End If Return Math.Round(tax, 2) 'Round to nearest cent End Function Function TaxSingle(ByVal adjPay As Double) As Double 'Task 4.2: Compute federal tax withheld for single person Select Case adjPay Case 0 To 51 Return 0 Case 51 To 188 Return 0.1 * (adjPay - 51) Case 188 To 606 Return 13.7 + 0.15 * (adjPay - 188) Case 606 To 1341 Return 76.4 + 0.25 * (adjPay - 606)
[Page 242]
Case 1341 To 2922 Return 260.15 + 0.28 * (adjPay - 1341) Case 2922 To 6313 Return 702.83 + 0.33 * (adjPay - 2922) Case Is > 6313 Return 1821.86 + 0.35 * (adjPay - 6313) End Select End Function Function TaxMarried(ByVal adjPay As Double) As Double 'Task 4.3: Compute federal tax withheld for married person Select Case adjPay Case 0 To 154 Return 0 Case 154 To 435 Return 0.1 * (adjPay - 154) Case 435 To 1273 Return 28.1 + 0.15 * (adjPay - 435) Case 1273 To 2322 Return 153.8 + 0.25 * (adjPay - 1273) Case 2322 To 3646 Return 416.05 + 0.28 * (adjPay - 2322) Case 3646 To 6409 Return 786.77 + 0.33 * (adjPay - 3646) Case Is > 6409 Return 1698.56 + 0.35 * (adjPay - 6409) End Select End Function Function Net_Check(ByVal pay As Double, ByVal ficaTax As Double, _ ByVal fedTax As Double) As Double 'Task 5: Compute amount of money given to employee Return pay - ficaTax - fedTax End Function Sub ShowPayroll(ByVal empName As String, ByVal pay As Double, _ ByVal totalPay As Double, ByVal ficaTax As Double, _ ByVal fedTax As Double, ByVal check As Double) 'Task 6: Display results of payroll computations Dim fmtStr As String = "{0,24} {1,-10:C}" With lstResults.Items .Clear() .Add("Payroll results for " & empName) .Add("") .Add(String.Format(fmtStr, "Gross pay this period:", pay)) .Add("") .Add(String.Format(fmtStr, "Year-to-date earnings:", totalPay)) .Add("") .Add(String.Format(fmtStr, "Fica Taxes this period:", ficaTax)) .Add("") .Add(String.Format(fmtStr, "Income tax withheld:", fedTax)) .Add("") .Add(String.Format(fmtStr, "Net pay (check amount):", check)) End With End Sub


[Page 243]

Comments

  1. In the function FICA_Tax, care has been taken to avoid computing Social Security benefits tax on income in excess of $90,000 per year. The logic of the program makes sure an employee whose income crosses the $90,000 threshold during a given week is taxed only on the difference between $90,000 and his previous year-to-date income.

  2. The two functions TaxMarried and TaxSingle use Select Case blocks to incorporate the tax brackets given in Tables 5.11 and 5.12 for the amount of federal income tax withheld. The upper limit of each Case clause is the same as the lower limit of the next Case clause. This ensures that fractional values for adjPay, such as 61.54 in the TaxSingle function, will be properly treated as part of the higher salary range.

Категории