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...
----------------------------------------------------------------------------------------------- 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....
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...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not reduce fractions, do not use "const," do not provide any constructors, do not use three separate files. You will not receive credit for the assignment if you do any of these things. In your single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. It is required that you provide these member...
Create a class called RandomGuess. In this game, generate and store a random number between 1...
Create a class called RandomGuess. In this game, generate and store a random number between 1 and 100. Then, continuously (in a loop): Ask the user to enter a number between 1 and 100 Let the user know if the guess is high or low, until the user enters the correct value If the user enters the correct value, then display a count of the number of attempts it took and exit
Task Create a class called Mixed. Objects of type Mixed will store and manage rational numbers...
Task Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). The class, along with the required operator overloads, should be written in the files mixed.h and mixed.cpp. Details and Requirements Finish Lab 2 by creating a Mixed class definition with constructor functions and some methods. The Mixed class should have public member functions Evaluate(), ToFraction(), and Simplify(). The Evaluate() function should return a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT