public class Invoice implements Payable
1: // Fig. 10.12: Invoice.java
2: // Invoice class implements Payable.
3:
4: public class Invoice implements Payable
5: {
6: private String partNumber;
7: private String partDescription;
8: private int quantity;
9: private double pricePerItem;
10:
11: // four-argument constructor
12: public Invoice( String part, String description, int count,
13: double price )
14: {
15: partNumber = part;
16: partDescription = description;
17: setQuantity( count ); // validate and store quantity
18: setPricePerItem( price ); // validate and store price per item
19: } // end four-argument Invoice constructor
20:
21: // set part number
22: public void setPartNumber( String part )
23: {
24: partNumber = part;
25: } // end method setPartNumber
26:
27: // get part number
28: public String getPartNumber()
29: {
30: return partNumber;
31: } // end method getPartNumber
32:
33: // set description
34: public void setPartDescription( String description )
35: {
36: partDescription = description;
37: } // end method setPartDescription
38:
39: // get description
40: public String getPartDescription()
41: {
42: return partDescription;
43: } // end method getPartDescription
44:
45: // set quantity
46: public void setQuantity( int count )
47: {
48: quantity = ( count < 0 ) ? 0 : count; // quantity cannot be negative
49: } // end method setQuantity
50:
51: // get quantity
52: public int getQuantity()
53: {
54: return quantity;
55: } // end method getQuantity
56:
57: // set price per item
58: public void setPricePerItem( double price )
59: {
60: pricePerItem = ( price < 0.0 ) ? 0.0 : price; // validate price
61: } // end method setPricePerItem
62:
63: // get price per item
64: public double getPricePerItem()
65: {
66: return pricePerItem;
67: } // end method getPricePerItem
68:
69: // return String representation of Invoice object
70: public String toString()
71: {
72: return String.format( "%s: \n%s: %s (%s) \n%s: %d \n%s: $%,.2f",
73: "invoice", "part number", getPartNumber(), getPartDescription(),
74: "quantity", getQuantity(), "price per item", getPricePerItem() );
75: } // end method toString
76:
77: // method required to carry out contract with interface Payable
78: public double getPaymentAmount()
79: {
80: return getQuantity() * getPricePerItem(); // calculate total cost
81: } // end method getPaymentAmount
82: } // end class Invoice
83:
84:
85: /**************************************************************************
86: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
87: * Pearson Education, Inc. All Rights Reserved. *
88: * *
89: * DISCLAIMER: The authors and publisher of this book have used their *
90: * best efforts in preparing the book. These efforts include the *
91: * development, research, and testing of the theories and programs *
92: * to determine their effectiveness. The authors and publisher make *
93: * no warranty of any kind, expressed or implied, with regard to these *
94: * programs or to the documentation contained in these books. The authors *
95: * and publisher shall not be liable in any event for incidental or *
96: * consequential damages in connection with, or arising out of, the *
97: * furnishing, performance, or use of these programs. *
98: *************************************************************************/