Library Fine Calculation in Java

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

IntermediateModule 2: Conditional ProgramsExample 16 of 20
library-fine-calculation.java
1import java.util.Scanner;
2
3public class Main {
4 public static void main(String[] args) {
5 Scanner sc = new Scanner(System.in);
6
7 System.out.print("Enter number of days late: ");
8 int days = sc.nextInt();
9
10 double fine;
11 if (days <= 0) {
12 fine = 0;
13 } else if (days <= 5) {
14 fine = days * 1.0;
15 } else if (days <= 10) {
16 fine = 5 * 1.0 + (days - 5) * 2.0;
17 } else {
18 fine = 5 * 1.0 + 5 * 2.0 + (days - 10) * 5.0;
19 }
20
21 System.out.println("Fine = " + fine);
22
23 sc.close();
24 }
25}

Output

Enter number of days late: 8
Fine = 11.0

What's going on

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