In: Computer Science
Surfer Sam’s has a sale this Saturday, you need to reprogram the tills for the sale. Write the Java code.
Ask for a product price. If the total price is less than or equal to $100, apply a discount of 3% to the total sale. If the price is more than $100 and less than or equal to 500, apply a discount of 5% to the total sale. If the price is higher than $500, apply a discount of 8% to the total sale. Add the GST and print out the: original price, the discount amount, GST and final price. The following is a sample, user input is shown in bold underline:
1st Sample Run:
Enter the price: 699.90
Description Amount
Price 699.90
% Discount 8.00
Discount 55.99
Summary 643.91
GST 32.20
Final Price 676.10
2nd Sample Run:
Enter the price: 29.95
Description Amount
Price 29.95
% Discount 3.00
Discount 0.90
Summary 29.05
GST 1.45
Final Price 30.50
Solve this problem using doubles.
please answer in java
CODE -
import java.util.Scanner;
public class sale
{
public static void main(String[] args) {
// Variable initialization double discount_per = 0, discount, totalPrice, gst, finalPrice;
Scanner keyboard = new Scanner(System.in);
// Take price as input from user
System.out.print("Enter the price: ");
double price = keyboard.nextDouble();
// Close the scanner
keyboard.close();
// Determine the discount percentage according to the price
if(price <= 100)
discount_per = 3;
else if (price <= 500)
discount_per = 5;
else if (price > 500)
discount_per = 8;
// Calculate the discount
discount = (discount_per * price) / 100;
// Calculate the price after discount
totalPrice = price - discount;
// Calculate the GST
gst = (5 * totalPrice) / 100;
// Calculate final price after including GST
finalPrice = totalPrice + gst;
// Displaying the details
System.out.printf("%-17s %s\n", "Description", "Amount");
System.out.printf("%-15s %8.2f\n", "Price", price);
System.out.printf("%-15s %8.2f\n", "/% Discount", discount_per);
System.out.printf("%-15s %8.2f\n", "Discount", discount);
System.out.printf("%-15s %8.2f\n", "Summary", totalPrice);
System.out.printf("%-15s %8.2f\n", "GST", gst);
System.out.printf("%-15s %8.2f\n", "Final Price", finalPrice);
}
}
SCREENSHOTS -
CODE -
OUTPUT -
If you have any doubt regarding the solution, then do comment.
Do upvote.