Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Create an Invoice class that might serve as a receipt for items sold. An In

ID: 3855812 • Letter: J

Question

Java

Create an Invoice class that might serve as a receipt for items sold. An Invoice should be defined by the following attributes:

productName : String

quantityPurchased : int

pricePerItem : double

Provide getters and setters for each attribute, making sure that the numeric values given are not negative. Set them to 0 if they are. Include a method that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. Write a test program that shows everything working.

Explanation / Answer

Here is the code for Invoice.java:

class Invoice

{

String productName;

int quantityPurchased;

double pricePerItem;

public Invoice(String name, int qty, double price)

{

setProductName(name);

setQuantityPurchased(qty);

setPricePerItem(price);

}

public String getProductName() { return productName; }

public int getQuantityPurchased() { return quantityPurchased; }

public double getPricePerItem() { return pricePerItem; }

public void setProductName(String name) { productName = name; }

public void setQuantityPurchased(int qty)

{

quantityPurchased = qty;

if(qty < 0)

quantityPurchased = 0;

}

public void setPricePerItem(double price)

{

pricePerItem = price;

if(price < 0)

pricePerItem = 0;

}

public double invoiceAmount()

{

return pricePerItem * quantityPurchased;

}

}

And the code for TestInvoice.java is:

class TestInvoice

{

public static void main(String[] args)

{

Invoice myInvoice = new Invoice("Book", 5, 5.00);

System.out.printf("The total invoice amount is: %.2f ", myInvoice.invoiceAmount());

myInvoice.setQuantityPurchased(-30);

System.out.printf("The new invoice amount is: %.2f ", myInvoice.invoiceAmount());

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote