using java. import java.util.Scanner; Use If else statments. Do not use objects.
ID: 3706595 • Letter: U
Question
using java. import java.util.Scanner;
Use If else statments.
Do not use objects.
Do not use;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
********
use good comments. Also, include output from console.
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
package January;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
// Class FineCalculation definition
public class FineCalculation
{
// Instance variables to store data
int cardNumber;
int age;
String itemType;
int numberOfDaysOverdue;
double overdueFine;
String fineAdjustmentReason;
// Parameterized constructor to assign parameter data to instance variables
FineCalculation(int cardNumber, int age, String itemType, int numberOfDaysOverdue)
{
this.cardNumber = cardNumber;
this.age = age;
this.itemType = itemType;
this.numberOfDaysOverdue = numberOfDaysOverdue;
}// End of parameterized constructor
// Method to calculate file
void calculateFine()
{
// Checks if the item type is Book
if(itemType.equalsIgnoreCase("Book"))
{
// Checks if the age is greater than 70 for Senior Citizens
if(age > 70)
{
// Fine is zero
overdueFine = 0;
// Assigns the reason
fineAdjustmentReason = "Senior Citizens";
}// End of if condition
// Otherwise checks if the age is grater than equals to 6 and less than or equals to 17
// for Juvenile patrons
else if(age >= 6 && age <= 17)
{
// Fine is 1
overdueFine = 1;
// Assigns the reason
fineAdjustmentReason = "Juvenile patrons";
}// End of else if condition
// Otherwise
else
{
// Calculate the file
overdueFine = numberOfDaysOverdue * 0.5;
// Assigns the reason
fineAdjustmentReason = "No adjustment";
}// End of else
}// End of outer if condition
// Otherwise, checks if the item type is DVD
else if(itemType.equalsIgnoreCase("DVD"))
{
// Checks if the age is greater than 70 for Senior Citizens
if(age > 70)
{
// Fine is zero
overdueFine = 0;
// Assigns the reason
fineAdjustmentReason = "Senior Citizens";
}// End of if condition
// Otherwise checks if the age is grater than equals to 6 and less than or equals to 17
// for Juvenile patrons
else if(age >= 6 && age <= 17)
{
// Fine is one
overdueFine = 1;
// Assigns the reason
fineAdjustmentReason = "Juvenile patrons";
}// End of else if condition
// Otherwise
else
{
overdueFine = numberOfDaysOverdue * 1.5;
// Assigns the reason
fineAdjustmentReason = "No adjustment";
}// End of else
}// End of else if condition
// Otherwise, item type is Magazine
else
{
// Checks if the age is greater than 70 for Senior Citizens
if(age > 70)
{
// Fine is zero
overdueFine = 0;
// Assigns the reason
fineAdjustmentReason = "Senior Citizens";
}// End of if condition
// Otherwise checks if the age is grater than equals to 6 and less than or equals to 17
// for Juvenile patrons
else if(age >= 6 && age <= 17)
{
// Fine is one
overdueFine = 1;
// Assigns the reason
fineAdjustmentReason = "Juvenile patrons";
}// End of else if condition
// Otherwise
else
{
// Calculate the file
overdueFine = numberOfDaysOverdue * 0.25;
// Assigns the reason
fineAdjustmentReason = "No adjustment";
}// End of else
}// End of outer else
// Checks if the fine is greater than 5
if(overdueFine > 5)
{
// Re adjust the fine to 5
overdueFine = 5;
// Assigns the reason
fineAdjustmentReason = "maximum fine assessed of $5.00";
}// End of if condition
}// End of method
// Method to display information
void displayInformation()
{
System.out.printf(" Card number: %16s", cardNumber);
System.out.printf(" Age: %21d", age);
System.out.printf(" Item Type: %21s", itemType);
System.out.printf(" Number of Days Overdue: %d", numberOfDaysOverdue);
System.out.printf(" Overdue Fine: %14.2f", overdueFine);
System.out.printf(" Fine adjustment reason: %3s", fineAdjustmentReason);
}// End of method
// main method definition
public static void main(String[] args)
{
// Creates arrays to store item information
int id[] = {11111, 22222, 33333, 44444, 55555, 66666};
int age[] = {75, 30, 10, 50, 20, 15};
String type[] = {"magazine", "magazine", "DVD", "DVD", "book", "magazine"};
int due[] = {1, 10, 2, 2, 60, 3};
// Declares BufferedWriter object
BufferedWriter bw = null;
// try block begins
try
{
// Opens the file
File file = new File("FineReport.txt");
// The file gets created if it is not present at the specified location
if (!file.exists())
file.createNewFile();
// File writer object instantiated with file object
FileWriter fw = new FileWriter(file);
// Buffered writer object instantiated with file writer object
bw = new BufferedWriter(fw);
// Loops 6 times
for(int x = 0; x < 6; x++)
{
// Creates an object of FineCalculation class with the help of parameterized constructor
FineCalculation f = new FineCalculation(id[x], age[x], type[x], due[x]);
// Calls the method to calculate file
f.calculateFine();
// Calls the method to display information
f.displayInformation();
// Writes data to file
// String.valueOf to convert the data to string
bw.write(String.valueOf(f.cardNumber));
// To write new line
bw.newLine();
bw.write(String.valueOf(f.age));
bw.newLine();
bw.write(f.itemType);
bw.newLine();
bw.write(String.valueOf(f.numberOfDaysOverdue));
bw.newLine();
bw.write(String.valueOf(f.overdueFine));
bw.newLine();
bw.write(f.fineAdjustmentReason);
bw.newLine();
}// End of for loop
// Success message
System.out.println(" File written Successfully");
} // End of try block
// Catch to handle IOException
catch (IOException ioe)
{
ioe.printStackTrace();
}// End of catch
// finally block
finally
{
try
{
// Checks if BufferedWriter is not null then close
if(bw != null)
bw.close();
}// End of try block
// Catch block to handle exception
catch(Exception ex)
{
System.out.println(" Error in closing the BufferedWriter"+ex);
}// End of catch block
}// End of finally block
}// End of main method
}// End of class
Sample Output:
Card number: 11111
Age: 75
Item Type: magazine
Number of Days Overdue: 1
Overdue Fine: 0.00
Fine adjustment reason: Senior Citizens
Card number: 22222
Age: 30
Item Type: magazine
Number of Days Overdue: 10
Overdue Fine: 2.50
Fine adjustment reason: No adjustment
Card number: 33333
Age: 10
Item Type: DVD
Number of Days Overdue: 2
Overdue Fine: 1.00
Fine adjustment reason: Juvenile patrons
Card number: 44444
Age: 50
Item Type: DVD
Number of Days Overdue: 2
Overdue Fine: 3.00
Fine adjustment reason: No adjustment
Card number: 55555
Age: 20
Item Type: book
Number of Days Overdue: 60
Overdue Fine: 5.00
Fine adjustment reason: maximum fine assessed of $5.00
Card number: 66666
Age: 15
Item Type: magazine
Number of Days Overdue: 3
Overdue Fine: 1.00
Fine adjustment reason: Juvenile patrons
File written Successfully
File FineReport.txt file contents
11111
75
magazine
1
0.0
Senior Citizens
22222
30
magazine
10
2.5
No adjustment
33333
10
DVD
2
1.0
Juvenile patrons
44444
50
DVD
2
3.0
No adjustment
55555
20
book
60
5.0
maximum fine assessed of $5.00
66666
15
magazine
3
1.0
Juvenile patrons
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.