Question: Writing a java program that calculates the final retail price for items that are on sale at a store. The user of the program enters
Writing a java program that calculates the final retail price for items that are on sale at a store. The user of the program enters the original item price and the discount percent, the program then calculates the final price including sales tax and displays the information to the user.
Specifications
Please follow the instructions below:
1. Create a new NetBeans project called Retail.
2. Add a new Java class to the project called Retail.
3. Add a new method to the class called isValidValue. Below is the method header and the algorithm you need to follow:
/**
* The isValidValue method checks if the String parameter consists of a positive number.
* @param value The string value to check.
* @return True if the parameter is a positive number. False if the parameter is not
* numeric, or if it's numeric but negative.
*/
public static boolean isValidValue(String value)
{
// check if the value parameter contains only numbers using the matches method of the
// String class as follows:
// value.matches("^[0-9]+$")
// if the matches method returns false, then the isValidValue method also returns false
// else, do the following:
// 1- convert the value parameter to a double value using the parseDouble method of the
// Double class
// 2- check if the double value is negative; if so, the isValidValue method returns
// false. Else, the method returns true.
}
4. In the main method do the following:
A.Declare a constant of type double called SALES_TAX and initialize it to 7.5.
B. Declare three variables of type String called message, priceInput, and discountInput.
C. Declare five variables of type double called originalPrice, discountedPrice, salesTax, finalPrice, and discount.
D. Call the showMessageDialog method of the JOptionPane class to show a message as follows:
JOptionPane.showMessageDialog(null, "This program calculates the final price for items on sale";
E. Write a do/while loop that keeps prompting the user to enter the item price as long as they dont enter a valid value. In the body of the loop initialize the priceInput variable to the value entered by the user. In the condition of the loop, call the isValidValue method as follows:
do
{
priceInput = JOptionPane.showInputDialog("Please enter the original item price");
} while( !isValidValue(priceInput));
F. Similarly, write another do/while loop to prompt the user to enter the discount percentage. The message shoud be: "Please enter percent off (positive numbers only)". The value entered is used to initialize the discountInput variable.
G. After the two do/while loops have executed, the priceInput and discountInput variables should have a positive number, but they are in String format. Convert these two values to double and store the converted values in the originalPrice and discount variables respectively.
H. Initialize the discountedPrice variable to be the original item price minus the discount.
I. Initialize the salesTax variable using the discountedPrice variable and SALES_TAX constant.
J. Initialize the finalPrice variable to be the discountedPrice variable plus the salesTax variable.
K. Set the message variable to have all the text that will be displayed to the user. You may use the format method of the String class to format the amounts. For example:
message = String.format("%-20s $%,8.2f", "Original Price:", originalPrice);
L. Display the message variable to the user as follows:
JOptionPane.showMessageDialog(null, message);
M. Include the following as the last line of code in the program:
System.exit(0);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
