In: Computer Science
Invoice Class - 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).
Write a test app named InvoiceTest that demonstrates class Invoice’s capabilities. Create two Invoice objects, print their state, and invoice amounts.
Please this is what I have and I know it should work , but I keep getting these errors.
PA3.2.java:1: error: class, interface, or enum expected
Invoice class
^
PA3.2.java:1: error: <identifier> expected
Invoice class
^
PA3.2.java:69: error: <identifier> expected
InvoiceTest class wth main method
^
PA3.2.java:69: error: '{' expected
InvoiceTest class wth main method
^
PA3.2.java:90: error: reached end of file while parsing
}
^
5 errors
error: compilation failed
When I try and fix them, that's when I get this error. error: can't find main(String[]) method in class: Invoice
Please someone help me!!!!!
Invoice class
public class Invoice
{
private String partNumber;
private String description ;
private int quantity;
private double pricePerUnit;
//constructor that initializes the four instance variables
public Invoice(String partNumber, String description, int quantity,
double pricePerUnit) {
this.partNumber = partNumber;
this.description = description;
if(quantity>0)
this.quantity = quantity;
else
this.quantity = 0;
if(pricePerUnit>0)
this.pricePerUnit = pricePerUnit;
else
this.pricePerUnit = 0;
}
//getters and setters methods
public String getPartNumber() {
return partNumber;
}
public void setPartNumber(String partNumber) {
this.partNumber = partNumber;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
if(quantity>0)
this.quantity = quantity;
else
this.quantity = 0;
}
public double getPricePerUnit() {
return pricePerUnit;
}
public void setPricePerUnit(double pricePerUnit) {
if(pricePerUnit>0)
this.pricePerUnit = pricePerUnit;
else
this.pricePerUnit = 0;
}
//method that computes the total price and return it
public double getInvoiceAmount()
{
return pricePerUnit*quantity;
}
}
InvoiceTest class wth main method
public class InvoiceTest {
public static void main(String[] args) {
Invoice obj1 =new Invoice("P101","Iphone 7" , 100, 20.5);
Invoice obj2 =new Invoice("P102","Iphone 10" , 10,799 );
System.out.println("Product Number : "+obj1.getPartNumber());
System.out.println("Product Desc. : "+obj1.getDescription());
System.out.println("Quantity : "+obj1.getQuantity());
System.out.println("Price per unit :
$"+obj1.getPricePerUnit());
System.out.println("Invoice Amount :
$"+obj1.getInvoiceAmount());
System.out.println("\n\nProduct Number :
"+obj2.getPartNumber());
System.out.println("Product Desc. : "+obj2.getDescription());
System.out.println("Quantity : "+obj2.getQuantity());
System.out.println("Price per unit :
$"+obj2.getPricePerUnit());
System.out.println("Invoice Amount :
$"+obj2.getInvoiceAmount());
}
}
Seems like there is nothing wrong with the code, but issue is in how you use these two classes. From the error messages you provided, it is clear that you are using a file with name: PA3.2.java. I am guessing that you are putting the code of these two classes inside PA3.2.java and trying to run it, which is wrong.
There should be exactly two files in your project folder/package: Invoice.java containing Invoice class and InvoiceTest.java file containing InvoiceTest class. InvoiceTest contains the main method, so you should run that class. Follow these steps:
1) Copy below code and paste into a file named Invoice.java
//Invoice.java
public class Invoice {
private String partNumber;
private String description;
private int quantity;
private double pricePerUnit;
// constructor that initializes the four instance variables
public Invoice(String partNumber, String description, int quantity,
double pricePerUnit) {
this.partNumber = partNumber;
this.description = description;
if (quantity > 0)
this.quantity = quantity;
else
this.quantity = 0;
if (pricePerUnit > 0)
this.pricePerUnit = pricePerUnit;
else
this.pricePerUnit = 0;
}
// getters and setters methods
public String getPartNumber() {
return partNumber;
}
public void setPartNumber(String partNumber) {
this.partNumber = partNumber;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
if (quantity > 0)
this.quantity = quantity;
else
this.quantity = 0;
}
public double getPricePerUnit() {
return pricePerUnit;
}
public void setPricePerUnit(double pricePerUnit) {
if (pricePerUnit > 0)
this.pricePerUnit = pricePerUnit;
else
this.pricePerUnit = 0;
}
// method that computes the total price and return it
public double getInvoiceAmount() {
return pricePerUnit * quantity;
}
}
2) Copy below code and paste into a file named InvoiceTest.java
//InvoiceTest.java
public class InvoiceTest {
public static void main(String[] args) {
Invoice obj1 = new Invoice("P101", "Iphone 7", 100, 20.5);
Invoice obj2 = new Invoice("P102", "Iphone 10", 10, 799);
System.out.println("Product Number : " + obj1.getPartNumber());
System.out.println("Product Desc. : " + obj1.getDescription());
System.out.println("Quantity : " + obj1.getQuantity());
System.out.println("Price per unit : $" + obj1.getPricePerUnit());
System.out.println("Invoice Amount : $" + obj1.getInvoiceAmount());
System.out.println("\n\nProduct Number : " + obj2.getPartNumber());
System.out.println("Product Desc. : " + obj2.getDescription());
System.out.println("Quantity : " + obj2.getQuantity());
System.out.println("Price per unit : $" + obj2.getPricePerUnit());
System.out.println("Invoice Amount : $" + obj2.getInvoiceAmount());
}
}
3) Run InvoiceTest.java, you should get the below output:
Product Number : P101
Product Desc. : Iphone 7
Quantity : 100
Price per unit : $20.5
Invoice Amount : $2050.0
Product Number : P102
Product Desc. : Iphone 10
Quantity : 10
Price per unit : $799.0
Invoice Amount : $7990.0
Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks