Calculate Discount Based on Amount in Java
intermediate· Control Flow · Conditional Statements
Problem
Discount schemes commonly scale with purchase amount, where crossing into a higher bracket unlocks a bigger percentage off the entire amount.
Given a purchase amount, calculate the discount and final payable amount using bracket-based discount percentages.
Input
amount = 7500
Output
Discount: 750.0, Final amount: 6750.0
Java Program
Java
public class DiscountCalculator {
public static void main(String[] args) {
double amount = 7500;
double discountPercent;
if (amount >= 10000) { // check from the highest bracket down, since the brackets overlap
discountPercent = 15;
} else if (amount >= 5000) {
discountPercent = 10;
} else if (amount >= 1000) {
discountPercent = 5;
} else {
discountPercent = 0;
}
double discount = amount * discountPercent / 100;
double finalAmount = amount - discount;
System.out.println("Discount: " + discount + ", Final amount: " + finalAmount);
}
}Output
Discount: 750.0, Final amount: 6750.0
Core Logic
Testing the highest amount bracket first, and stopping at the first one the purchase qualifies for, picks the correct discount percentage in a single pass.
How It Works
- 1
amount >= 10000is checked first, the highest discount bracket. - 2Each subsequent
else ifchecks a lower bracket — 5000, then 1000 — falling back to no discount below that. - 3Once
discountPercentis resolved,amount * discountPercent / 100computes the actual discount amount. - 4The final payable amount is the original amount minus that discount.
For
amount = 7500, it qualifies for the 10% bracket, giving a discount of 750 and a final amount of 6750.💡
Key Point: Unlike the tiered tax and electricity bill programs, this discount applies its percentage to the entire amount at once — the bracket only decides which single rate to use, not multiple partial rates.
Key Concepts
if / else ifdescending thresholdsarithmetic