Skip to content

Commit 4a93a0b

Browse files
committed
feat: Add CalculateDiscount class with variable argument discount calculator
WHAT the code does: - Defines a `CalculateDiscount` class with method `calculateTotalWithDiscount(double... prices)`: - Accepts a variable number of price values. - Sums all the prices. - Applies discounts based on total: - `< 500` → 10% discount. - `500–1000` → 5% discount. - `> 1000` → no discount. - Returns final discounted total. - `main()` tests the method with three examples: 1. `(100, 150, 200)` → total `450`, 10% discount → `405.0`. 2. `(300, 250)` → total `550`, 5% discount → `522.5`. 3. `(600, 500)` → total `1100`, no discount → `1100.0`. WHY this matters: - Demonstrates **varargs (variable arguments)** in methods. - Combines iteration, conditional logic, and return values. - Simulates a real-world use case: applying discounts based on order total. HOW it works: 1. Enhanced `for` loop calculates total sum. 2. Conditional logic (`if / else if / else`) applies correct discount tier. 3. Prints messages to explain discount applied. 4. Final discounted total is printed in each test case. Tips & gotchas: - Conditions are mutually exclusive, ensuring only one discount applies. - Precision issues may arise with floating-point values; use `BigDecimal` for money in real applications. - Current implementation applies discount once on total — not per item. - Varargs allows flexibility: `calculateTotalWithDiscount()` can take any number of price inputs. Use-cases: - Educational demo of **varargs + conditional logic**. - Useful for e-commerce or billing simulations. - Good practice for method design with real-world scenarios. - Can be extended with multiple discount strategies, loyalty points, or tax calculation. Short key: class-calculatediscount-varargs-discount. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 0b30607 commit 4a93a0b

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed
Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
1-
public class CalculateDiscount{
2-
// Method to calculate the total after applying a discount
3-
double calculateTotalWithDiscount(double... prices) {
4-
// Calculate the sum of all prices
1+
public class CalculateDiscount {
2+
double calculateTotalWithDiscount(double... prices)
3+
// Method to calculate the total after applying a discount.
4+
{
55
double sum = 0;
6+
7+
// Calculate the sum of all prices.
68
for (double price : prices) {
79
sum += price;
810
}
911

10-
// Apply discounts based on the sum
11-
if (sum < 500) {
12+
if (sum < 500)
13+
// Apply discounts based on the sum.
14+
{
1215
System.out.println("Applying 10% discount (Total is less than 500).");
13-
return sum * 0.9; // 10% discount
14-
} else if (sum >= 500 && sum <= 1000) {
16+
return sum * 0.9; // 10% discount.
17+
}
18+
else if (sum >= 500 && sum <= 1000)
19+
{
1520
System.out.println("Applying 5% discount (Total is between 500 and 1000).");
1621
return sum * 0.95; // 5% discount
17-
} else {
22+
}
23+
else
24+
{
1825
System.out.println("No discount applied (Total is greater than 1000).");
1926
return sum; // No discount for totals above 1000
2027
}
2128
}
22-
2329
public static void main(String[] args) {
2430
CalculateDiscount calculator = new CalculateDiscount();
2531

26-
// Example usage
27-
double total1 = calculator.calculateTotalWithDiscount(100, 150, 200); // Total = 450
28-
System.out.println("Total after discount: " + total1); // Should print 405.0
32+
double total1 = calculator.calculateTotalWithDiscount(100, 150, 200);
33+
System.out.println("Total after discount: " + total1);
2934

30-
double total2 = calculator.calculateTotalWithDiscount(300, 250); // Total = 550
31-
System.out.println("Total after discount: " + total2); // Should print 522.5
35+
double total2 = calculator.calculateTotalWithDiscount(300, 250);
36+
System.out.println("Total after discount: " + total2);
3237

33-
double total3 = calculator.calculateTotalWithDiscount(600, 500); // Total = 1100
34-
System.out.println("Total after discount: " + total3); // Should print 1100.0
38+
double total3 = calculator.calculateTotalWithDiscount(600, 500);
39+
System.out.println("Total after discount: " + total3);
3540
}
3641
}

0 commit comments

Comments
 (0)