10.9.3 Class Invoice

We now create class Invoice (Fig. 10.12) to represent a simple invoice that contains billing 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 pricePerItem obtain only nonnegative values. ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.