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?

Solution:

import java.util.Scanner;

class Invoice{

String partNumber;

String partDescription;

int itemPurchased;

double pricePerItem;

Invoice(){

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(System.in);

Invoice invoice = new Invoice();

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

invoice.setPartNumber(sc.nextLine());

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

invoice.setPartDescription(sc.nextLine());

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

invoice.setItemPurchased(sc.nextInt());

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

invoice.setPricePerItem(sc.nextDouble());

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 *