Create a class called Invoice that a hardware store might use to represent an in
ID: 3544604 • Letter: C
Question
Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables (fields) - a part number (type String), a part description (type String), a quantity of the item being purchased (type int), and a price per item (type double).
Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method name getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.0. Write a tst application named InvoiceTest that demonstrates class Invoice's capabilities.
Explanation / Answer
Invoice.java
2 // Invoice class.
34
public class Invoice
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
18 if ( count > 0 ) // determine whether count is positive
19 quantity = count; // valid count assigned to quantity
20
21 if ( price > 0.0 ) // determine whether price is positive
pricePerItem = price; // valid price assigned to pricePerItem
23 } // end four-argument Invoice constructor
24
25 // set part number
26 public void setPartNumber( String part )
27 {
28 partNumber = part;
29 } // end method setPartNumber
30
31 // get part number
32 public String getPartNumber()
33 {
34 return partNumber;
35 } // end method getPartNumber
36
37 // set description
38 public void setPartDescription( String description )
39 {
40 partDescription = description;
41 } // end method setPartDescription
42
43 // get description
44 public String getPartDescription()
45 {
46 return partDescription;
47 } // end method getPartDescription
48
49 // set quantity
50 public void setQuantity( int count )
51 {
52 if ( count > 0 ) // determine whether count is positive
53 quantity = count; // valid count assigned to quantity
54
55 if ( count < = 0 ) // determine whether count is zero or negative
56 quantity = 0; // invalid count; quantity set to 0
57 } // end method setQuantity
58
59 // get quantity
60 public int getQuantity()
61 {
62 return quantity;
63 } // end method getQuantity
64
65 // set price per item
66 public void setPricePerItem( double price )
67 {
68 if ( price > 0.0 ) // determine whether price is positive
69 pricePerItem = price; // valid price assigned to pricePerItem
70
71 if ( price < = 0.0 ) // determine whether price is zero or negative
72 pricePerItem = 0.0; // invalid price; pricePerItem set to 0.0
73 } // end method setPricePerItem
74
75 // get price per item
76 public double getPricePerItem()
{
78 return pricePerItem;
79 } // end method getPricePerItem
80
81 // calculates and returns the invoice amount
82 public double getInvoiceAmount()
83 {
84 return getQuantity() * getPricePerItem(); // calculate total cost
85 } // end method getPaymentAmount
86 } // end class Invoice
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.