In this example, we will calculate the fine for a library book based on the number of overdue days. We'll use if
statements to apply a fine based on the number of days the book is overdue.
1️⃣ Example: Simple Library Fine Calculation
Let's assume the following:
- A fine of $1 per day for overdue books.
- If the book is returned within 7 days of the due date, there is no fine.
- If the book is returned after 7 days but within 15 days, the fine is $2 per day.
- If the book is returned after 15 days, the fine is $5 per day.
Code Example:
#include <iostream>
using namespace std;
int main() {
int overdueDays;
double fine = 0.0;
// Taking number of overdue days as input
cout << "Enter the number of overdue days: ";
cin >> overdueDays;
// Fine calculation based on overdue days
if (overdueDays > 15) {
fine = overdueDays * 5; // $5 per day if overdue for more than 15 days
} else if (overdueDays > 7) {
fine = overdueDays * 2; // $2 per day if overdue between 8 and 15 days
} else if (overdueDays > 0) {
fine = overdueDays * 1; // $1 per day if overdue between 1 and 7 days
} else {
fine = 0; // No fine if no overdue days
}
// Displaying the calculated fine
cout << "The fine for overdue book is: $" << fine << endl;
return 0;
}
Explanation:
- If the book is overdue for more than 15 days, the fine is calculated at $5 per day.
- If the book is overdue for between 8 and 15 days, the fine is $2 per day.
- If the book is overdue for between 1 and 7 days, the fine is $1 per day.
- If there are no overdue days (
overdueDays <= 0
), then the fine is $0.
Output 1 (if overdueDays = 5):
Enter the number of overdue days: 5
The fine for overdue book is: $5
Output 2 (if overdueDays = 10):
Enter the number of overdue days: 10
The fine for overdue book is: $20
Output 3 (if overdueDays = 20):
Enter the number of overdue days: 20
The fine for overdue book is: $100
Output 4 (if overdueDays = 0):
Enter the number of overdue days: 0
The fine for overdue book is: $0
2️⃣ Real-World Application:
- Library Management Systems: This type of fine calculation system can be integrated into library management software where users can check their due dates and calculate any fines they may owe for overdue books.
- Automated Fine Calculation: This logic could be used in automated systems that calculate fines each day a book is overdue.
3️⃣ Additional Enhancements:
You could extend this program to consider more advanced conditions, like:
- Different fine rates for different types of books (e.g., textbooks, novels).
- Adding discounts or waivers based on membership or student status.
- Tracking multiple books with different overdue days and calculating the total fine.
Would you like to extend this example to handle such enhancements, or include other factors like membership status or type of book?
0 Comments