Question

In: Computer Science

Invoice Class - Create a class called Invoice that a hardware store might use to represent...

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).

  • Your class should have a constructor that initializes the four instance variables. If the quantity passed to the constructor is not positive, it should be set to 0 by the constructor. If the price per item passed to the constructor is not positive, it should be set to 0.0 by the constructor.

  • Provide a set and a get method for each instance variable. If the quantity passed to the setQuantity method is not positive, it should be set to 0 by the method. If the price per item passed to the setPricePerItem method is not positive, it should be set to 0.0 by the method.

  • Provide a method named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value.

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());
}
  
}

Solutions

Expert Solution

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


Related Solutions

3.12 (Invoice Class) Create a class called Invoice that a hardware store might use to represent...
3.12 (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). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for...
with PHP Create a class called Invoice that a hardware store might use to represent an...
with PHP 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...
----------------------------------------------------------------------------------------------- Create a class called MathOperations that a teacher might use to represent the basic type...
----------------------------------------------------------------------------------------------- Create a class called MathOperations that a teacher might use to represent the basic type of mathematical operations that may be performed. The class should include the following: Three double private variables as instance variables, number1, number2, and result. Your class should have a default constructor that initializes the three instance variables to zero. Your class should also have a constructor that initializes the two instance variables (number1 and number2) to the value entered by the user from keyboard....
With PHP Create a class called Account that a bank might use to represent customers' bank...
With PHP Create a class called Account that a bank might use to represent customers' bank accounts. Your class should include one data member of type int to represent the account balance. Your class should provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it is greater than or equal to 0. If not, the balance should be set to 0 and the...
Please solve using jupyter notebook . 10.10- (Invoice Class) Create a class called Invoice that a...
Please solve using jupyter notebook . 10.10- (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 data attributes—a part number (a string), a part description (a string), a quantity of the item being purchased (an int) and a price per item (a Decimal). Your class should have an __init__ method that initializes the four data attributes....
Create a class named UsedFurnitureItem to represent a used furniture item that the store sells. Private...
Create a class named UsedFurnitureItem to represent a used furniture item that the store sells. Private data members of a UsedFurnitureItem are: age (double) // age in years – default value for brandNewPrice (double) // the original price of the item when it was brand new description (string) // a string description of the item condition (char) // condition of the item could be A, B, or C. size (double) // the size of the item in cubic inches. weight...
Create a class called RandomGuess. In this game, generate and store a random number between 1...
Create a class called RandomGuess. In this game, generate and store a random number between 1 and 100. Then, continuously (in a loop): Ask the user to enter a number between 1 and 100 Let the user know if the guess is high or low, until the user enters the correct value If the user enters the correct value, then display a count of the number of attempts it took and exit
Task Create a class called Mixed. Objects of type Mixed will store and manage rational numbers...
Task Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). The class, along with the required operator overloads, should be written in the files mixed.h and mixed.cpp. Details and Requirements Finish Lab 2 by creating a Mixed class definition with constructor functions and some methods. The Mixed class should have public member functions Evaluate(), ToFraction(), and Simplify(). The Evaluate() function should return a...
Task CPP Create a class called Mixed. Objects of type Mixed will store and manage rational...
Task CPP Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). The class, along with the required operator overloads, should be written in the files mixed.h and mixed.cpp. Details and Requirements Finish Lab 2 by creating a Mixed class definition with constructor functions and some methods. The Mixed class should have public member functions Evaluate(), ToFraction(), and Simplify(). The Evaluate() function should return...
Create a Class to contain a customer order Create attributes of that class to store Company...
Create a Class to contain a customer order Create attributes of that class to store Company Name, Address and Sales Tax. Create a public property for each of these attributes. Create a class constructor without parameters that initializes the attributes to default values. Create a class constructor with parameters that initializes the attributes to the passed in parameter values. Create a behavior of that class to generate a welcome message that includes the company name. Create a Class to contain...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT