Question
This also must have comments and documentation
a.Create a class named Purchase. Each Purchase contains an invoice number, amount of sale, and amount of sales tax. Include set methods for the invoice number and sale amount. Within the set () method for the sale amount, calculate the sales tax as 5% of the sale amount. Also include a display method that displays a purchase’s details. Save the file as Purchase. java
b.Create an application that declares a Purchase object and prompts the user for purchase details. When you prompt for an invoice number, do not let the user proceed until a number between 1,000 and 8,000 has been entered. When you prompt for a sale amount, do not proceed until the user has entered a non-negative value. After a valid Purchase object has been created, display the object’s invoice number, sale amount, and sales tax. Save the file as CreatePurchase.java
--------------------------------------------------------------------------------
Explanation / Answer
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class Purchase { private int invoiceNo; private double saleAmount; private double tax; // Not sure if this constructor is needed ?! public Purchase(int invoiceNo, double saleAmount, double tax){ this.invoiceNo = invoiceNo; this.saleAmount = saleAmount; this.tax = tax; } public Purchase(){ } // The set methods public void setInvoiceNumber(int invoiceNo) { this.invoiceNo = invoiceNo; } public void setSaleAmount(double saleAmount) { this.saleAmount = saleAmount; this.tax = (this.saleAmount * .05); } // The DisplayMethod public void DisplayInfo () { System.out.println(" Invoice Number = " + invoiceNo + " Amount of Sale = " + saleAmount + " Sales Tax = " + tax); } } public class CreatePurchase { // Here’s the main method which will run the application for you. I’ll add comments for explanations public static void main(String[]args) throws IOException{ /* * When you prompt for an invoice number, do not let the user proceed until a number between 1,000 and 8,000 has been entered. * When you prompt for a sale amount, do not proceed until the user has entered a non-negative value. * After a valid Purchase object has been created, display the object’s invoice number, sale amount, and sales tax. * */ int inputInvoice = -1; double saleAm = -1; // The following Buffered Reader is used to read from the user BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); while(inputInvoice < 1000 || inputInvoice > 8000){ System.out.println("Please enter an invoice number from 1000 to 8000: "); // BufferedReader always returns a string String invoiceNoStr = stdin.readLine(); // Change the string input read into integer and save it to inputInvoice inputInvoice = Integer.parseInt(invoiceNoStr); } System.out.println("Thank You! Current Invoice Number = "+inputInvoice); while(saleAm < 0){ System.out.println("Please enter a Sale Amount (0 or greater)"); // BufferedReader always returns a string String amountStr = stdin.readLine(); // Change the string input read into integer and save it to inputInvoice saleAm = Double.parseDouble(amountStr); } System.out.println("Thank You!"); // Creating the Purchase Purchase p = new Purchase(); p.setInvoiceNumber(inputInvoice); p.setSaleAmount(saleAm); p.DisplayInfo(); System.out.println("Thanks you! Have a Nice Day"); } } // I tried it and it works! I'll be glad to answer any questions you might have.