Create a class called Invoice that a hardware store might use to represent an in
ID: 3881178 • Letter: C
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 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.
Explanation / Answer
InvoiceTest.java
import java.util.Scanner;
public class InvoiceTest{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
Invoice invoice = new Invoice();
System.out.println(" Enter part number:");
invoice.setPartNumber(sc.nextLine());
System.out.println("Enter part description:");
invoice.setPartDescription(sc.nextLine());
System.out.println("Enter item purchased:");
invoice.setItemPurchased(sc.nextInt());
System.out.println("Enter price per item:");
invoice.setPricePerItem(sc.nextDouble());
System.out.println(" Details of items purchasing-->");
System.out.println(" Part Number:"+invoice.getPartNumber());
System.out.println(" Part Description:"+invoice.getPartDescription());
System.out.println(" Total Billing Amount:"+invoice.getInvoiceAmount());
}
}
Invoice.java
public class Invoice{
String partNumber;
String partDescription;
int itemPurchased;
double pricePerItem;
public Invoice(){
partNumber = "()";
partDescription = "()";
itemPurchased = 0;
pricePerItem = 0.0;
}
double getInvoiceAmount(){
return(itemPurchased*pricePerItem);
}
void setPartNumber(String pn){
partNumber = pn;
}
void setPartDescription(String pd){
partDescription = pd;
}
void setPricePerItem(double ppi){
pricePerItem = ppi;
}
public void setItemPurchased(int itemPurchased) {
this.itemPurchased = itemPurchased;
}
String getPartNumber(){
return partNumber;
}
String getPartDescription(){
return partDescription;
}
int getItemPurchased(){
return itemPurchased;
}
double getPricePerItem(){
return pricePerItem;
}
}
Output:
Enter part number:
1111
Enter part description:
aaaaa
Enter item purchased:
5
Enter price per item:
100
Details of items purchasing-->
Part Number:1111
Part Description:aaaaa
Total Billing Amount:500.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.