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

(Invoice Class) Create a class called Invoice that a hardware store might use to

ID: 3796513 • Letter: #

Question

(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 each instance variable. In addition, 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. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.0. Write a test app named InvoiceTest that demonstrates class Invoice’s capabilities.

Submit the 2 .java files and the .class files. Run your tester class twice with two different sets of data. Copy the output and place it at the bottom of the tester class as comments OR copy the output to a Word document and submit with the two classes.

Explanation / Answer

Invoice.java
public class Invoice {
   //fields of class invoice
   String part_number,part_description;
   int quantity;
   double price;
   public Invoice()//default constructor
   {
       part_number="";
       part_description="";
       quantity=0;
       price=0;
   }
   //parameterized constructor
   public Invoice(String part_number, String part_description, int quantity, double price) {
      
       this.part_number = part_number;
       this.part_description = part_description;
       if(quantity<0)
           this.quantity = 0;
       else
       {
           this.quantity = quantity;
       }
       if(price<0)
       this.price = 0;
       else
           this.price = price;

   }
   //getter methods
   public String getPart_number() {
       return part_number;
   }
   public String getPart_description() {
       return part_description;
   }
   public int getQuantity() {
       return quantity;
   }
   public double getPrice() {
       return price;
   }
   //setters method
   public void setPart_number(String part_number) {
       this.part_number = part_number;
   }
  
   public void setPart_description(String part_description) {
       this.part_description = part_description;
   }
  
   public void setQuantity(int quantity) {
       this.quantity = quantity;
   }
  
   public void setPrice(double price) {
       this.price = price;
   }
   //get invoice amount
   public double getInvoiceAmount()
   {
       return quantity*price;
   }

}

===============================================

InvoiceTest.java


public class InvoiceTest {

   public static void main(String[] args) {
       // TODO Auto-generated method stub

       //create object of invoice class
       Invoice v=new Invoice("123", "Metal part", 3, 100);
       System.out.println("Part number is "+v.getPart_number());
       System.out.println("Part description is "+v.getPart_description());
       System.out.println("Part quantity is "+v.getQuantity());
       System.out.println("Part Price is "+v.getPrice());
       System.out.println("Invoice amount is "+v.getInvoiceAmount());

       Invoice v1=new Invoice();
       //demonstrate setter methods
       v1.setPart_description("Plastic part");
       v1.setPart_number("456");
       v1.setPrice(500);
       v1.setQuantity(2);
       System.out.println("=======================================================");
       System.out.println("Part number is "+v1.getPart_number());
       System.out.println("Part description is "+v1.getPart_description());
       System.out.println("Part quantity is "+v1.getQuantity());
       System.out.println("Part Price is "+v1.getPrice());
       System.out.println("Invoice amount is "+v1.getInvoiceAmount());

      
   }

}

=====================================================

Output:

Part number is 123
Part description is Metal part
Part quantity is 3
Part Price is 100.0
Invoice amount is 300.0
=======================================================
Part number is 456
Part description is Plastic part
Part quantity is 2
Part Price is 500.0
Invoice amount is 1000.0

=======================================================

Here i have included both test cases in same code.You can separate them or leave as it is.

If you have any questions ask me.