Real World Example

Payment Method Fees

Payment gateways like Razorpay and PayU charge different fees per payment method. A switch expression maps each method to its fee rule cleanly.

Example

public class Main {
    public static void main(String[] args) {
        String[] methods = {"UPI", "DEBIT_CARD", "CREDIT_CARD", "NET_BANKING", "WALLET"};
        double amount = 2000;
        for (String method : methods) {
            double feePercent = switch (method) {
                case "UPI" -> 0.0;
                case "DEBIT_CARD" -> 0.4;
                case "CREDIT_CARD", "WALLET" -> 1.8;
                case "NET_BANKING" -> {
                    double base = 1.2;
                    yield amount > 1000 ? base - 0.2 : base;
                }
                default -> throw new IllegalArgumentException("Unsupported: " + method);
            };
            double fee = amount * feePercent / 100;
            System.out.printf("%-12s fee %.1f%% = Rs. %6.2f%n", method, feePercent, fee);
        }
    }
}

Output

UPI          fee 0.0% = Rs.   0.00
DEBIT_CARD   fee 0.4% = Rs.   8.00
CREDIT_CARD  fee 1.8% = Rs.  36.00
NET_BANKING  fee 1.0% = Rs.  20.00
WALLET       fee 1.8% = Rs.  36.00