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 initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named get Invoice Amount 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. Write a test application named InvoiceTest that demonstrates class Invoice’s capabilities.
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 initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named get Invoice Amount 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. Write a test application named InvoiceTest that demonstrates class Invoice’s capabilities.
import java.io.*;
class Invoice
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String part_num;
String part_des;
int num_of_items;
double price_of_items;
double amount;
Invoice()
{
part_num="1";
part_des="hdd";
num_of_items=0;
price_of_items=1000;
}
public String get_part_num()throws IOException
{
System.out.println("Enter part number");
part_num=br.readLine();
return part_num;
}
public String get_part_des()throws IOException
{
System.out.println("Enter part description ");
part_des=br.readLine();
return part_des;
}
public int get_num_of_items()throws IOException
{
System.out.println("Enter number of items ");
num_of_items=Integer.parseInt(br.readLine());
return num_of_items;
}
public double get_price_of_items()throws IOException
{
System.out.println("Enter price of items");
price_of_items=Double.parseDouble(br.readLine());
return price_of_items;
}
public String set_part_num(String num)
{
part_num=num;
return part_num;
}
public String set_part_des(String des)
{
part_des=des;
return part_des;
}
public int set_num_of_items(int numitem)
{
num_of_items=numitem;
return num_of_items;
}
public double set_price_of_items(double price )
{
price_of_items=price;
return price_of_items;
}
public double Invoice_amount()
{
amount = price_of_items*num_of_items;
//System.out.printf("\n Amount \n"+amount);
amount = (amount>0)?amount:0;
return amount;
}
public void displayInfo()
{
System.out.printf("part number\t"+part_num+"\tpart description\t"+part_des+"\tnum_of_items\t"+num_of_items+"\tprice_of_items\t"+price_of_items);
System.out.printf("\n Amount \t"+amount);
System.out.println();
}
}
public class InvoiceTest
{
public static void main(String [] args)throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Invoice inv1 = new Invoice();
Invoice inv2 = new Invoice();
inv1.get_part_num();
inv1.get_part_des();
inv1.get_num_of_items();
inv1.get_price_of_items();
inv1.Invoice_amount();
inv1.displayInfo();
inv2.set_part_num("1");
inv2.set_part_des("chip");
inv2.set_num_of_items(12);
inv2.set_price_of_items(12);
inv2.Invoice_amount();
inv2.displayInfo();
}
}
import java.io.*;
class Invoice
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String part_num;
String part_des;
int num_of_items;
double price_of_items;
double amount;
Invoice()
{
part_num="1";
part_des="hdd";
num_of_items=0;
price_of_items=1000;
}
public String get_part_num()throws IOException
{
System.out.println("Enter part number");
part_num=br.readLine();
return part_num;
}
public String get_part_des()throws IOException
{
System.out.println("Enter part description ");
part_des=br.readLine();
return part_des;
}
public int get_num_of_items()throws IOException
{
System.out.println("Enter number of items ");
num_of_items=Integer.parseInt(br.readLine());
return num_of_items;
}
public double get_price_of_items()throws IOException
{
System.out.println("Enter price of items");
price_of_items=Double.parseDouble(br.readLine());
return price_of_items;
}
public String set_part_num(String num)
{
part_num=num;
return part_num;
}
public String set_part_des(String des)
{
part_des=des;
return part_des;
}
public int set_num_of_items(int numitem)
{
num_of_items=numitem;
return num_of_items;
}
public double set_price_of_items(double price )
{
price_of_items=price;
return price_of_items;
}
public double Invoice_amount()
{
amount = price_of_items*num_of_items;
//System.out.printf("\n Amount \n"+amount);
amount = (amount>0)?amount:0;
return amount;
}
public void displayInfo()
{
System.out.printf("part number\t"+part_num+"\tpart description\t"+part_des+"\tnum_of_items\t"+num_of_items+"\tprice_of_items\t"+price_of_items);
System.out.printf("\n Amount \t"+amount);
System.out.println();
}
}
public class InvoiceTest
{
public static void main(String [] args)throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Invoice inv1 = new Invoice();
Invoice inv2 = new Invoice();
inv1.get_part_num();
inv1.get_part_des();
inv1.get_num_of_items();
inv1.get_price_of_items();
inv1.Invoice_amount();
inv1.displayInfo();
inv2.set_part_num("1");
inv2.set_part_des("chip");
inv2.set_num_of_items(12);
inv2.set_price_of_items(12);
inv2.Invoice_amount();
inv2.displayInfo();
}
}
Thanks a lot for the document. Its really helping
ReplyDeletepublic class Invoice{
ReplyDeleteprivate String partNumber;
private String partDescription;
private int quantity;
private double unitPrice;
// constructor
public Invoice(String partNumber, String partDescription, int quantity, double unitPrice){
setPartNumber(partNumber);
setDescription(partDescription);
setQuantity(quantity);
setUnitPrice(unitPrice);
}
// setters
public void setPartNumber(String partNumber){
this.partNumber = partNumber;
}
public void setDescription(String partDescription){
this.partDescription = partDescription;
}
public void setQuantity(int quantity){
this.quantity = (quantity < 0) ? 0 : quantity;
}
public void setUnitPrice(double unitPrice){
this.unitPrice = (unitPrice < 0.0) ? 0.0 : unitPrice;
}
// getters
public String getPartNumber(){
return partNumber;
}
public String getDescription(){
return partDescription;
}
public int getQuantity(){
return quantity;
}
public double getUnitPrice(){
return unitPrice;
}
public double getInvoiceAmount(){
return getQuantity() * getUnitPrice();
}
}
public class InvoiceTest{
public static void main(String[] args){
// testing positive values
Invoice spanner = new Invoice("123", "A Spanner", 15, 12.5);
printInvoice(spanner.getPartNumber(), spanner.getDescription(),
spanner.getQuantity(), spanner.getUnitPrice(),
spanner.getInvoiceAmount());
// testing negative values
Invoice hammer = new Invoice("124", "A Hammer", -1, -15.0);
printInvoice(hammer.getPartNumber(), hammer.getDescription(),
hammer.getQuantity(), hammer.getUnitPrice(),
hammer.getInvoiceAmount());
}
// print invoice
private static void printInvoice(String num, String desc,
int quan, double price, double total){
System.out.printf("%s: %s - %d * %.2f = %.2f\n", num, desc, quan, price, total);
}
}