Java Reference
In-Depth Information
28
// get part number
29
public
String getPartNumber()
30
{
31
return
partNumber;
// should validate
32
}
33
34
// get description
35
public
String getPartDescription()
36
{
37
return
partDescription;
38
}
39
40
// set quantity
41
public
void
setQuantity(
int
quantity)
42
{
43
if
(quantity <
0
)
// validate quantity
44
throw new
IllegalArgumentException(
"Quantity must be >= 0")
;
45
46
this
.quantity = quantity;
47
}
48
49
// get quantity
50
public
int
getQuantity()
51
{
52
return
quantity;
53
}
54
55
// set price per item
56
public
void
setPricePerItem(
double
pricePerItem)
57
{
58
if
(pricePerItem <
0.0
)
// validate pricePerItem
59
throw new
IllegalArgumentException(
60
"Price per item must be >= 0"
);
61
62
this
.pricePerItem = pricePerItem;
63
}
64
65
// get price per item
66
public
double
getPricePerItem()
67
{
68
return
pricePerItem;
69
}
70
71
// return String representation of Invoice object
72
@Override
73
public
String toString()
74
{
75
return
String.format(
"%s: %n%s: %s (%s) %n%s: %d %n%s: $%,.2f"
,
76
"invoice"
,
"part number"
, getPartNumber(), getPartDescription(),
77
"quantity"
, getQuantity(),
"price per item
", getPricePerItem());
78
}
79
Fig. 10.12
|
Invoice
class that implements
Payable
. (Part 2 of 3.)