(Invoice Class) Create a class called Invoice that a hardware store might use to
ID: 3885001 • Letter: #
Question
(Invoice Class) 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—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (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 named 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 test app named InvoiceTest that demonstrates class Invoice’s capabilities.
Explanation / Answer
Invoice.java
---------------------
public class Invoice {
private String partNumber;
private String partDescription;
private int itemQuantity = 0;
private double pricePerItem = 0;
// Default Constructor
public Invoice() {
}
// Parameterized Constructor
public Invoice(String partNumber, String partDescription, int itemQuantity,
double pricePerItem) {
super();
this.partNumber = partNumber;
this.partDescription = partDescription;
if (itemQuantity > 0)
this.itemQuantity = itemQuantity;
if (pricePerItem > 0)
this.pricePerItem = pricePerItem;
}
public String getPartNumber() {
return partNumber;
}
public void setPartNumber(String partNumber) {
this.partNumber = partNumber;
}
public String getPartDescription() {
return partDescription;
}
public void setPartDescription(String partDescription) {
this.partDescription = partDescription;
}
public int getItemQuantity() {
return itemQuantity;
}
public void setItemQuantity(int itemQuantity) {
if (itemQuantity > 0)
this.itemQuantity = itemQuantity;
}
public double getPricePerItem() {
return pricePerItem;
}
public void setPricePerItem(double pricePerItem) {
if (pricePerItem > 0)
this.pricePerItem = pricePerItem;
}
public double getInvoiceAmount() {
return this.pricePerItem * this.itemQuantity;
}
}
--------------------- End-----------------
InvoiceTest.java
-------------------
public class InvoiceTest {
public static void main(String[] args) {
// Example With setters/Getters
Invoice invoice = new Invoice();
invoice.setPartNumber("101A");
invoice.setPartDescription("Shockup-Ring");
invoice.setItemQuantity(3);
invoice.setPricePerItem(202.34);
Double invoiceAmount1 = invoice.getInvoiceAmount();
System.out.println("Calculated Invoice Amount Is " + invoiceAmount1);
// Example With constructor
Invoice invoice2 = new Invoice("102A", "Engine-Oil", 8, 103.23);
Double invoiceAmount2 = invoice2.getInvoiceAmount();
System.out.println("Calculated Invoice Amount Is " + invoiceAmount2);
}
}
------------------- End-------------------
Output
=======
Calculated Invoice Amount Is 607.02
Calculated Invoice Amount Is 825.84
Description
-------------------
1. There are two classes given in above source code. We need to place both files in same pacjage or directory.
2. We need to run the main method to ge the above output.
3. Tow examples are covered in Invoicetest class.
A. invoice is generated using setter and getter methods.
B. Invoice is generated using parameterized constructor.
Please check the code and let me know if you need more assistence on same.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.