Program 1 - Invoice class Create a class called Invoice that a hardware store mi
ID: 3655768 • Letter: P
Question
Program 1 - 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 (fields) - a part number (type String), a part description (type String), a quantity of the item being purchased (type int), and a price for the item (type double). Your class should have a constructor that initializes the four instance variables. Provide set and a get methods 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. Write a test application named InvoiceTest to demonstrate the use of ALL the methods in the class.Explanation / Answer
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(" Enter 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(" Detail of items purchasing-->"); System.out.print(" Part number :" + invoice.getPartNumber()); System.out.print(" Part description :" + invoice.getPartDescription()); System.out.print(" Total Billing Amount :" + invoice.getInvoiceAmount()); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.