// Fig. 10.12: Invoice.java // Invoice class implements Payable. public class Invoice implements Payable { private String partNumber; private String partDescription; private int quantity; private double pricePerItem; // four-argument constructor public Invoice( String part, String description, int count, double price ) { partNumber = part; partDescription = description; setQuantity( count ); // validate and store quantity setPricePerItem( price ); // validate and store price per item } // end four-argument Invoice constructor // set part number public void setPartNumber( String part ) { partNumber = part; } // end method setPartNumber // get part number public String getPartNumber() { return partNumber; } // end method getPartNumber // set description public void setPartDescription( String description ) { partDescription = description; } // end method setPartDescription // get description public String getPartDescription() { return partDescription; } // end method getPartDescription // set quantity public void setQuantity( int count ) { quantity = ( count < 0 ) ? 0 : count; // quantity cannot be negative } // end method setQuantity // get quantity public int getQuantity() { return quantity; } // end method getQuantity // set price per item public void setPricePerItem( double price ) { pricePerItem = ( price < 0.0 ) ? 0.0 : price; // validate price } // end method setPricePerItem // get price per item public double getPricePerItem() { return pricePerItem; } // end method getPricePerItem // return String representation of Invoice object public String toString() { return String.format( "%s: \n%s: %s (%s) \n%s: %d \n%s: $%,.2f", "invoice", "part number", getPartNumber(), getPartDescription(), "quantity", getQuantity(), "price per item", getPricePerItem() ); } // end method toString // method required to carry out contract with interface Payable public double getPaymentAmount() { return getQuantity() * getPricePerItem(); // calculate total cost } // end method getPaymentAmount } // end class Invoice /************************************************************************** * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and * * Pearson Education, Inc. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/