(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.
java invoice application source code
Invoice.java
/*
* Filename: Invoice.java
*
* Description: Exercise 3.13 - Invoice Class
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
*
* =====================================================================================
*/
public class Invoice{
private String partNumber;
private String partDescription;
private int quantity;
private double unitPrice;
// constructor
public Invoice(String partNumber, String partDescription, int quantity, double unitPrice){
setPartNumber(partNumber);
setDescription(partDescription);
setQuantity(quantity);
setUnitPrice(unitPrice);
}
// setters
public void setPartNumber(String partNumber){
this.partNumber = partNumber;
}
public void setDescription(String partDescription){
this.partDescription = partDescription;
}
public void setQuantity(int quantity){
this.quantity = (quantity < 0) ? 0 : quantity;
}
public void setUnitPrice(double unitPrice){
this.unitPrice = (unitPrice < 0.0) ? 0.0 : unitPrice;
}
// getters
public String getPartNumber(){
return partNumber;
}
public String getDescription(){
return partDescription;
}
public int getQuantity(){
return quantity;
}
public double getUnitPrice(){
return unitPrice;
}
public double getInvoiceAmount(){
return getQuantity() * getUnitPrice();
}
}
InvoiceTest.java
/*
* Filename: InvoiceTest.java
*
* Description: Exercise 3.13 - Invoice Class
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
*
* =====================================================================================
*/
public class InvoiceTest{
public static void main(String[] args){
// testing positive values
Invoice spanner = new Invoice("123", "A Spanner", 15, 12.5);
printInvoice(spanner.getPartNumber(), spanner.getDescription(),
spanner.getQuantity(), spanner.getUnitPrice(),
spanner.getInvoiceAmount());
// testing negative values
Invoice hammer = new Invoice("124", "A Hammer", -1, -15.0);
printInvoice(hammer.getPartNumber(), hammer.getDescription(),
hammer.getQuantity(), hammer.getUnitPrice(),
hammer.getInvoiceAmount());
}
// print invoice
private static void printInvoice(String num, String desc,
int quan, double price, double total){
System.out.printf("%s: %s - %d * %.2f = %.2f\n", num, desc, quan, price, total);
}
}