First test: sample task
First, read the task to the end and then start programming!
The 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, without any label - 0.15 euros, green label – 2 euros.
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 print is added into the file on a separate line. Each line contains the data about one print: the description of the print, 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
Magazinewhich extends classPrint; - a class called
Bookwhich 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), print's description (String) and the penalty charge (double).
2. Print class is abstract. It has a constructor which instantiates the print's description (String), the print's label (String), the print's borrower (String) and the number of days the book has already been borrowed for (int).
- The class has an abstract
booleanmethod calledmoreInfowhich informs if the book should be ordered from the repository. - The class has an
intreturn type instance method (without parameters) calleddaysToLendwhich returns the period (the number of days) within which the book must be returned to the library (note the period depends on the label of the print). If the label is something else butgreen,yellow,blue,blank, the method returns value 0. - The class has a
doublereturn type method (without parameters) calledchargePerDaywhich returns the penalty charge for one day associated with the label on the print. - The class has a
voidreturn type methodcalculateChargewhich takes in an argument ofPenaltyChargetype. The method calculates the total penalty charge associated with the print and let thePenaltyChargestore the charge. - The class has
toStringmethod which returns the data about the print in a readable way. The output must also contain the additional information of the print (usemoreInfomethod). - The class has to implement
Comparable<Print>interface. Its methodcompareTohas to compare the prints based on the print's description.
3. Book class is a subclass of Print class. The class has a constructor which instantiates the instance fields, and the following methods:
moreInfomethod which returnstrueif the book has a yellow or a blue label; otherwise, the method returnsfalse.toStringmethod which returns the result of superclass's methodtoStringcomplemented with a note that the print is a book.
4. Magazine class is a subclass of Print class. The class has a constructor which instantiates the instance fields, and the following methods:
moreInfomethod which returnstrueif the magazine was printed in 2000 or earlier; otherwise, the method returnsfalse.toStringmethod which returns the result of superclass's methodtoStringcomplemented with a note that the print is a magazine.
5. ChargeWarning class implements PenaltyCharge interface. The class has a private instance field for the admissible penalty charge (double) and a private list for the debtors (ArrayList<String>).
- The class has a constructor with one
doubletype parameter for the admissible penalty charge. - The class has
getmethod for the list of debtors. - The method
saveChargesaves 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
voidreturn type method (without parameters) calledsendWarningwhich outputs the reader's name and the print's description associated with the largest penalty charge. - The class has a
voidreturn type method calledsaveCharge. The method saves only the data about the largest penalty charge and associated with it reader's name and print's description.
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 in the data from the file.
- Sort the prints using
java.util.Collections.sort(List<T> list)method according to the rules stated incompareTomethod. - Create an instance of
ChargeWarningclass (the admissible penalty charge is 0.2€). - Create an instance of
LargestPenaltyChargeclass. - Apply
calculateChargemethod to each print using the instance ofChargeWarningclass as an argument. After the calculation of the penalty charges, output the names of the debtors. - Apply
calculateChargemethod to each print using the instance ofLargestPenaltyChargeclass as an argument. Output a warning message about the person who has the largest penalty charge.