In: Computer Science
In java
Implement the class Book. It has the following instance
variables: name, subject, year, maximumLoanPeriod, and loanPeoriod.
The following methods should be included:
• Constructor(s), Accessors and Mutators as needed.
• public double computeFine() => calculates the fine due on this
item
The fine is calculated as follows:
• If the loanPeriod <= maximumLoanPeriod, there is no fine on
the book.
• If loanPeriod > maximumLoanPeriod
o If the subject of the book is "CS" the fine is 10.00 per
day.
o If the subject of the book is any other subject area, the fine is
SR 5.00
per day.
o If the year of the book is < 2015, there is a 20% reduction in
the fine. o If the year of the book is > 2015, the fine is
increased by 20%
• toString() method that show description of the object.
Read the information of the attached file books.txt. The first line
is the number of books and followed be a book on each line. The
book details is separated by “-” Save the file content into array
then Print the following.
• the number of books whose subject area is "CS".
• the total fine on all books
• the book with the highest fine.
• the total number of books with no fine.
Notes:
- Use the given books.txt.
——-——————————-
the text is
4
Introduction to Programming - CS - 2016 - 5 - 10
Introduction to Databases - CS - 2012 - 6 - 3
Thermodynamics - Physics - 1999 - 10 - 15
Linear Algebra - Math - 2015 - 12 - 10
// Book.java : Java program to create and implement the class Book
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Book {
// data members
private String name;
private String subject;
private int year;
private int maximumLoanPeriod;
private int loanPeriod;
// default constructor
public Book()
{
this("","",0,0,0);
}
// parameterized constructor
public Book(String name, String subject, int year, int loanPeriod, int maximumLoanPeriod)
{
this.name = name;
this.subject = subject;
this.year = year;
this.maximumLoanPeriod = maximumLoanPeriod;
this.loanPeriod = loanPeriod;
}
// setters
public void setName(String name)
{
this.name = name;
}
public void setSubject(String subject)
{
this.subject = subject;
}
public void setYear(int year)
{
this.year = year;
}
public void setMaximumLoanPeriod(int maximumLoanPeriod)
{
this.maximumLoanPeriod = maximumLoanPeriod;
}
public void setLoanPeriod(int loanPeriod)
{
this.loanPeriod = loanPeriod;
}
// getters
public String getName()
{
return name;
}
public String getSubject()
{
return subject;
}
public int getYear()
{
return year;
}
public int getMaximumLoanPeriod()
{
return maximumLoanPeriod;
}
public int getLoanPeriod()
{
return loanPeriod;
}
// method to compute and return the fine
public double computeFine()
{
double fine = 0;
if(loanPeriod > maximumLoanPeriod)
{
if(subject.equalsIgnoreCase("CS"))
fine = (loanPeriod-maximumLoanPeriod)*10;
else
fine = (loanPeriod-maximumLoanPeriod)*5;
if(year < 2015)
{
fine -= 0.2*fine;
}else if(year > 2015)
{
fine += 0.2*fine;
}
}
return fine;
}
// toString method
public String toString()
{
return("Name : "+name+" Subject : "+subject+" Year : "+year+" Loan Period : "+loanPeriod+" Maximum LoanPeriod : "+maximumLoanPeriod);
}
public static void main(String[] args) throws FileNotFoundException {
Scanner fileScan = new Scanner(new File("books.txt")); // open the file, provide full path to file
int numBooks;
Book books[];
String line;
numBooks = fileScan.nextInt(); // read the number of books
fileScan.nextLine(); // ignore the '\n' at the end
books = new Book[numBooks];
int i=0;
// loop to read till the end of file
while(fileScan.hasNextLine())
{
line = fileScan.nextLine(); // read the line
// The format of the record : name-subject-year-loanPeriod-maximumLoanPeriod
String fields[] = line.split("-");// split the read line using "-" as the delimiter
// store the book read in the array
books[i] = new Book(fields[0],fields[1],Integer.parseInt(fields[2]),Integer.parseInt(fields[3]),Integer.parseInt(fields[4]));
i++;
}
fileScan.close(); // close the file
int csBooks = 0;
double totalFine = 0;
int highestFineBook = -1;
double highestFine = 0;
int noFineBooks = 0;
// loop to calculate the statistics
for(i=0;i<numBooks;i++)
{
if(books[i].getSubject().equalsIgnoreCase("CS"))
csBooks++;
double fine = books[i].computeFine();
if(fine == 0)
noFineBooks++;
else
{
if(fine > highestFine)
{
highestFine = fine;
highestFineBook = i;
}
}
totalFine += fine;
}
// output the statistics
System.out.println("The number of books whose subject area is CS : "+csBooks);
System.out.printf("Total fine on all books : SR %.2f",totalFine);
if(highestFineBook != -1)
{
System.out.printf("\nBook with the highest fine of SR : %.2f\n",highestFine);
System.out.println(books[highestFineBook]);
}else
System.out.println("No books with fine");
System.out.println("Total number of books with no fine : "+noFineBooks);
}
}
//end of program
Output:
Input file:
Output: