Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store.

Question: 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 Integer) and a price per item (type Integer). Your class should have a constructor that initializes the four instance variables. Provide a property for each instance variable. 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; Use validation in the properties for these instance variables to ensure that they remain positive. In addition, provide a method named DisplayInvoiceAmount that calculates and displays the invoice amount (that is, multiplies the quantity by the price per item). How do you write an application that demonstrates the class Invoice’s capabilities?


import java.util.Scanner;

class Invoice{

String partNumber;

String partDescription;

int itemPurchased;

double pricePerItem;


partNumber = “”;

partDescription = “”;

itemPurchased = 0;

pricePerItem = 0.0;


String getPartNumber(){

return partNumber;


String getPartDescription(){

return partDescription;


int getItemPurchased(){

return itemPurchased;


double getPricePerItem(){

return pricePerItem;


double getInvoiceAmount(){

return (itemPurchased * pricePerItem);


void setPartNumber(String pn){

partNumber = pn;


void setPartDescription(String pd){

partDescription = pd;


void setItemPurchased(int ip){

itemPurchased = ip;


void setPricePerItem(double ppi){

pricePerItem = ppi;



class InvoiceDemo {

public static void main(String args[]) {

Scanner sc = new Scanner(;

Invoice invoice = new Invoice();

System.out.print(“\nEnter part number :”);


System.out.print(“Enter part description :”);


System.out.print(“Enter item purchased :”);


System.out.print(“Enter price per item :”);


System.out.print(“\n\nDetail of items purchasing–>”);

System.out.print(“\nPart number :” + invoice.getPartNumber());

System.out.print(“\nPart description :” + invoice.getPartDescription());

System.out.print(“\nTotal Billing Amount :” + invoice.getInvoiceAmount());


Leave a Reply

Your email address will not be published. Required fields are marked *