Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class called Invoice that a hardware store might use to represent an in

ID: 670602 • Letter: C

Question

Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. This class has one instance variable: quantity of the item being purchased (type int). Provide a property with get and set accessors for quantity. If the value passed to the set accessor is negative, the value of the instance variable should be left unchanged. Your class should have a constructor that initializes quantity. In addition, provide a method GetInvoiceAmount that calculates the invoice amount. Assume that the unit price of the item being sold is $0.75. Therefore, the invoice amount equals quantity multiplied by $0.75. Use decimal for variables and values that are related to money. Write a test application named InvoiceTest that demonstrates class Invoice’s capabilities. In this test application, ask the user to enter quantity. Use it to create an invoice object. Retrieve the value stored in the quantity instance variable (via the get accessor of its corresponding property) and display it. Use the GetInvoiceAmount method to calculate the invoice amount. Display the invoice amount.

Explanation / Answer

public class Invoice { //main method required in every java program public static void main( String[] args ) { //creates a scanner and enables apps to read bytes of info typed in System.out.println(" Enter item number, description, quantity, price - all separated by commas: "); Scanner input = new Scanner( System.in); String str = input.next(); String [] str1 = str.split(","); Invoice in = new Invoice(str1[0], str1[1], Integer.parseInt(str1[2]), Double.parseDouble(str1[3]) ); System.out.println(in.toString()); } //instance variables String number; String description; int quantity; double prices; //constructor public Invoice( String pt, String dsc, int count, double prices ) { number = pt; description = dsc; setQuantity( count ); setPrices( prices ); } //set part number public void setNumber( String pt ) { number = pt; } //get part number public String getNumber() { return number; } //set part description public void setDescription( String dsc ) { number = dsc; } //get part description public String getDescription() { return description; } // set part quantity public void setQuantity( int count ) { quantity = ( count < 0 ) ? 0 : count; } // get part quantity public int getQuantity() { return quantity; } // set price per item public void setPrices( double price ) { prices = ( price < 0.0 ) ? 0.0 : price; } // get price per item public double getPrices() { return prices; } // return String public String toString() { return String.format( "%s: %s: %s (%s) %s: %d %s: $%,.2f %s: $%,.2f", "invoice", "part number", getNumber(), getDescription(), "quantity", getQuantity(), "price per item", getPrices(), "Total", getInvoiceAmount() ); } //method calculates invoice amount and returns double as a value public double getInvoiceAmount() { return getQuantity() * getPrices(); // calculate total cost } // end method getPaymentAmount } // end class Invoice
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote