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

Create a class called Invoice that a hardware store might use to represent an in

ID: 3929281 • 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-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 getlnvoiceAmount 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

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg;

public class Invoice {
  
private String partNumber,partDescription;
private int quantity;
private double price;
  
public Invoice(String partNumber,String partDescription,int quantity,double price)
{
this.partDescription = partDescription;
this.partNumber = partNumber;
this.quantity = quantity>=0?quantity:0;
this.price = price>=0?price:0;
}
  
public void setDescription(String partDescription)
{
this.partDescription = partDescription;
}
  
public void setNumber(String partNumber)
{
this.partNumber = partNumber;
}
  
public void setQuantity(int quantity)
{
this.quantity = quantity;
}
  
public void setPrice(double price)
{
this.price = price;
}
  
public String getDescription()
{
return this.partDescription;
}
  
public String getNumber()
{
return this.partNumber;
}
  
public int getQuantity()
{
return this.quantity;
}
  
public double getPrice()
{
return this.price;
}
  
public double getInvoiceAmount()
{
return this.price*this.quantity;
}
  
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("Part Number : "+this.partNumber + " ");
sb.append("Part Description : "+this.partDescription + " ");
sb.append("Quantity : "+this.quantity + " ");
sb.append("Price per item : "+this.price + " ");
return sb.toString();
}
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg;

public class TestInvoice {
  
public static void main(String[] args)
{
Invoice inv = new Invoice("12345", "MRF Tyre", 2, 3999.99);
System.out.println("Invoice Details : "+inv);
System.out.println("Invoice amount is : "+inv.getInvoiceAmount());
}
}

OUTPUT:

run:
Invoice Details :
Part Number : 12345
Part Description : MRF Tyre
Quantity : 2
Price per item : 3999.99

Invoice amount is : 7999.98
BUILD SUCCESSFUL (total time: 0 seconds)

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