In: Computer Science
Use JAVAfor this program
Program Description
You work in a local gift shop that is currently running a large sale. You have been asked to create a program to calculate the total of a user’s purchases and determine if they qualify for a discount. Use the following steps to create the program:
Subtotal = Price * Quantity
Subtotal |
Discount |
Under $25 |
5% discount |
$25 but not more than $75. |
10% discount |
Over $75 |
15% discount |
Discount Amount = Subtotal * (Discount/100)
Discounted Cost = Subtotal - Discount Amount
Sample Input and Output (formatting, spacing, and indentation should be as shown below)
What is the price of the item you wish to purchase?
17.99
What is the quantity you wish to purchase? 3
What is the name of the item? T-Shirt
Discount Calculation Utility
Item
Name:
T-Shirt
Price: $17.99
Quantity:
3
Subtotal: $53.97
Discount: $5.40
Discounted Cost: $48.57
All the explanation is in the comments of the code itself.
Code--
import java.util.*;
public class GiftShop
{
public static void main(String[] args)
{
Scanner sc=new
Scanner(System.in);
double
price,subtotal,discountAmt,discountedCost;
int quantity,discount;
String name;
//ask the user the price of the
item
System.out.print("What is the price
of the item you wish to purchase? ");
price=sc.nextDouble();
//ask the user for quantity
System.out.print("What is the
quantity you wish to purchase? ");
quantity=sc.nextInt();
sc.nextLine();
//ask the user for item name
System.out.print("What is the name
of the item? ");
name=sc.nextLine();
//do the required calculation
subtotal=price*quantity;
//find the discount
if(subtotal<(double)25)
{
discount=5;
}
else
if(subtotal<(double)75)
{
discount=10;
}
else
{
discount=15;
}
discountAmt=subtotal*((double)discount/100);
discountedCost=subtotal-discountAmt;
//create a string object
String str=new String("Discount
Calculation Utility");
//display the output
System.out.println(str);
System.out.println("Item
Name:\t"+name);
System.out.printf("Price:
$%.2f\n",price);
System.out.println("Quantity:\t"+quantity);
System.out.printf("Subtotal:
$%.2f\n",subtotal);
System.out.printf("Discount:
$%.2f\n",discountAmt);
System.out.printf("Discounted Cost:
$%.2f\n",discountedCost);
}
}
Output Screenshot--
Note-
Please upvote if you like the effort.