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 initialize 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., multiples 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 names InvoiceTest that demonstrates class Invoice’s capabilities.
import java.io.*;
class Invoice
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String partnum;
String partdes;
int numofitems;
double priceofitems;
double amount;
Invoice()
{
partnum="1";
partdes="hdd";
numofitems=0;
priceofitems=1000;
}
public String getpartnum()throws
IOException
{
System.out.println("Enter part number:");
partnum=br.readLine();
return partnum;
}
public String getpartdes()throws
IOException
{
System.out.println("Enter part description: ");
partdes=br.readLine();
return partdes;
}
public int getnumofitems()throws
IOException
{
System.out.println("Enter number of items:");
numofitems=Integer.parseInt(br.readLine());
return numofitems;
}
public double getpriceofitems()throws
IOException
{
System.out.println("Enter price of items:");
priceofitems=Double.parseDouble(br.readLine());
return
priceofitems;
}
public String setpartnum(String num)
{
partnum=num;
return partnum;
}
public String setpartdes(String des)
{
partdes=des;
return partdes;
}
public int setnumofitems(int numitem)
{
numofitems=numitem;
return numofitems;
}
public double setpriceofitems(double price
)
{
priceofitems=price;
return
priceofitems;
}
public double getInvoiceamount()
{
amount =
priceofitems*numofitems;
amount =
(amount>0)?amount:0;
return amount;
}
public void displayInfo()
{
System.out.printf("part number:"+partnum);
System.out.printf("\n part description:"+partdes);
System.out.printf("\n num of items:"+numofitems);
System.out.printf("\n price of items:"+priceofitems);
System.out.printf("\n Amount:"+amount);
System.out.println();
}
}
public class InvoiceTest
{
public static void main(String [] args)throws
IOException
{
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
Invoice i1 = new
Invoice();
Invoice i2 = new
Invoice();
i1.getpartnum();
i1.getpartdes();
i1.getnumofitems();
i1.getpriceofitems();
i1.getInvoiceamount();
i1.displayInfo();
i2.setpartnum("1");
i2.setpartdes("chip");
i2.setnumofitems(12);
i2.setpriceofitems(12);
i2.getInvoiceamount();
i2.displayInfo();
}
}
Get Answers For Free
Most questions answered within 1 hours.