Java Reference
In-Depth Information
1
// Fig. 10.11: Payable.java
2
// Payable interface declaration.
3
4
5
6
7
public interface Payable
{
double getPaymentAmount(); // calculate payment; no implementation
}
Fig. 10.11 | Payable interface declaration.
10.9.3 Class Invoice
We now create class Invoice (Fig. 10.12) to represent a simple invoice that contains bill-
ing information for only one kind of part. The class declares private instance variables
partNumber , partDescription , quantity and pricePerItem (in lines 6-9) that indicate
the part number, a description of the part, the quantity of the part ordered and the price
per item. Class Invoice also contains a constructor (lines 12-26), get and set methods
(lines 29-69) that manipulate the class's instance variables and a toString method (lines
72-78) that returns a String representation of an Invoice object. Methods setQuantity
(lines 41-47) and setPricePerItem (lines 56-63) ensure that quantity and pricePer-
Item obtain only nonnegative values.
1
// Fig. 10.12: Invoice.java
2
// Invoice class that implements Payable.
3
4
5
public class Invoice implements Payable
{
6
private final String partNumber;
7
private final String partDescription;
8
private int quantity;
9
private double pricePerItem;
10
11
// constructor
12
public Invoice(String partNumber, String partDescription, int quantity,
13
double pricePerItem)
14
{
15
if (quantity < 0 ) // validate quantity
16
throw new IllegalArgumentException( "Quantity must be >= 0") ;
17
18
if (pricePerItem < 0.0 ) // validate pricePerItem
19
throw new IllegalArgumentException(
20
"Price per item must be >= 0" );
21
22
this .quantity = quantity;
23
this .partNumber = partNumber;
24
this .partDescription = partDescription;
25
this .pricePerItem = pricePerItem;
26
} // end constructor
27
Fig. 10.12 | Invoice class that implements Payable . (Part 1 of 3.)
 
 
Search WWH ::




Custom Search