Java Reference
In-Depth Information
A line item needs to store a Product object and the product quantity. That leads
to the following instance fields:
public class LineItem
{
. . .
private int quantity;
private Product theProduct;
}
The methods themselves are now very easy. Here is a typical example. You already
know what the getTotalPrice method of the LineItem class needs to doȌ
get the unit price of the product and multiply it with the quantity.
551
552
/**
Computes the total cost of this line item.
@return the total price
*/
public double getTotalPrice()
{
return theProduct.getPrice() * quantity;
}
We will not discuss the other methods in detailȌthey are equally straightforward.
Finally, you need to supply constructors, another routine task.
Here is the entire program. It is a good practice to go through it in detail and match
up the classes and methods against the CRC cards and UML diagram.
ch12/invoice/InvoicePrinter.java
1 /**
2 This program demonstrates the invoice classes by
3 printing a sample invoice.
4 */
5 public class InvoicePrinter
6 {
7 public static void main(String[] args)
8 {
9 Address samsAddress
Search WWH ::




Custom Search