Monday, May 18, 2009

Employee Payroll System

Lab 16 Be sure to put each class in its own file. Add a property to the base class, e.g. numberOfDependents or yearOfBirth. Add code that uses (prints) the new property. Add an additional employee beyond the ones in the sample program.

// Fig. 10.18: ConstructorTest.cs
// Display order in which base class and derived class constructors
// are called.
using System;

public class ConstructorTest
{
public static void Main( string[] args )
{
CommissionEmployee4 employee1 = new CommissionEmployee4( "Bob",
"Lewis", "333-33-3333", 5, 5000.00M, .04M );

CommissionEmployee4 employee5 = new CommissionEmployee4("Mason",
"Lewis", "444-44-4444", 2, 5000.00M, .04M);
} // end Main
} // end class ConstructorTest

============================================================================
// Fig. 10.17: BasePlusCommissionEmployee5.cs
// BasePlusCommissionEmployee5 class declaration.
using System;

public class BasePlusCommissionEmployee5 : CommissionEmployee4
{
private decimal baseSalary; // base salary per week

// six-parameter derived class constructor
// with call to base class CommissionEmployee4 constructor
public BasePlusCommissionEmployee5( string first, string last,
string ssn, int depend, decimal sales, decimal rate, decimal salary )
: base( first, last, ssn, depend, sales, rate )
{
BaseSalary = salary; // validate base salary via property

Console.WriteLine(
"\nBasePlusCommissionEmployee5 constructor:\n" + this );
} // end six-parameter BasePlusCommissionEmployee5 constructor

// property that gets and sets
// base-salaried commission employee's base salary
public decimal BaseSalary
{
get
{
return baseSalary;
} // end get
set
{
baseSalary = ( value < 0 ) ? 0 : value;
} // end set
} // end property BaseSalary

// calculate earnings
public override decimal Earnings()
{
return BaseSalary + base.Earnings();
} // end method Earnings

// return string representation of BasePlusCommissionEmployee5
public override string ToString()
{
return string.Format( "{0} {1}\n{2}: {3:C}",
"base-salaried", base.ToString(), "base salary", BaseSalary );
} // end method ToString
} // end class BasePlusCommissionEmployee5

================================================================
// Fig. 10.16: CommissionEmployee4.cs
// CommissionEmployee4 class represents a commission employee.
using System;

public class CommissionEmployee4
{
private string firstName;
private string lastName;
private string socialSecurityNumber;
private decimal grossSales; // gross weekly sales
private decimal commissionRate; // commission percentage
private int depend;
// five-parameter constructor
public CommissionEmployee4( string first, string last, string ssn, int depends,
decimal sales, decimal rate )
{
// implicit call to object constructor occurs here
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
GrossSales = sales; // validate gross sales via property
CommissionRate = rate; // validate commission rate via property
depend = depends;
Console.WriteLine( "\nCommissionEmployee4 constructor:\n" + this );
} // end five-parameter CommissionEmployee4 constructor

// read-only property that gets commission employee's first name
public string FirstName
{
get
{
return firstName;
} // end get
} // end property FirstName

// read-only property that gets commission employee's last name
public string LastName
{
get
{
return lastName;
} // end get
} // end property LastName

// read-only property that gets
// commission employee's social security number
public string SocialSecurityNumber
{
get
{
return socialSecurityNumber;
} // end get
} // end property SocialSecurityNumber

// property that gets and sets commission employee's gross sales
public int Depend
{
get
{
return depend;
} //end get
}
public decimal GrossSales
{
get
{
return grossSales;
} // end get
set
{
grossSales = ( value < 0 ) ? 0 : value;
} // end set
} // end property GrossSales

// property that gets and sets commission employee's commission rate
public decimal CommissionRate
{
get
{
return commissionRate;
} // end get
set
{
commissionRate = ( value > 0 && value < 1 ) ? value : 0;
} // end set
} // end property CommissionRate

// calculate commission employee's pay
public virtual decimal Earnings()
{
return CommissionRate * GrossSales;
} // end method Earnings

// return string representation of CommissionEmployee object
public override string ToString()
{
return string.Format(
"{0}: {1} {2}\n{3}: {4}\n{5}: {6:C}\n{7}: {8:F2}\n{9}:{10}",
"commission employee", FirstName, LastName,
"social security number", SocialSecurityNumber,
"gross sales", GrossSales, "commission rate", CommissionRate
,"Number of Dependants", Depend);
} // end method ToString
} // end class CommissionEmployee4

No comments:

Post a Comment