Public library has sa simple policy for late retums te al afred free each day X, a book is overdue, but there's a grace period representedly Y allowing returns within a certain number of days past the de curring a fee. However, the policy includes a cap represented by the total fee, beyond which no additional charges are applied, howe the book is returned. Your task is to find and retum an integer e total late fee for a returned book.
![]() |
Smart Library Returns: Your Go-To Late Fee Calculator “ Code In Java And Python.” |
Input Specification:
input1: An integer value X representing the number of days cook returned late.
input2: An integer value Y representing the number of days past the the that can elapse without incurring a fee.
input3: An integer value Z representing the fixed fee charged for each the Inrdue beyond the grace period.
input4: An integer value A representing the maximum total late fee for that can be charged.
Output Specification:
Return an integer value representing the total late fee for a returned book.
Explanation:
Here, the number of overdue days = 5, grace period=3, fee charged per day =2 and the maximum total fee that can be charged =10
- Effective Days = overdue days - grace period 5-3-2
- Total fees = Fees per day Effective days=2*2-4
- Since total fees < maximum total fee that can be charged (41 the output.
Example 2:
- input1: 15
- input2: 5
- input3: 3
- input4: 20
- Output: 20
Explanation:
wordue days = 15, grace period = 5, fee changed per day -
20
Explanation:
Here, the number of overdue days = 15, grace period 5, fee charged per day =3 and maximum total fee that can be charged = 20.
- Effective Days = overdue days - grace period 15-5-10
- Total fees = Fees per day * Effective days = 3*10=30
Since total fees fee that can > maximum total fee can be charged (30>20) is returned as the output he output..
Java Code
import java.io.*;
import java.util.*;
class UserMainCode {
public int bookFine(int input1, int input2, int input3, int input4) {
// Calculate effective overdue days
int effectiveDays = input1 - input2;
// If effective days are less than or equal to zero, no fee is charged
if (effectiveDays <= 0) {
return 0;
}
// Calculate total fee
int totalFee = effectiveDays * input3;
// Apply the maximum total fee cap
if (totalFee > input4) {
totalFee = input4;
}
return totalFee;
}
public static void main(String[] args) {
UserMainCode code = new UserMainCode();
// Example 1
int input1_1 = 5;
int input2_1 = 3;
int input3_1 = 2;
int input4_1 = 10;
System.out.println("Total late fee: " + code.bookFine(input1_1, input2_1, input3_1, input4_1)); // Output: 4
// Example 2
int input1_2 = 15;
int input2_2 = 5;
int input3_2 = 3;
int input4_2 = 20;
System.out.println("Total late fee: " + code.bookFine(input1_2, input2_2, input3_2, input4_2)); // Output: 20
}
}
Explanation:
- Imports:
- import java.io.*; and import java.util.*; are included to handle input-output and utility functions.
- The UserMainCode class includes the bookFine method.
- The bookFine method takes four integer inputs (input1, input2, input3, input4) which represent the number of overdue days, grace period, fee per day, and the maximum total fee respectively.
- It calculates the effective overdue days by subtracting the grace period from the overdue days.
- If the effective days are less than or equal to zero, it returns 0 indicating no fee.
- It calculates the total fee by multiplying the effective days by the fee per day.
- If the calculated total fee exceeds the maximum total fee, it applies the cap by setting the total fee to the maximum total fee.
- The main method demonstrates the usage of the bookFine method with the provided examples.
- It creates an instance of UserMainCode and calls bookFine with sample inputs to print the total late fee for each example.
Python Code
class Library:
def book_fine(self, overdue_days, grace_period, fee_per_day, max_total_fee):
# Calculate effective overdue days
effective_days = overdue_days - grace_period
# If effective days are less than or equal to zero, no fee is charged
if effective_days <= 0:
return 0
# Calculate total fee
total_fee = effective_days * fee_per_day
# Apply the maximum total fee cap
if total_fee > max_total_fee:
total_fee = max_total_fee
return total_fee
# Example usage
library = Library()
# Example 1
overdue_days_1 = 5
grace_period_1 = 3
fee_per_day_1 = 2
max_total_fee_1 = 10
print("Total late fee:", library.book_fine(overdue_days_1, grace_period_1, fee_per_day_1, max_total_fee_1)) # Output: 4
# Example 2
overdue_days_2 = 15
grace_period_2 = 5
fee_per_day_2 = 3
max_total_fee_2 = 20
print("Total late fee:", library.book_fine(overdue_days_2, grace_period_2, fee_per_day_2, max_total_fee_2)) # Output: 20
- The Library class contains the book_fine method.
- The book_fine method takes four parameters: overdue_days, grace_period, fee_per_day, and max_total_fee which represent the number of overdue days, grace period, fee per day, and the maximum total fee respectively.
- It calculates the effective overdue days by subtracting the grace period from the overdue days.
- If the effective days are less than or equal to zero, it returns 0 indicating no fee.
- It calculates the total fee by multiplying the effective days by the fee per day.
- If the calculated total fee exceeds the maximum total fee, it applies the cap by setting the total fee to the maximum total fee.
- The example usage demonstrates how to use the book_fine method with sample inputs and prints the total late fee for each example.