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 on 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; Joosep Kask; 125 Guardians/2013,10; yellow; Teele Tamm; 59 Oskar Luts, Kevade; blue; Teele Tamm; 39 Artur Jürisson, Eesti-inglise õigussõnaraamat; blank; Jaan Aruhein; 224 M. Lutz, Learning Python; green; Joosep Kask; 22 Wired/2010,15; blank; Joosep Kask; 4
Each line of the file contains the data about one print: the description of a 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 (do not worry how this number is updated each day). These data is delimited by a semicolon and a space. Note that the description of a book contains information about the author and the title separated by a comma and a space. The description of a magazine contains the title, a slash, the publication year, a comma, and the issue. It can be assumed that if the description of a 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 files for:
- an abstract class called
Print
; - an interface called
PenaltyCharge
; - a class called
Magazine
which extends classPrint
; - a class called
Book
which extends 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
), a description of a print (String
) and a penalty charge (double
).
2. Print
class is abstract. It has a constructor which initializes the description of a print (String
), the label of a print (String
), the borrower of a 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 a print). If the label is something else butgreen
,yellow
,blue
,blank
, the method returns 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 a 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 a print and let thePenaltyCharge
store the charge (use methodsaveCharge
). - The class has
toString
method which returns the data about a print in a readable way. The output must also contain some additional information of a print (usemoreInfo
method). - The class has to implement
Comparable<Print>
interface. Its methodcompareTo
has to compare the prints based on the description of a print.
3. Book
class is a subclass of 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 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 PenaltyCharge
interface. The class has a private instance field for an 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 admissible level and reader's name is not in the debtor's list yet.
6. LargestPenaltyCharge
class implements PenaltyCharge
interface. The class has three private instance fields: the largest penalty charge (double
), the reader (String
) and the description of the print (String
) associated with the largest penalty charge.
- 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 largest penalty charge. - The class has a
void
return type method calledsaveCharge
. The method saves only the data about the largest penalty charge, the reader's name and his/her borrowed book (the description of a book with the largest penalty charge).
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 at the end of the method signature the following note: throws Exception
. 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
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
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.