Question

In: Computer Science

3.12 (Invoice Class) Create a class called Invoice that a hardware store might use to represent...

3.12 (Invoice Class) 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 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. Add a main method to the class that demonstrates class Invoice's capabilities by reading in values from stdin and prints to stdout the complete state of the object each time the state of the object is changed.

This is for an intro to java

Solutions

Expert Solution

Java Program:

package invoicedriver;

class Invoice {
   //Private data members
   private String partNumber;
   private String partDescription;
   private int quantity;
   private double pricePerItem;

//Constructor
public Invoice(String partNumber, String partDescription, int quantity, double pricePerItem) {
this.partNumber = partNumber;
this.partDescription = partDescription;
//Checking values
if(quantity < 0) {
this.quantity = 0;
}
else {
this.quantity = quantity;
}
if(pricePerItem < 0) {
this.pricePerItem = 0.0;
}
else {
this.pricePerItem = pricePerItem;
}
}
  
//Setter and Getter methods
public String getPartNumber() {
return partNumber;
}

public void setPartNumber(String partNumber) {
this.partNumber = partNumber;
}

public String getPartDescription() {
return partDescription;
}

public void setPartDescription(String partDescription) {
this.partDescription = partDescription;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
//Checking values
if(quantity < 0) {
this.quantity = 0;
}
else {
this.quantity = quantity;
}
}

public double getPricePerItem() {
return pricePerItem;
}

public void setPricePerItem(double pricePerItem) {
//Checking values
if(pricePerItem < 0) {
this.pricePerItem = 0.0;
}
else {
this.pricePerItem = pricePerItem;
}
}
  
//Method that calculates the invoice amount
public double getInvoiceAmount() {
return (this.quantity * this.pricePerItem);
}
}

public class InvoiceDriver {

//Main method
public static void main(String[] args) {
  
//Creating objects
Invoice inv1 = new Invoice("P101", "Android Mobiles", 10, 23.5);
Invoice inv2 = new Invoice("P201", "Tablet", 5, 14.9);
  
//Printing result
System.out.println("\nInvoice 1: \n\n Part Number: " + inv1.getPartNumber() + "\n Description: " + inv1.getPartDescription() + "\n Invoice Amount: $" + inv1.getInvoiceAmount());
System.out.println("\nInvoice 2: \n\n Part Number: " + inv2.getPartNumber() + "\n Description: " + inv2.getPartDescription() + "\n Invoice Amount: $" + inv2.getInvoiceAmount());
}
}
__________________________________________________________________________________________

Sample Run:


Related Solutions

Invoice Class - Create a class called Invoice that a hardware store might use to represent...
Invoice Class - 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. If the quantity passed to the constructor is...
Invoice Class - Create a class called Invoice that a hardware store might use to represent...
Invoice Class - 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. If the quantity passed to the constructor is...
with PHP Create a class called Invoice that a hardware store might use to represent an...
with PHP 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...
2. Create a class called Invoice that a store might use to represent an invoice for...
2. Create a class called Invoice that a store might use to represent an invoice for an item sold at the store. An Invoice should include four data members—the ID for the item sold (type string), name of item (type string), item description (type string) and the price of the item (type int). Your class should have a constructor that initializes the four data members. A constructor that receives multiple arguments. Example: ClassName( TypeName1 parameterName1, TypeName2 parameterName2, ... ) Provide...
----------------------------------------------------------------------------------------------- Create a class called MathOperations that a teacher might use to represent the basic type...
----------------------------------------------------------------------------------------------- Create a class called MathOperations that a teacher might use to represent the basic type of mathematical operations that may be performed. The class should include the following: Three double private variables as instance variables, number1, number2, and result. Your class should have a default constructor that initializes the three instance variables to zero. Your class should also have a constructor that initializes the two instance variables (number1 and number2) to the value entered by the user from keyboard....
With PHP Create a class called Account that a bank might use to represent customers' bank...
With PHP Create a class called Account that a bank might use to represent customers' bank accounts. Your class should include one data member of type int to represent the account balance. Your class should provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it is greater than or equal to 0. If not, the balance should be set to 0 and the...
Please solve using jupyter notebook . 10.10- (Invoice Class) Create a class called Invoice that a...
Please solve using jupyter notebook . 10.10- (Invoice Class) 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 data attributes—a part number (a string), a part description (a string), a quantity of the item being purchased (an int) and a price per item (a Decimal). Your class should have an __init__ method that initializes the four data attributes....
12. Create a class called bMoney. It should store money amounts as long doubles. Use the...
12. Create a class called bMoney. It should store money amounts as long doubles. Use the function mstold() to convert a money string entered as input into a long double, and the function ldtoms() to convert the long double to a money string for display. (See Exercises 6 and 10.) You can call the input and output member functions getmoney() and putmoney(). Write another member function that adds two bMoney amounts; you can call it madd(). Adding bMoney objects is...
IN C++ Create a class called TextInt. The purpose of a TextInt is to store an...
IN C++ Create a class called TextInt. The purpose of a TextInt is to store an integer and convert it to the English text form of the integer when needed, such as ‘zero’ for 0, ‘one’ for 1, and so on, up to ‘nine thousand nine hundred ninety nine’ for 9999. You do NOT need punctuation (commas, hyphens, ‘and’, etc.) The TextInt class should have its own .h and .cpp files. At least the translate function should be implemented in...
Create a class named UsedFurnitureItem to represent a used furniture item that the store sells. Private...
Create a class named UsedFurnitureItem to represent a used furniture item that the store sells. Private data members of a UsedFurnitureItem are: age (double) // age in years – default value for brandNewPrice (double) // the original price of the item when it was brand new description (string) // a string description of the item condition (char) // condition of the item could be A, B, or C. size (double) // the size of the item in cubic inches. weight...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT