Library Fine Calculation

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

IntermediateTopic: Module 2: Conditional Programs
Back

Java Library Fine Calculation Program

This program helps you to learn the fundamental structure and syntax of Java programming.

Try This Code
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

Understanding Library Fine Calculation

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

Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.

Table of Contents