In: Computer Science
Write a Java program called RevenueAdvanced to calculate the
revenue from a sale based on the unit price and quantity of a
product input by the user. (use if and if-else
statements)
• The discount rate is
o 0% for the quantity purchased between 1 and 49 units.
o 10% for the quantity purchased between 50 and 99 units.
o 15% for the quantity purchased between 100 and 149 units.
o 25% for the quantity purchased greater than or equal150
units.
New: Catch any invalid inputs as the following, if so, output a
warning message and end the program.
o Employee Number: should be nine digits, no zero leading,
non-negative, and no decimal points.
o Item Price: should be within the range [0.25, 100], and
non-negative.
o Quantity: non-negative, and no decimal points.
Demo (1) a successful run:
Welcome to "Temple" store
Enter item price: 10
Enter quantity: 60
The item price is: $10.0
The order is: 60 item(s)
The cost is: $600.0
The discount is: 10.0%
The discount amount is: $60.0
The total is: $540.0
Thank You for using "Temple" store
Demo (2) a failed run:
Welcome to "Temple" store
Enter item price: -30
This is not a valid item price.
Please run the program again
Thank You for using "Temple" store
Demo (3) another failed run:
Welcome to "Temple" store
Enter item price: 10
Enter quantity: -90
This is not a valid quantity order.
Please run the program again
Thank You for using "Temple" store
Explanation:
here is the code which asks for the item quantity and the price of each item from the user and then calculates the total of the amount and shows the discount and other details on the screen.
Code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
System.out.println("Welcome to
\"Temple\" store");
Scanner sc = new
Scanner(System.in);
System.out.print("Enter item price:
");
double item_price =
sc.nextDouble();
if(item_price<0.25 ||
item_price>100)
{
System.out.println("This is not a
valid item price.");
System.out.println("Please run the
program again");
}
else
{
System.out.print("Enter quantity:
");
double quantity =
sc.nextDouble();
if(quantity<0 ||
quantity-(int)quantity >0)
{
System.out.println("This is not a
valid quantity order.");
System.out.println("Please run the
program again");
}
else
{
System.out.println("The order is:
"+(int)quantity+" item(s)");
System.out.println("The cost is:
$"+(quantity*item_price));
int discount;
if(quantity<50)
{
discount = 0;
}
else if(quantity<100)
{
discount = 10;
}
else if(quantity<150)
{
discount = 15;
}
else
{
discount = 25;
}
System.out.println("The discount
is: "+discount+"%");
System.out.println("The discount
amount is: $"+(discount*(quantity*item_price)/100.0));
System.out.println("The total is:
$"+((quantity*item_price)-(discount*(quantity*item_price)/100.0)));
}
}
System.out.println("Thank you for
using \"Temple\" store");
}
}
Output:
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!