Create a class called with your Reg No, as Invoice18Arid876, 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 getInvoice 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.0. Write a test application named InvoiceTestYourName that demonstrates class Invoice’s capabilities.
public class Invoice18Arid876 {
private String partNumber;
private String partDescription;
private int quantity;
private double pricePerItem;
public Invoice18Arid876() {}
public Invoice18Arid876(String partNumber,String partDescription,int quantity,double pricePerItem) {
this.partNumber=partNumber;
this.partDescription=partDescription;
this.quantity=quantity;
this.pricePerItem=pricePerItem;
}
/**
* A method named getInvoice 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.0.
* @return
*/
public double getInvoiceAmount() {
if(this.quantity<0) {
this.quantity=0;
}
if(this.pricePerItem<0) {
this.pricePerItem=0;
}
return this.quantity*this.pricePerItem;
}
/**
* @return the partNumber
*/
public String getPartNumber() {
return partNumber;
}
/**
* @param partNumber the partNumber to set
*/
public void setPartNumber(String partNumber) {
this.partNumber = partNumber;
}
/**
* @return the partDescription
*/
public String getPartDescription() {
return partDescription;
}
/**
* @param partDescription the partDescription to set
*/
public void setPartDescription(String partDescription) {
this.partDescription = partDescription;
}
/**
* @return the quantity
*/
public int getQuantity() {
return quantity;
}
/**
* @param quantity the quantity to set
*/
public void setQuantity(int quantity) {
this.quantity = quantity;
}
/**
* @return the pricePerItem
*/
public double getPricePerItem() {
return pricePerItem;
}
/**
* @param pricePerItem the pricePerItem to set
*/
public void setPricePerItem(double pricePerItem) {
this.pricePerItem = pricePerItem;
}
}
import java.util.Scanner;
public class InvoiceTestYourName {
/**
* The start point of the program
*
* @param args
*/
public static void main(String[] args) {
// Create Scanner object
Scanner keyboard = new Scanner(System.in);
try {
// Get the size of array
System.out.print("Enter number: ");
String partNumber=keyboard.nextLine();
System.out.print("Enter description: ");
String partDescription=keyboard.nextLine();
System.out.print("Enter quantity: ");
int quantity=keyboard.nextInt();
System.out.print("Enter price per item: ");
double pricePerItem=keyboard.nextDouble();
Invoice18Arid876 invoice18Arid876=new Invoice18Arid876(partNumber,partDescription,quantity,pricePerItem);
System.out.println("The number: "+partNumber);
System.out.println("The description: "+partDescription);
System.out.println("The quantity: "+quantity);
System.out.println("The price per item: "+pricePerItem);
System.out.println("The invoice amount: "+invoice18Arid876.getInvoiceAmount());
} catch (Exception e) {
// Display error message
System.out.println(e.getMessage());
}
// close Scanner
keyboard.close();
}
}
Comments
Leave a comment