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

Create a Java class called Invoice for a store to invoice for an item sold at th

ID: 3671889 • Letter: C

Question

Create a Java class called Invoice for a store to invoice for an item sold at the store.

An invoice should include the following four private instance variables:

partNum: part number (type int)

partDesc: part description (type String)

quantity: quantity of the item being purchased (type int)

price: price per item (type double).

The class should include the following methods:

a constructor that initializes the four instance variables. If the quantity or the price is not positive it should be set to 0.

getter and setter methods for each instance variable.

getInvoiceAmount: a method that calculates and returns the total invoice amount.

toString: a method that gives a one-line description of the invoice.  

Write a test driver, named InvoiceDriver, to demonstrate Invoice capabilities

Explanation / Answer

//Invoice.java
public class Invoice
{
private String partNumber ;
private String partDescription ;
private int quantity ;
private double price ;
public Invoice ( String partNo , String partDesc , int quaty , double rate )
{
if( partNo != null )
partNumber = partNo ;
else
partNumber = "0" ;
if( partDesc != null )
partDescription = partDesc ;
else
partDescription = "0" ;
if( quaty > 0 )
quantity = quaty ;
else
quantity = 0 ;
if( rate > 0.0 )
price = rate ;
else
price = 0 ;
}

public String getPart_Num ( )
{
return partNumber ;
}

public String getPart_Desc ( )
{
return partDescription ;
}

public int get_Quantity ( )
{
return quantity ;
}

public double getRate ( )
{
return price;
}

public void setPart_Number ( String partNo )
{
if( partNo != null )
{
partNumber = partNo ;
}
else
{
partNumber = "0" ;
}
}

public void setPart_Desc ( String partDesc )
{
if( partDesc != null )
{
partDescription = partDesc ;
}
else
{
partDescription = "0" ;
}
}

public void set_Quantity ( int quaty )
{
if( quaty > 0 )
{
quantity = quaty ;
}
else
{
quantity = 0 ;
}
}

public void set_Rate ( double rate )
{
if( rate > 0.0 )
{
price = rate ;
}
else
{
price = 0.0 ;
}
}

public double getInvoiceBill_Amount ( )
{
return ( double ) quantity * price ;
}
}


InvoiceDriver.java

public class InvoiceDriver
{
public static void main( String[] argements )
{
Invoice bill1 = new Invoice( "B6655" , "Telugu Titans" , 611 , 361.11 ) ;
Invoice bill2 = new Invoice( "B6656" , "U Mumbai" , 411 , 61.11 ) ;

System.out.printf ( " Invoice_Bill 1 : %s %s %d $%.2f " ,
   bill1.getPart_Num ( ) ,
   bill1.getPart_Desc ( ) ,
   bill1.get_Quantity ( ) ,
   bill1.getRate ( )
   ) ;
System.out.printf ( " Invoice_Bill 2 : %s %s %d $%.2f " ,
   bill2.getPart_Num ( ) ,
   bill2.getPart_Desc ( ) ,
   bill2.get_Quantity ( ) ,
   bill2.getRate ( )
   ) ;
}
}