java biggnars need to comment and output 1. Library Fines A local public library
ID: 3594335 • Letter: J
Question
java biggnars need to comment and output
1. Library Fines
A local public library needs a program to calculate the overdue fines owed by its patrons. The library needs the clerk to enter the patron’s library card number, age of the patron, the number of days overdue for the item and the type of the item using the follow (enter 1, 2 or 3):
1. Book
2. Magazine
3. DVD
Books that are overdue are assessed a fine of 50 cents per day. Magazines are assessed a fine of 25 cents per day. DVDs are assessed a fine of $1.50 a day. There is a maximum fine assessed of $5.00. Senior Citizens (over the age of 70) do not pay fines. Juvenile patrons (ages 6 through 17) pay a maximum fine of $1.00.
Once the fine has been calculated all information about the patron and the overdue material should be printed to the screen. Here is an example of the report that should be output:
Card number 12345
Age 22
Item Type DVD
Number of Days Overdue 2
Overdue Fine $3.00
Fine adjustment reason (e.g, Senior citizen, Juvenile, exceed max)
Each time the program is executed it calculates the fine for a single item. If the user enters invalid data, then a message must be presented on the screen specifying the invalid information and the calculation is aborted.
a) Run your program multiple times using the following information as input. After each run of the program save your entire console output screen to a text file as discussed in our labs.
· 11111 is 75 years old and has an overdue magazine. The magazine is 1 day overdue.
· 22222 is 30 years old and has an overdue magazine. The magazine is 10 days overdue.
· 33333 is 10 years old and has an overdue DVD. The DVD is 2 days overdue.
· 44444 is 50 years old and has an overdue DVD. The DVD is 2 days overdue.
· 55555 is 20 years old and has an overdue book. The book is 60 days overdue.
· 66666 is 15 years old and has an overdue magazine. The magazine is 3 days overdue.
Explanation / Answer
LibraryTest.java
package library;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class LibraryTest {
//Variable to store adjustment reason
static String adjReason;
//Main method
public static void main(String[] args) {
//Scanner to read input from console
Scanner scanner = new Scanner(System.in);
System.out.println("======Enter Patron Details=====");
System.out.println("Enter Patron Library card number: ");
//check if card number is an integer or not
while (!scanner.hasNextInt()) {
scanner.nextLine();
System.out.println("Please Enter Valid card number");
}
int cardNo = scanner.nextInt();
System.out.println("Enter Age of the Patron: ");
//check for valid age
while (!scanner.hasNextInt()) {
scanner.nextLine();
System.out.println("Please Enter Valid Age");
}
int age = scanner.nextInt();
System.out.println("Enter Number of days overdue: ");
while (!scanner.hasNextInt()) {
scanner.nextLine();
System.out.println("Please Enter Valid Number of days");
}
int daysOverdue = scanner.nextInt();
System.out.println("Enter Type of Item from below options: (1) Book (2) Magzine (3) DVD ");
while (!scanner.hasNextInt()) {
scanner.nextLine();
System.out.println("Invalid Entry");
System.out.println("Enter Type of Item from below options: (1) Book (2) Magzine (3) DVD ");
}
String itemName = "";
int item = scanner.nextInt();
//Assign item names based on item number provided
if (item == 1) {
itemName = "Book";
} else if (item == 2) {
itemName = "Magazine";
} else if (item == 3) {
itemName = "DVD";
} else {
System.out.println("Invalid Entry for item number");
System.exit(0);
}
//calculateFine method to calculate fine based on age, item and number of days overdue
double fine = calculateFine(age, item, daysOverdue) / 100;
//store output in StringBuffer
StringBuffer sb = new StringBuffer();
sb.append("Card Number: " + cardNo + " Age: " + age + " Item Type: " + itemName + " Number of Days Overdue: "
+ daysOverdue);
sb.append(" Overdue Fine: $" + fine);
sb.append(" Fine adjustment reason: " + adjReason);
System.out.println(sb.toString());
//writes console output to file
writeToFile(sb);
scanner.close();
}
public static double calculateFine(int age, int item, int daysOverdue) {
double fine = 0;
if (item == 1) {
for (int i = 0; i < daysOverdue; i++) {
fine += 50;
}
} else if (item == 2) {
for (int i = 0; i < daysOverdue; i++) {
fine += 25;
}
} else if (item == 3) {
for (int i = 0; i < daysOverdue; i++) {
fine += 150;
}
}
if (age > 17 && age <= 70 && fine > 500) {
fine = 500;
adjReason = "Exceeded Maximum";
} else if (age > 70) {
fine = 0;
adjReason = "Senior Citizen";
} else if (age > 6 && age < 17 && fine > 100) {
fine = 100;
adjReason = "Juvenile";
} else {
adjReason = "None";
}
return fine;
}
public static void writeToFile(StringBuffer sb) {
try {
//Output file path to which console output should be written
File file = new File("D:/workspace/chegg/src/library.txt");
// Create writer to write to file
BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
writer.write(System.getProperty("line.separator"));
writer.write(sb.toString());
writer.write(System.getProperty("line.separator"));
// Flush the writer after writing contents into the file
writer.flush();
// Close the writer
writer.close();
} catch (
FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sample Console Output--
======Enter Patron Details=====
Enter Patron Library card number:
66666
Enter Age of the Patron:
15
Enter Number of days overdue:
3
Enter Type of Item from below options:
(1) Book
(2) Magzine
(3) DVD
2
Card Number: 66666
Age: 15
Item Type: Magazine
Number of Days Overdue: 3
Overdue Fine: $0.75
Fine adjustment reason: None
Sample file output library.txt
Card Number: 11111
Age: 75
Item Type: Magazine
Number of Days Overdue: 1
Overdue Fine: $0.0
Fine adjustment reason: Senior Citizen
Card Number: 22222
Age: 30
Item Type: Magazine
Number of Days Overdue: 10
Overdue Fine: $2.5
Fine adjustment reason: None
Card Number: 33333
Age: 10
Item Type: DVD
Number of Days Overdue: 2
Overdue Fine: $1.0
Fine adjustment reason: Juvenile
Card Number: 44444
Age: 50
Item Type: DVD
Number of Days Overdue: 2
Overdue Fine: $3.0
Fine adjustment reason: None
Card Number: 55555
Age: 20
Item Type: Book
Number of Days Overdue: 60
Overdue Fine: $5.0
Fine adjustment reason: Exceeded Maximum
Card Number: 66666
Age: 15
Item Type: Magazine
Number of Days Overdue: 3
Overdue Fine: $0.75
Fine adjustment reason: None
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.