C#大学课程(第五版)课后习题12.12修改应付款程序
/*12.12
(修改应付款程序)修改图12.11~图12.15 中的应付款程序,使它包含图12.4~ 图12.9 中的工资程序的完整功能。这个程序需同样能够处理两个Invoice 对象,但现在应对4 种Employee 派生类的每一种处理一个对象。如果当前被处理的对象是BasePlusCommissionEmployee 则程序需将员工的底薪增加10%。最后,程序要对每个对象输出工资额。利用如下步骤来创建这个新程序。
a)修改HourlyEmployee 类( 见图12.6 )和ComissionEmployee 类( 见图12.7),将它们作为实现了JIPayable 的Employee 类版本( 见图12.13)的派生类,放入IPayable 层次中。[提示: 在每个派生类中将Earnings 方法的名称改成GetPaymentAmount ]
b)修改BasePluscommsionEmployee类( 见图12.8),使它扩展在a) 中创建的ComissionEmployee类的版本。
c)修改PayableInterfaceTest(见图12.15 ),多态地处理两个Imoice,一个SalariedEmployee,一个HourlyEmployee,一个ComissionEmployee和一个BasePlusCommissionEmployee。首先要输出每一个IPayable对象的字符串表示。然后如果对象是BasePlusCommissionEmployee,则将它的底薪增加10%。最后,对每个lPayable 对象输出工资额。
*/
using System;
public class BasePlusCommissionEmployee : CommissionEmployee
{
private decimal baseSalary;
public BasePlusCommissionEmployee( string first, string last,
string ssn, decimal sales, decimal rate, decimal salary )
: base( first, last, ssn, sales, rate )
{
BaseSalary = salary; // validate and store base salary
}
public decimal BaseSalary
{
get
{
return baseSalary;
}
set
{
if ( value >= 0 )
baseSalary = value;
else
throw new ArgumentOutOfRangeException( "BaseSalary",
value, "BaseSalary must be >= 0" );
}
}
public override decimal GetPaymentAmount()
{
return BaseSalary + base.GetPaymentAmount();
}
public override string ToString()
{
return string.Format( "{0} {1}; {2}: {3:C}",
"base-salaried", base.ToString(),
"base salary", BaseSalary );
}
}
using System;
public class CommissionEmployee : Employee
{
private decimal grossSales;
private decimal commissionRate;
public CommissionEmployee( string first, string last, string ssn,
decimal sales, decimal rate )
: base( first, last, ssn )
{
GrossSales = sales;
CommissionRate = rate;
}
public decimal GrossSales
{
get
{
return grossSales;
}
set
{
if ( value >= 0 )
grossSales = value;
else
throw new ArgumentOutOfRangeException(
"GrossSales", value, "GrossSales must be >= 0" );
}
}
public decimal CommissionRate
{
get
{
return commissionRate;
}
set
{
if ( value > 0 && value < 1 )
commissionRate = value;
else
throw new ArgumentOutOfRangeException( "CommissionRate",
value, "CommissionRate must be > 0 and < 1" );
}
}
public override decimal GetPaymentAmount()
{
return CommissionRate * GrossSales;
}
public override string ToString()
{
return string.Format( "{0}: {1}\n{2}: {3:C}; {4}: {5:F}",
"commission employee", base.ToString(),
"gross sales", GrossSales,
"commission rate", CommissionRate );
}
}
public abstract class Employee : IPayable
{
public string FirstName { get; private set; }
public string LastName { get; private set; }
public string SocialSecurityNumber { get; private set; }
public Employee( string first, string last, string ssn )
{
FirstName = first;
LastName = last;
SocialSecurityNumber = ssn;
}
public override string ToString()
{
return string.Format( "{0} {1}\nsocial security number: {2}",
FirstName, LastName, SocialSecurityNumber );
}
public abstract decimal GetPaymentAmount();
}
using System;
public class HourlyEmployee : Employee
{
private decimal wage;
private decimal hours;
public HourlyEmployee( string first, string last, string ssn,
decimal hourlyWage, decimal hoursWorked )
: base( first, last, ssn )
{
Wage = hourlyWage;
Hours = hoursWorked;
}
public decimal Wage
{
get
{
return wage;
}
set
{
if ( value >= 0 ) // validation
wage = value;
else
throw new ArgumentOutOfRangeException( "Wage",
value, "Wage must be >= 0" );
}
}
public decimal Hours
{
get
{
return hours;
}
set
{
if ( value >= 0 && value <= 168 ) // validation
hours = value;
else
throw new ArgumentOutOfRangeException( "Hours",
value, "Hours must be >= 0 and <= 168" );
}
}
public override decimal GetPaymentAmount()
{
if ( Hours <= 40M )
return Wage * Hours;
else
return 40M * Wage + ( Hours - 40M ) * Wage * 1.5M;
}
public override string ToString()
{
return string.Format(
"hourly employee: {0}\n{1}: {2:C}; {3}: {4:F}",
base.ToString(), "hourly wage", Wage,
"hours worked", Hours );
}
}
using System;
public class Invoice : IPayable
{
private int quantity;
private decimal pricePerItem;
public string PartNumber { get; set; }
public string PartDescription { get; set; }
public Invoice( string part, string description, int count,
decimal price )
{
PartNumber = part;
PartDescription = description;
Quantity = count;
PricePerItem = price;
}
public int Quantity
{
get
{
return quantity;
}
set
{
if ( value >= 0 ) // validate quantity
quantity = value;
else
throw new ArgumentOutOfRangeException( "Quantity",
value, "Quantity must be >= 0" );
}
}
public decimal PricePerItem
{
get
{
return pricePerItem;
}
set
{
if ( value >= 0 ) // validate price
pricePerItem = value;
else
throw new ArgumentOutOfRangeException( "PricePerItem",
value, "PricePerItem must be >= 0" );
}
}
public override string ToString()
{
return string.Format(
"{0}: \n{1}: {2} ({3}) \n{4}: {5} \n{6}: {7:C}",
"invoice", "part number", PartNumber, PartDescription,
"quantity", Quantity, "price per item", PricePerItem );
}
public decimal GetPaymentAmount()
{
return Quantity * PricePerItem; // calculate total cost
}
}
public interface IPayable
{
decimal GetPaymentAmount();
}
using System;
public class SalariedEmployee : Employee
{
private decimal weeklySalary;
public SalariedEmployee( string first, string last, string ssn,
decimal salary ) : base( first, last, ssn )
{
WeeklySalary = salary; // validate salary via property
}
public decimal WeeklySalary
{
get
{
return weeklySalary;
}
set
{
if ( value >= 0 ) // validation
weeklySalary = value;
else
throw new ArgumentOutOfRangeException( "WeeklySalary",
value, "WeeklySalary must be >= 0" );
}
}
public override decimal GetPaymentAmount()
{
return WeeklySalary;
}
public override string ToString()
{
return string.Format( "salaried employee: {0}\n{1}: {2:C}",
base.ToString(), "weekly salary", WeeklySalary );
}
}
using System;
public class PayableInterfaceTest
{
public static void Main( string[] args )
{
IPayable[] payableObjects = new IPayable[ 6 ];
payableObjects[ 0 ] = new Invoice( "01234", "seat", 2, 375M );
payableObjects[ 1 ] = new Invoice( "56789", "tire", 4, 79.95M );
payableObjects[ 2 ] = new SalariedEmployee( "John", "Smith",
"111-11-1111", 800M );
payableObjects[ 3 ] = new HourlyEmployee( "Karen", "Price",
"222-22-2222", 16.75M, 40M );
payableObjects[ 4 ] = new CommissionEmployee( "Sue", "Jones",
"333-33-3333", 10000M, .06M );
payableObjects[ 5 ] = new BasePlusCommissionEmployee( "Bob",
"Lewis", "444-44-4444", 5000M, .04M, 300M );
Console.WriteLine(
"Invoices and Employees processed polymorphically:\n" );
foreach ( var currentPayable in payableObjects )
{
Console.WriteLine( "{0}", currentPayable.ToString() );
if ( currentPayable is BasePlusCommissionEmployee )
{
BasePlusCommissionEmployee employee =
( BasePlusCommissionEmployee ) currentPayable;
employee.BaseSalary *= 1.1M;
Console.WriteLine(
"new base salary with 10% increase is: {0:C}",
employee.BaseSalary );
}
Console.WriteLine( "{0}: {1:C}\n",
"payment due", currentPayable.GetPaymentAmount() );
}
}
}

浙公网安备 33010602011771号