Java Reference
In-Depth Information
}
public double computeCommission()
{
double commission = 0.0;
if(sales > 0.0)
{
commission = sales * commissionRate;
}
sales = 0.0; //Start over
return commission;
}
}
Here are a few points I want to make about the SalesPerson class:
All of the fields are private, which is commonly done even if the accessor
and mutator methods of a field do not do anything special. For example,
setName() and getName() simply change and return the name field,
respectively.
■■
The id field only has a corresponding accessor method, but no mutator
method. In other words, you can get the contents of the id field, but it
cannot be changed. This makes id a read-only field. A field can also be
made write-only by including only a set method.
■■
The only way to change commissionRate is to invoke setCommission-
Rate(), and this method only changes the field when the value passed
in is between 0 and 20 percent.
■■
The constructor invoked the set methods for the name and commission-
Rate fields, even though the constructor can access these fields directly.
The advantage of a constructor using the accessor methods is that the
logic for assigning a value to a field is centralized in one location and
does not need to be repeated. For example, if the constructor did not
invoke setCommissionRate(), the constructor would have needed to
check the rate passed in to ensure that it was in the proper range of
0 to 20 percent. But setCommissionRate() already contains this logic.
■■
The sales field has neither a set nor a get method; however, the field is
still an important part of the class because it keeps track of the amount
of sales for the salesperson.
■■
Search WWH ::




Custom Search