Create a class called Invoice that a hardware store might us
Solution
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg;
public class Invoice {
private String partNumber,partDescription;
private int quantity;
private double price;
public Invoice(String partNumber,String partDescription,int quantity,double price)
{
this.partDescription = partDescription;
this.partNumber = partNumber;
this.quantity = quantity>=0?quantity:0;
this.price = price>=0?price:0;
}
public void setDescription(String partDescription)
{
this.partDescription = partDescription;
}
public void setNumber(String partNumber)
{
this.partNumber = partNumber;
}
public void setQuantity(int quantity)
{
this.quantity = quantity;
}
public void setPrice(double price)
{
this.price = price;
}
public String getDescription()
{
return this.partDescription;
}
public String getNumber()
{
return this.partNumber;
}
public int getQuantity()
{
return this.quantity;
}
public double getPrice()
{
return this.price;
}
public double getInvoiceAmount()
{
return this.price*this.quantity;
}
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append(\"Part Number : \"+this.partNumber + \"\ \");
sb.append(\"Part Description : \"+this.partDescription + \"\ \");
sb.append(\"Quantity : \"+this.quantity + \"\ \");
sb.append(\"Price per item : \"+this.price + \"\ \");
return sb.toString();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg;
public class TestInvoice {
public static void main(String[] args)
{
Invoice inv = new Invoice(\"12345\", \"MRF Tyre\", 2, 3999.99);
System.out.println(\"Invoice Details : \ \"+inv);
System.out.println(\"Invoice amount is : \"+inv.getInvoiceAmount());
}
}
OUTPUT:
run:
Invoice Details :
Part Number : 12345
Part Description : MRF Tyre
Quantity : 2
Price per item : 3999.99
Invoice amount is : 7999.98
BUILD SUCCESSFUL (total time: 0 seconds)

