Question: 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. Write a test application named InvoiceTest that demonstrates class Invoice’s capabilities.
To run the application save both files with the same name as the class (because it is a public class) and with the .java file extension (in this case Ex03_13.java and Ex03_13_Test.java). Then compile both classes and run Ex03_13_Test. Ex03_13 will not run because it does not have a main method.
To compile both classes at the same time using the command prompt, use the command
javac Ex03_13.java Ex03_13_Test.java
To run Ex03_13_Test, use the command
java Ex03_13_Test
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 3.13 - Invoice Class<br /> * This Program Creates An Invoice For An Item Sold At The Store<br /> *<br /> */ <br /><br /> public class Ex03_13 {<br /> <br /> private String partNumber;<br /> private String partDescription;<br /> private int quantityOfItemPurchased;<br /> private double pricePerItem;<br /> <br /> public Ex03_13 (String number, String description, int quantity, double price) {<br /> partNumber = number;<br /> partDescription = description;<br /> quantityOfItemPurchased = quantity;<br /> pricePerItem = price;<br /> }<br /> <br /> public void setPartNumber (String number) {<br /> partNumber = number;<br /> }<br /> <br /> public String getPartNumber () {<br /> return partNumber;<br /> }<br /> <br /> public void setPartDescription (String description) {<br /> partDescription = description;<br /> }<br /> <br /> public String getPartDescription () {<br /> return partDescription;<br /> }<br /> <br /> public void setQuantityOfItemPurchased (int quantity) {<br /> quantityOfItemPurchased = quantity;<br /> }<br /> <br /> public int getQuantityOfItemPurchased () {<br /> return quantityOfItemPurchased;<br /> }<br /> <br /> public void setPricePerItem (double price) {<br /> pricePerItem = price;<br /> }<br /> <br /> public double getPricePerItem () {<br /> return pricePerItem;<br /> }<br /> <br /> public double getInvoiceAmount () {<br /> double calculateTotalAmount;<br /> calculateTotalAmount = quantityOfItemPurchased * pricePerItem;<br /> return calculateTotalAmount;<br /> }<br /> <br /> }<br /> |
Below is Class Ex03_13_Test.java to test Class Ex03_13.java
1 2 |
//Exercise 3.13 - Invoice Class Test<br />//This Program Demonstrates Class Ex03_13 (Invoice Class) Capabilities<br /><br />import java.util.Scanner;<br /><br />public class Ex03_13_Test {<br /> public static void main (String [] args) {<br /><br /> Ex03_13 invoice = new Ex03_13 (" ", " ", 0, 0.0);<br /><br /> Scanner input = new Scanner (System.in);<br /> <br /> String item;<br /> String description;<br /> int quantity;<br /> double price;<br /> <br /> System.out.print ("Enter Number: ");<br /> item = input.nextLine();<br /> invoice.setPartNumber(item);<br /> <br /> System.out.print ("Enter Description of Item Purchased: ");<br /> description = input.nextLine();<br /> invoice.setPartDescription(description);<br /> <br /> System.out.print ("Enter The Quantity of Item Purchased: ");<br /> quantity = input.nextInt();<br /> if (quantity > 0)<br /> invoice.setQuantityOfItemPurchased(quantity);<br /> <br /> System.out.print ("Enter The Price Per Item: ");<br /> price = input.nextDouble();<br /> if (price > 0)<br /> invoice.setPricePerItem (price);<br /> <br /> System.out.printf ("The Total Amount for all items purchased = $%.2fn",<br /> invoice.getInvoiceAmount());<br /><br /> }<br />}<br /> |
4 Comments
This comment has been removed by the author.
Hello Katty. I'm glad you found it helpful.
very helpfull….
very helpfull….