Erin S.

asked • 11/27/17

Write java for Invoice and TestInvoice

I have the first file for invoice no problem, I am unsure how to write TestInvoice
 
Invoice
 
import java.util.Scanner;

public class Invoice
{
private String partNumber;
private String partDescription;
private int quantity;
private double pricePerItem;

// four-argument constructor
public Invoice( String part, String description, int count,
double price )
{
partNumber = part;
partDescription = description;
setQuantity( count ); // validate and store quantity
setPricePerItem( price ); // validate and store price per item
} // end four-argument Invoice constructor

// set part number
public void setPartNumber( String part )
{
partNumber = part;
} // end method setPartNumber

// get part number
public String getPartNumber()
{
return partNumber;
} // end method getPartNumber

// set description
public void setPartDescription( String description )
{
partDescription = description;
} // end method setPartDescription

// get description
public String getPartDescription()
{
return partDescription;
} // end method getPartDescription

// set quantity
public void setQuantity( int count )
{
quantity = ( count < 0 ) ? 0 : count; // quantity cannot be negative
} // end method setQuantity

// get quantity
public int getQuantity()
{
return quantity;
} // end method getQuantity

// set price per item
public void setPricePerItem( double price )
{
pricePerItem = ( price < 0.0 ) ? 0.0 : price; // validate price
} // end method setPricePerItem

// get price per item
public double getPricePerItem()
{
return pricePerItem;
} // end method getPricePerItem

// return String representation of Invoice object
public String toString()
{
return String.format( "%s: \n%s: %s (%s) \n%s: %d \n%s: $%,.2f",
"invoice", "part number", getPartNumber(), getPartDescription(),
"quantity", getQuantity(), "price per item", getPricePerItem() );
} // end method toString

// method required to carry out contract with interface Payable
public double getPaymentAmount()
{
return getQuantity() * getPricePerItem(); // calculate total cost
} // end method getPaymentAmount
} // end class Invoice
 
 
TestInvoice
 
public class TestInvoice
{
public static void main(String[]args)
{
Invoice aToy = new Invoice("part number is 21945", "part is black with white dots" ,


}
}

1 Expert Answer

By:

Abubakar Q. answered • 07/14/25

Tutor
New to Wyzant

Computer Science Made Easy – AP CS, Java, Python & C++

Abubakar Q.

You will have to add multiple test cases for each method within your class file. The above is a good starting point.
Report

07/14/25

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.