// Methods.java — defining, calling and overloading methods.
public class Methods {

    // A method with parameters and a return value
    static double lineTotal(int quantity, double price) {
        return quantity * price;
    }

    // Overloading: same name, different parameters
    static double lineTotal(int quantity, double price, double discountPercent) {
        double total = lineTotal(quantity, price);
        return total - total * discountPercent / 100;
    }

    // A method that returns nothing (void)
    static void printReceipt(String customer, double amount) {
        System.out.printf("%-12s %8.2f%n", customer, amount);
    }

    // Varargs: accept any number of arguments
    static int sum(int... numbers) {
        int total = 0;
        for (int n : numbers) {
            total += n;
        }
        return total;
    }

    // Recursion: a method that calls itself
    static long factorial(int n) {
        return n <= 1 ? 1 : n * factorial(n - 1);
    }

    public static void main(String[] args) {
        printReceipt("Acme Ltd", lineTotal(24, 8.50));
        printReceipt("Globex", lineTotal(10, 20.00, 15));
        System.out.println("Sum: " + sum(1, 2, 3, 4));
        System.out.println("5! = " + factorial(5));
    }
}
