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

Create a new Java project. The application uses an input dialog box. a. Create a

ID: 3704065 • Letter: C

Question

Create a new Java project.

The application uses an input dialog box.

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 nonnegative 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.

Sample Output:

Invoice #1234, Amount of sale: $150.15, Tax: $7.51

- If the user enters invalid invoice number:

- Also, create a dialog box for an invalid amount.

Input Enter invoice number 1234 OK Cancel

Explanation / Answer

CreatePurchase.java

import javax.swing.JOptionPane;

public class CreatePurchase {

public static void main(String[] args) {

int invoiceNumber = Integer.parseInt(JOptionPane.showInputDialog("Enter invoice number: "));

while(invoiceNumber<1000 || invoiceNumber>8000) {

invoiceNumber = Integer.parseInt(JOptionPane.showInputDialog("Invoice number must be between 1000 and 8000 Enter invoice number: "));

}

double saleAmount = Double.parseDouble(JOptionPane.showInputDialog("Enter sale amount: "));

Purchase p = new Purchase();

p.setInvoiceNumber(invoiceNumber);

p.setSaleAmount(saleAmount);

p.display();

}

}

Purchase.java

import java.text.DecimalFormat;

public class Purchase {

private int invoiceNumber;

private double saleAmount, saleTax;

public Purchase() {

}

public void setInvoiceNumber(int invoiceNumber) {

this.invoiceNumber = invoiceNumber;

}

public void setSaleAmount(double saleAmount) {

this.saleAmount = saleAmount;

this.saleTax=(saleAmount*5)/100;

}

public void display() {

DecimalFormat df = new DecimalFormat("0.00");

System.out.println("Invoice #"+invoiceNumber+", Amount of sale: $"+df.format(saleAmount)+", Tax: $"+df.format(saleTax));

}

}

Output:

Invoice #1234, Amount of sale: $150.15, Tax: $7.51

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