Erin S.

asked • 11/28/17

Java code invoice and test 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
 
 
 
import java.util.Scanner;

public class InvoiceTest
{
public static void main(String[]args)
{
Invoice purchase = new Invoice(" ", " ",0,0.00);
Scanner input = new Scanner(System.in);

String item;
String description;
int quantity;
double price;

System.out.print("Enter Number:");
item = input.nextLine();
Invoice.setPartNumber(item);


System.out.print ("Enter Description of Item:");
description = input.nextLine();
Invoice.setPartDescription(description);

System.out.print("Enter the Qauntity of Item:");
quantity = input.nextInt();
if (quantity > 0)
Invoice.setQauntityofItemPurchased(qauntity);

System.out.print("Enter the Price Per Item:");
price = input.nextDouble();
if (price > 0)
Invoice.setPricePerItem (price);

System.out.printf("The Total Amount for all items purchased = $%.2f\n",
Invoice.getInvoiceAmount());

}
}

1 Expert Answer

By:

Larry C. answered • 11/28/17

Tutor
4.9 (294)

Computer Science and Mathematics professional

Erin S.

File compiles for Invoice, I receive errors for the test file. I am not sure what I am doing wrong or how to fix it.Error says non static method in my "if statments."
 
InvoiceTest.java:17: error: non-static method setPartNumber(String) cannot be referenced from a static context
Invoice.setPartNumber(item);
^
InvoiceTest.java:22: error: non-static method setPartDescription(String) cannot be referenced from a static context
Invoice.setPartDescription(description);
^
InvoiceTest.java:27: error: cannot find symbol
Invoice.setQauntityofItemPurchased(qauntity);
^
symbol: variable qauntity
location: class InvoiceTest
InvoiceTest.java:32: error: non-static method setPricePerItem(double) cannot be referenced from a static context
Invoice.setPricePerItem (price);
^
InvoiceTest.java:35: error: cannot find symbol
Invoice.getInvoiceAmount());
^
symbol: method getInvoiceAmount()
location: class Invoice
Report

11/28/17

Larry C.

The errors on lines 17 and 22 are due to you not invoking them for an instance of class Invoice. (Your instance was named 'purchase', not 'Invoice')
The error on line 27 appears to be simply typing mistakes in your method and variable names.
The error on line 35 is referencing a method that doesn't exist.
Report

11/29/17

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.