By using Netbeat.Create a class called Invoice that a hardware store might use t
ID: 3752054 • Letter: B
Question
By using Netbeat.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 per item (type 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 name 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 application named InvoiceTest that demonstrates class Invoice's capabilities.
Explanation / Answer
package invoice;
public class Invoice {
private String part_number,part_description;
private int quantity;
private double price;
//Constructor
public Invoice() {
// TODO Auto-generated constructor stub
part_number="XXX";
part_description="XXX";
quantity=0;
price=0.0;
}
//Get and Set methods
public void setPart_number(String pNumber) {
part_number=pNumber;
}
public String getPart_number() {
return part_number;
}
public void setPart_description(String pDecription) {
part_description=pDecription;
}
public String getPart_description() {
return part_description;
}
public void setQuantity(int pQuant) {
if(pQuant<0)
quantity=0;
else
quantity=pQuant;
}
public int getQuantity() {
return quantity;
}
public void setPrice(double pPrice) {
if(pPrice<0.0)
price=0.0;
else
price=pPrice;
}
public double getPrice() {
return price;
}
//Get Invoice Amount method
public double getInvoiceAmount() {
return quantity*price;
}
}
package invoice;
//Invoice Test Application
public class InvoiceTest {
public static void main(String[] args) {
Invoice in=new Invoice();
in.setPart_description("ABC");
in.setPart_number("123");
in.setQuantity(4);
in.setPrice(499.99);
System.out.println("Part Decription= "+in.getPart_description());
System.out.println("Part Number= "+in.getPart_number());
System.out.println("Part Quantity= "+in.getQuantity());
System.out.println("Part Price= "+in.getPrice());
System.out.println("Invoice Amount= "+in.getInvoiceAmount());
// If Quantity and Price is negative
in.setPart_description("ABC");
in.setPart_number("123");
in.setQuantity(-1);
in.setPrice(-1);
System.out.println("Part Decription= "+in.getPart_description());
System.out.println("Part Number= "+in.getPart_number());
System.out.println("Part Quantity= "+in.getQuantity());
System.out.println("Part Price= "+in.getPrice());
System.out.println("Invoice Amount= "+in.getInvoiceAmount());
}
}
Output for Above Program
Part Decription= ABC
Part Number= 123
Part Quantity= 4
Part Price= 499.99
Invoice Amount= 1999.96
Part Decription= ABC
Part Number= 123
Part Quantity= 0
Part Price= 0.0
Invoice Amount= 0.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.