First test: sample task
First, read the task to the end and then start programming!
A library lends out different prints (books and magazines) for different periods. The period depends on the label attached to the print: green label - 24h (1 day), without any label (blank label) - 14 days, yellow label - 30 days and blue label - 60 days. If the print is not returned on time, the reader has to pay a penalty. The penalty for one delayed day is imposed according to the label on the print: yellow or blue label - 0.05 euros per day, without any label - 0.15 euros per day, green label – 2 euros per day.
Usually readers return books on time, but sometimes there are delays. Write a program which helps detect the readers with large penalty charges and prints out the reader with the largest penalty charge. The program should send a warning message to such readers.
The data about the borrowed books and their readers are given in the file in the following format:
H. Schildt, Java: The Complete Reference; yellow; Josef Marr; 125 Guardians/2013,10; yellow; Adele Tak; 59 Oskar Luts, Kevade; blue; Adele Tak; 39 Alastair Thomas, Historical Dictionary of Denmark; blank; Jane Arnold; 224 M. Lutz, Learning Python; green; Josef Marr; 22 Wired/2010,15; blank; Josef Marr; 4
Each line of the file contains the data about one print: the description of the print (title and/or author etc.), the label, the reader and the number of days the borrowed book has already been in the reader's pocket (ths number is updated each day by the library). The elements in one line are delimited by semicolons and a spaces. Note that if the description of a book contains information about the author and the title, they separated by a comma and a space. The description of a magazine contains the title, a slash, publication year, a comma, and issue. It can be assumed that if the description of the print does not contain a slash, it is a book.
Write a program which meets the following requirements (even if you do not like them or the requirements seem to be weird). The program must have java file(s) for:
- an interface called
PenaltyCharge
; - an abstract class called
Print
; - a class called
Book
which extends the classPrint
; - a class called
Magazine
which extends the classPrint
; - a class called
ChargeWarning
; - a class called
LargestPenaltyCharge
; - a test class.
1. PenaltyCharge
is an interface which has a void
return type method called saveCharge
. The method takes in the following parameters: reader's name (String
), description of the print (String
) and penalty charge (double
).
2. Print
class is abstract. It has a constructor which initializes the description of the print (String
), the label of the print (String
), the borrower of the print (String
) and the number of days the book has already been borrowed for (int
).
- The class has an abstract
boolean
method calledmoreInfo
which informs if the book should be ordered from the repository. - The class has an
int
return type instance method (without parameters) calleddaysToLend
which returns the period (the number of days) within which the book must be returned to the library (note that the period depends on the label of the print). If the label is something else than "green", "yellow", "blue", "blank", then the method returns the value 0. - The class has a
double
return type method (without parameters) calledchargePerDay
which returns the penalty charge for one day associated with the label on the print. - The class has a
void
return type methodcalculateCharge
which takes in an argument ofPenaltyCharge
type. The method calculates the total penalty charge associated with the print and lets thePenaltyCharge
store the charge (use the methodsaveCharge
). - The class has
toString
method which returns the data about the print in a readable way. The output must also contain some additional information of the print (use themoreInfo
method). - The class has to implement the
Comparable<Print>
interface. Its methodcompareTo
has to compare the prints based on the description of a print.
3. Book
class is a subclass of the Print
class. The class has a constructor which initializes the instance fields, and the following methods:
moreInfo
method which returnstrue
if the book has a yellow or a blue label; otherwise, the method returnsfalse
.toString
method which returns the result of superclass's methodtoString
complemented with a note that the print is a book.
4. Magazine
class is a subclass of the Print
class. The class has a constructor which initializes the instance fields, and the following methods:
moreInfo
method which returnstrue
if the magazine was printed in 2000 or earlier; otherwise, the method returnsfalse
.toString
method which returns the result of superclass's methodtoString
complemented with a note that the print is a magazine.
5. ChargeWarning
class implements the PenaltyCharge
interface. The class has a private instance field for the maximum admissible penalty charge (double
) and a private list of debtors (ArrayList<String>
).
- The class has a constructor with one
double
type parameter for the admissible penalty charge. - The class has
get
method for the list of debtors. - The method
saveCharge
saves the reader's name in the list if the amount of the penalty charge (per book) exceeds the maximum admissible level and reader's name is not in the debtor's list yet.
6. LargestPenaltyCharge
class implements the PenaltyCharge
interface. The class is intended to store the data about the largest penalty charge. It has three private instance fields: penalty charge (double
), reader (String
) and description of the print (String
).
- The class has a
void
return type method (without parameters) calledsendWarning
which outputs the reader's name and the description of the print associated with the penalty charge. - The class has a
void
return type method calledsaveCharge
. The method saves the data about the penalty charge, the reader's name and his/her borrowed book (the description of a book).
7. The main class should have a public static List<Print>
return type method called readPrintsIn
. The method takes in a file name as an argument and returns the data from the file. The method may throw out an exception; therefore, add the following note: throws Exception
at the end of the method signature. The number of lines in the file is not known. The program should not crash if the file is empty.
The main class should perform the following operations:
- use the static method to read the data from the file;
- sort the prints using
java.util.Collections.sort(List<T> list)
method according to the rules stated incompareTo
method; - create an instance of
ChargeWarning
class (the admissible penalty charge is 0.2€); - create an instance of
LargestPenaltyCharge
class; - apply the
calculateCharge
method to each print using the instance ofChargeWarning
class as an argument; after the calculation of the penalty charges, output the names of debtors; - apply the
calculateCharge
method to each print using the instance ofLargestPenaltyCharge
class as an argument; output a warning message about the person who has the largest penalty charge.