14 a Create a class named Purchase Each purchase contains an
14). 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 objects invoice number, sale amount, and sales tax. Save the file as CreatePurchase.java.
**Please display all numbers as 01, 02, 03 etc. NOT 1,2,3,etc. Thank you!
Solution
Below is you code: -
Purchase.java
public class Purchase {
int invoiceNum = 0;
double salePrice = 0.00;
double tax;
public void setInvoiceNum(int invoice) {
invoiceNum = invoice;
}
public void setSalePrice(double saleAmount) {
salePrice = saleAmount;
tax = (saleAmount * .05);
}
public void showSale() {
System.out.println(\"Your invoice number is: \" + invoiceNum);
System.out.println(\"The price of the item is: $\" + salePrice);
System.out.println(\"Taxes on your item is: $\" + tax);
}
}
CreatePurchase.java
import java.util.Scanner;
public class CreatePurchase {
public static void main(String[] args) {
int invoice;
double saleAmount;
invoice = 0;
saleAmount = 0.00;
Purchase sold = new Purchase();
Scanner input = new Scanner(System.in);
System.out.println(\"Please enter your invoice number\");
invoice = input.nextInt();
while ((invoice < 1000) || (invoice > 8000)) {
System.out.println(\"You entered an invalid number.\");
System.out.println(\"Please enter a number between 1000 and 8000\");
invoice = input.nextInt();
}
System.out.println(\"Please enter the sale amount: \");
saleAmount = input.nextDouble();
while (saleAmount < 0) {
System.out.println(\"You entered an invalid number.\");
System.out.println(\"Please enter a number greater than 0.\");
saleAmount = input.nextDouble();
}
sold.setInvoiceNum(invoice);
sold.setSalePrice(saleAmount);
sold.showSale();
}
}
Output
Please enter your invoice number
1
You entered an invalid number.
Please enter a number between 1000 and 8000
1000
Please enter the sale amount:
2
Your invoice number is: 1000
The price of the item is: $2.0
Taxes on your item is: $0.1