Create a class called Invoice that a hardware store might use to represent an in
ID: 3542305 • 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 (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 tst application named InvoiceTest that demonstrates class Invoice's capabilities.
Explanation / Answer
class Invoice
{
private String part_no;
private String part_desc;
private int item;
private double price;
public Invoice()
{
part_no = "";
part_desc = "";
item = 0;
price = 0.0;
}
public Invoice(String p_n,String p_d,int i,double p)
{
part_no = p_n;
part_desc = p_d;
item = i;
price = p;
}
public void set_part_no(String p_n)
{
part_no = p_n;
}
public void set_part_desc(String p_d)
{
part_desc = p_d;
}
public void set_item(int i)
{
item = i;
}
public void set_pprice(double p)
{
price = p;
}
public String get_part_no()
{
return part_no;
}
public String set_part_desc()
{
return part_desc;
}
public int set_item()
{
return item;
}
public double set_pprice()
{
return price;
}
public double getInvoiceAmount()
{
if(item>0 && price>0.0)
return price*item;
return 0.0;
}
}
public class InvoiceTest
{
public static void main(String[] args)
{
Invoice inv1 = new Invoice("123","Tyre",12,45);
System.out.println("Invoice amount given by " + inv1.getInvoiceAmount());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.