Home

Visual J# Examples: Employee Payroll

 

Introduction

In this example, we will calculate the overtime worked by an employee of a company. To do this, we will consider each week day with 8 regular hours. If an employee works more than 8 hours in one day, any time over that is considered overtime. In our payroll simulation, we will cover two weeks but each week has its separate calculation. After collecting the time for both weeks and performing the calculation, we will display the number of regular hours, the number of hours worked overtime, the amount pay for the regular hours, and the amount pay for overtime, if any. At the end, we will display the total net pay.

Practical LearningPractical Learning: Introducing the Date Picker

  1. Start a new Visual J# Windows Application named Payroll1
  2. Set the form's Icon to Drive:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Graphics\icons\Office\CRDFLE03.ICO
  3. Design the form as follows:
     
    Georgetown Cleaning Services - Employee Payroll - Form Design
    Control Name Text Other Properties
    GroupBox GroupBox   Employee Identification  
    Label Label   Employee #:  
    TextBox TextBox txtEmployeeNbr    
    Label Label   Hourly Salary:  
    TextBox TextBox txtHourlySalary    
    GroupBox GroupBox   Time Period  
    Label Label Starting Date:    
    DateTimePicker DateTimePicker dtpStartingDate    
    Label Label End Date:    
    DateTimePicker DateTimePicker dtpEndDate    
    GroupBox GroupBox   Time Sheet  
    Label Label   Monday  
    Label Label   Tuesday  
    Label Label   Wednesday  
    Label Label   Thursday  
    Label Label   Friday  
    Label Label   Saturday  
    Label Label   Sunday  
    Label Label   First Week:  
    TextBox TextBox txtMonday1 0.00 TextAlign: Right
    TextBox TextBox txtTuesday1 0.00 TextAlign: Right
    TextBox TextBox txtWednesday1 0.00 TextAlign: Right
    TextBox TextBox txtThursday1 0.00 TextAlign: Right
    TextBox TextBox txtFriday1 0.00 TextAlign: Right
    TextBox TextBox txtSaturday1 0.00 TextAlign: Right
    TextBox TextBox txtSunday1 0.00 TextAlign: Right
    Label Label   Second Week:  
    TextBox TextBox txtMonday2 0.00 TextAlign: Right
    TextBox TextBox txtTuesday2 0.00 TextAlign: Right
    TextBox TextBox txtWednesday2 0.00 TextAlign: Right
    TextBox TextBox txtThursday2 0.00 TextAlign: Right
    TextBox TextBox txtFriday2 0.00 TextAlign: Right
    TextBox TextBox txtSaturday2 0.00 TextAlign: Right
    TextBox TextBox txtSunday2 0.00 TextAlign: Right
    GroupBox GroupBox   Payroll Processing  
    Label Label   Hours  
    Label Label   Amount  
    Button Button btnProcessIt Process It  
    Label Label   Regular  
    TextBox TextBox txtRegularHours 0.00 TextAlign: Right
    TextBox TextBox txtRegularAmount 0.00 TextAlign: Right
    Label Label   Total Earnings  
    TextBox TextBox txtTotalEarnings 0.00 TextAlign: Right
    Label Label   Overtime  
    TextBox TextBox txtOvertimeHours 0.00 TextAlign: Right
    TextBox TextBox txtOvertiimeAmount 0.00 TextAlign: Right
    Button Button btnClose    
  4. Double-click the Close button and implement its Click event as follows:
     
    private void btnClose_Click (Object sender, System.EventArgs e)
    {
    	Close();
    }
  5. Execute it to test it and then close the form
  6. Double-click an unoccupied area of the form and implement its Load event as follows:
     
    private void Form1_Load (Object sender, System.EventArgs e)
    {
    	System.DateTime todayDate = System.DateTime.get_Today();
    	System.DateTime twoWeeksAgo = todayDate.AddDays(-14);
    
    	this.dtpStartingDate.set_Value(twoWeeksAgo);
    }
  7. Return to the form and click the top DateTimePicker control
  8. In the Properties window, click the Events button Events and double-click the CloseUp field
  9. Implement the event as follows:
     
    private void dtpStartingDate_CloseUp (Object sender, System.EventArgs e)
    {
    	// Get the starting date
    	System.DateTime dteStart = this.dtpStartingDate.get_Value();
    
    	// Find out if the user selected a day that is not Monday
    	if( dteStart.get_DayOfWeek() != System.DayOfWeek.Monday )
    	{
    		MessageBox.Show("The date you selected in invalid\n" +
    				"The time period should start on a Monday");
    
    		this.dtpStartingDate.Focus();
    	}
    }
  10. Return to the form and click the other DateTimePicker control
  11. In the Properties window, click the Events button Events and double-click the CloseUp field
  12. Implement the event as follows:
     
    private void dtpEndDate_CloseUp (Object sender, System.EventArgs e)
    {
    	// Get the starting date
    	System.DateTime dteStart = this.dtpStartingDate.get_Value();
    	// Get the ending date
    	System.DateTime dteEnd   = this.dtpEndDate.get_Value();
    			
    	// Make sure the first day of the period is Monday
    	if( dteStart.get_DayOfWeek() != System.DayOfWeek.Monday )
    	{
    		MessageBox.Show("The starting date you selected in invalid\n" +
    				"The time period should start on a Monday");
    
    		this.dtpStartingDate.Focus();
    	}
    		
    	// Find the number of days that separate the start and end
    	System.TimeSpan timeDifference = dteEnd.Subtract(dteStart);
    	double fourteenDaysLater = timeDifference.get_Days();
    
    	if( (dteEnd.get_DayOfWeek() != System.DayOfWeek.Sunday) || (fourteenDaysLater != 13) )
    	{
    		MessageBox.Show("The ending date you selected in invalid\n" +
    				"The time period should span 2 weeks and end on a Sunday");
    
    		this.dtpEndDate.Focus();
    	}
    }
  13. Return to the form. Double-click the Process It button and implement its Click event as follows:
     
    private void btnPresseIt_Click (Object sender, System.EventArgs e)
    {
    	double monday1 = 0.00, tuesday1 = 0.00, wednesday1 = 0.00, thursday1 = 0.00,
    		friday1 = 0.00, saturday1 = 0.00, sunday1 = 0.00,
    		monday2 = 0.00, tuesday2 = 0.00, wednesday2 = 0.00, thursday2 = 0.00,
    		friday2 = 0.00, saturday2 = 0.00, sunday2 = 0.00;
    	double totalHoursWeek1, totalHoursWeek2;
    
    	double regHours1 = 0.00, regHours2 = 0.00, ovtHours1 = 0.00, ovtHours2 = 0.00;
    	double regAmount1 = 0.00, regAmount2 = 0.00, ovtAmount1 = 0.00, ovtAmount2 = 0.00;
    	double regularHours, overtimeHours;
    	double regularAmount, overtimeAmount, totalEarnings;
    
    	double hourlySalary = 0.00;
    
    	// Retrieve the hourly salary
    	try 
    	{
    		hourlySalary = System.Convert.ToDouble(this.txtHourlySalary.get_Text());
    	}
    	catch(System.FormatException ex)
    	{
    		MessageBox.Show("The value you typed for the salary is invalid \n" +
    				"Please try again");
    		this.txtHourlySalary.Focus();
    	}
    
    	// Retrieve the value of each day worked
    	try 
    	{
    		monday1 = System.Convert.ToDouble(this.txtMonday1.get_Text());
    	}
    	catch(System.FormatException ex)
    	{
    		MessageBox.Show("You typed an invalid value\n" +
    				"Please try again");
    		this.txtMonday1.Focus();
    	}
    	try 
    	{
    		tuesday1 = System.Convert.ToDouble(this.txtTuesday1.get_Text());
    	}
    	catch(System.FormatException ex)
    	{
    		MessageBox.Show("You typed an invalid value\n" +
    				"Please try again");
    			this.txtTuesday1.Focus();
    	}
    	try 
    	{
    		wednesday1 = System.Convert.ToDouble(this.txtWednesday1.get_Text());
    	}
    	catch(System.FormatException ex)
    	{
    		MessageBox.Show("You typed an invalid value\n" +
    				"Please try again");
    		this.txtWednesday1.Focus();
    	}
    	try 
    	{
    		thursday1 = System.Convert.ToDouble(this.txtThursday1.get_Text());						  
    	}
    	catch(System.FormatException ex)
    	{
    		MessageBox.Show("You typed an invalid value\n" +
    				"Please try again");
    		this.txtThursday1.Focus();
    	}
    	try 
    	{
    		friday1 = System.Convert.ToDouble(this.txtFriday1.get_Text());
    	}
    	catch(System.FormatException ex)
    	{
    		MessageBox.Show("You typed an invalid value\n" +
    				"Please try again");
    		this.txtFriday1.Focus();
    	}
    	try 
    	{
    		saturday1 = System.Convert.ToDouble(this.txtSaturday1.get_Text());
    	}
    	catch(System.FormatException ex)
    	{
    		MessageBox.Show("You typed an invalid value\n" +
    				"Please try again");
    		this.txtSaturday1.Focus();
    	}
    	try 
    	{
    		sunday1 = System.Convert.ToDouble(this.txtSunday1.get_Text());
    	}
    	catch(System.FormatException ex)
    	{
    		MessageBox.Show("You typed an invalid value\n" +
    				"Please try again");
    		this.txtSunday1.Focus();
    	}
    	try 
    	{
    		monday2 = System.Convert.ToDouble(this.txtMonday2.get_Text());
    	}
    	catch(System.FormatException ex)
    	{
    		MessageBox.Show("You typed an invalid value\n" +
    				"Please try again");
    			this.txtMonday2.Focus();
    	}
    	try 
    	{
    		tuesday2 = System.Convert.ToDouble(this.txtTuesday2.get_Text());
    	}
    	catch(System.FormatException ex)
    	{
    		MessageBox.Show("You typed an invalid value\n" +
    				"Please try again");
    		this.txtTuesday2.Focus();
    	}
    	try 
    	{
    		wednesday2 = System.Convert.ToDouble(this.txtWednesday2.get_Text());
    	}
    	catch(System.FormatException ex)
    	{
    		MessageBox.Show("You typed an invalid value\n" +
    				"Please try again");
    		this.txtWednesday2.Focus();
    	}
    	try 
    	{
    		thursday2 = System.Convert.ToDouble(this.txtThursday2.get_Text());
    	}
    	catch(System.FormatException ex)
    	{
    		MessageBox.Show("You typed an invalid value\n" +
    				"Please try again");
    		this.txtThursday2.Focus();
    	}
    	try 
    	{
    		friday2 = System.Convert.ToDouble(this.txtFriday2.get_Text());
    	}
    	catch(System.FormatException ex)
    	{
    		MessageBox.Show("You typed an invalid value\n" +
    				"Please try again");
    		this.txtFriday2.Focus();
    	}
    	try 
    	{
    		saturday2 = System.Convert.ToDouble(this.txtSaturday2.get_Text());
    	}
    	catch(System.FormatException ex)
    	{
    		MessageBox.Show("You typed an invalid value\n" +
    				"Please try again");
    		this.txtSaturday2.Focus();
    	}
    	try 
    	{
    		sunday2 = System.Convert.ToDouble(this.txtSunday2.get_Text());
    	}
    	catch(System.FormatException ex)
    	{
    		MessageBox.Show("You typed an invalid value\n" +
    				"Please try again");
    		this.txtSunday2.Focus();
    	}
    
    	// Calculate the total number of hours for each week
    	totalHoursWeek1 = monday1 + tuesday1 + wednesday1 + thursday1 +
    			friday1 + saturday1 + sunday1;
    	totalHoursWeek2 = monday2 + tuesday2 + wednesday2 + thursday2 + 
    			friday2 + saturday2 + sunday2;
    
    	// The overtime is paid time and half
    	double ovtSalary = hourlySalary * 1.5;
    
    	// If the employee worked under 40 hours, there is no overtime
    	if( totalHoursWeek1 < 40 )
    	{
    		regHours1  = totalHoursWeek1;
    		regAmount1 = hourlySalary * regHours1;
    		ovtHours1  = 0.00;
    		ovtAmount1 = 0.00;
    	} // If the employee worked over 40 hours, calculate the overtime
    	else if( totalHoursWeek1 >= 40 )
    	{
    		regHours1  = 40;
    		regAmount1 = hourlySalary * 40;
    		ovtHours1  = totalHoursWeek1 - 40;
    		ovtAmount1 = ovtHours1 * ovtSalary;
    	}
    
    	if( totalHoursWeek2 < 40 )
    	{
    		regHours2  = totalHoursWeek2;
    		regAmount2 = hourlySalary * regHours2;
    		ovtHours2  = 0.00;
    		ovtAmount2 = 0.00;
    	}
    	else if( totalHoursWeek2 >= 40 )
    	{
    		regHours2  = 40;
    		regAmount2 = hourlySalary * 40;
    		ovtHours2  = totalHoursWeek2 - 40;
    		ovtAmount2 = ovtHours2 * ovtSalary;
    	}
    
    	regularHours   = regHours1  + regHours2;
    	overtimeHours  = ovtHours1  + ovtHours2;
    	regularAmount  = regAmount1 + regAmount2;
    	overtimeAmount = ovtAmount1 + ovtAmount2;
    	totalEarnings  = regularAmount + overtimeAmount;
    
    	this.txtRegularHours.set_Text(String.Format("{0:F}", (System.Double)regularHours));
    	this.txtOvertimeHours.set_Text(String.Format("{0:F}", (System.Double)overtimeHours));
    	this.txtRegularAmount.set_Text(String.Format("{0:C}", (System.Double)regularAmount));
    	this.txtOvertimeAmount.set_Text(String.Format("{0:C}", (System.Double)overtimeAmount));
    
    	this.txtTotalEarnings.set_Text(String.Format("{0:C}", (System.Double)totalEarnings));
    }
  14. Execute the application to test it
     
    Georgetown Cleaning Services - Employee Payroll
  15. After using it, close the form and return to your programming environment
 

Home Copyright 2004 FunctionX, Inc.