Library Fine Calculation

Calculate library fine based on number of days a book is overdue.

JavaIntermediate
Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter number of days late: ");
        int days = sc.nextInt();

        double fine;
        if (days <= 0) {
            fine = 0;
        } else if (days <= 5) {
            fine = days * 1.0;
        } else if (days <= 10) {
            fine = 5 * 1.0 + (days - 5) * 2.0;
        } else {
            fine = 5 * 1.0 + 5 * 2.0 + (days - 10) * 5.0;
        }

        System.out.println("Fine = " + fine);

        sc.close();
    }
}

Output

Enter number of days late: 8
Fine = 11.0

We use slabs of days to apply different fine rates as the delay increases.