Create a class called Invoice that a hardware store might use to represent an in
ID: 3885124 • 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 data members-a part number (type string), a part description (type string), a quantity of the item being purchased (type int) and a price per item (type int). Your class should have a constructor that initializes the four data members. Provide a set and a get function for each data member. In addition, provide a member function named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as an int 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. Write a test program that demonstrates class Invoice's capabilities.Explanation / Answer
/*
* This Program Creates An Invoice For An Item Sold At The Store
*/
public class Invoice {
// Declare instance variables
private String partNumber;
private String partDescription;
private int quantityOfItemPurchased;
private double pricePerItem;
// Constructor for variables’ initialization
public Invoice (String number, String description, int quantity, double price) {
partNumber = number;
partDescription = description;
quantityOfItemPurchased = quantity;
pricePerItem = price;
}
// Set method
public void setPartNumber (String number) {
partNumber = number;
}
public void setPartDescription (String description) {
partDescription = description;
}
// Get method
public String getPartNumber () {
return partNumber;
}
public String getPartDescription () {
return partDescription;
}
public void setQuantityOfItemPurchased (int quantity) {
quantityOfItemPurchased = quantity;
}
public int getQuantityOfItemPurchased () {
return quantityOfItemPurchased;
}
public void setPricePerItem (double price) {
pricePerItem = price;
}
public double getPricePerItem () {
return pricePerItem;
}
// getInvoiceAmount method
public double getInvoiceAmount () {
double calculateTotalAmount;
calculateTotalAmount = quantityOfItemPurchased * pricePerItem;
return calculateTotalAmount;
}
}
/*
* This Program Demonstrates Class Invoice Capabilities
*/
import java.util.Scanner;
public class InvoiceTest {
public static void main (String [] args) {
// Create Object(s)
Invoice object1 = new Invoice(“27”, “Blueberry parfait”, 3, 9.9);
Invoice object2 = new Invoice(“12”, “Rainbow Fraffucino”, 5, 6.6);
Invoice object3 = new Invoice(“94”, “Strawberry shortcake”, 7, 29.8);
// Print the Invoice
System.out.println(“This is an Invoice for the Item(s) Sold:” + “ ”);
System.out.println(object1.getPartNumber() + “ ” + object1.getPartDescription() + “ ” + object1.getQuantityOfItemPurchased() + “ ” + object1.getPricePerItem() + “ ” + object1.getInvoiceAmount());
System.out.println(object2.getPartNumber() + “ ” + object2.getPartDescription() + “ ” + object2.getQuantityOfItemPurchased() + “ ” + object2.getPricePerItem() + “ ” + object2.getInvoiceAmount());
System.out.println(object3.getPartNumber() + “ ” + object3.getPartDescription() + “ ” + object3.getQuantityOfItemPurchased() + “ ” + object3.getPricePerItem() + “ ” + object3.getInvoiceAmount());
System.out.println(“ ”);
double total = object1.getInvoiceAmount() + object2.getInvoiceAmount() + object3.getInvoiceAmount();
System.out.println(“TOTAL: “+ total + “ ”);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.