public static int factorial(int n) {
    if (n < 0) {
        return -1;
    }
    int result = 1;
    while (n > 0) {
        result = result * n;
        n = n - 1;
    }
    return result;
}
Create a function in Java to find the factorial of a number.