Class Diagram: see image.
The Publication class implements the "Serializable" interface so that its object can be stored. (The Serializable interface is defined in the "java.io" package.)
The Book class implements the "Serializable" interface so that its object can be stored. (The Serializable interface is defined in the "java.io" package.) It needs to define the following method:
public void copy (Book other)
It needs to copy every member variable value from "other" parameter to their corresponding variable of the object itself using "this" reference.
The BookComparator class implements the "Comparator" interface (The Comparator interface is in "java.util" package.). It needs to define the following method that was an inherited abstract method from Comparator interface:
public int compare(Object first, Object second)
(Note that you can also define: public int compare(Book first, Book second) instead by making the class implements Comparator< Book>.
If the first argument object has an author lexicographically less than that of the second argument, an int less than zero is returned. If the first argument object has an author lexicographically larger than that of the second argument, an int greater than zero is returned. If their authors are same, then their titles should be compared. (if the first argument object has a title lexicographically less than that of the second argument, an int less than zero is returned. If the first argument object has a title lexicographically larger than that of the second argument, an int greater than zero is returned). If their authors and titles are same, then their version should be compared (if the first argument object has a version smaller than that of the second argument, an int less than zero is returned. If the first argument object has a version larger than that of the second argument, an int greater than zero is returned). If they have same authors, titles and versions, then 0 should be returned.
The Sorts class is a utility class that will be used to sort a list of Book objects. Sorting algorithms are described in the Lecture Slides tab of the Blackboard. These algorithms sort numbers stored in an array. It is your job to modify it to sort an array of objects.
The Sorts class object will never be instantiated. It must have the following methods:
public static void sort(Book[] bookList, int size, Comparator< Book>)
Your sort method utilizes the compare method of the parameter Comparator object to sort. You can use one of Selection sort or Insertion Sort. The parameter size specifies how many first elements should be sorted. Note that not all elements in the array should be sorted, since some of them will be null pointers.
The BookManagement class has a list of Book objects that can be organized at the book management system. The book management system will be a fully encapsulated object. The BookManagement class implements the Serializable interface.
It has the following attributes:
Attribute name, Attribute type, Description
bookList, an array of Book objects, A list of Book objects in the book management system
currentBooksCount, int, the number of Book objects created and stored in the bookList
maxSize, int, the maximum number of Book objects that can be stored in the bookList array. It is also the size of the bookList array.
The following public methods should be provided to interact with the book management system:
BookManagement(int maximumsize)
A Constructor of the BookManagement class. Using the parameter value, it should initialize the member variable maxSize. Then it should instantiate an alTay of Book objects using the maxSize, and initialize each slot of the array to null for every index. It should also initialize the member variable currentBooksCount to 0.
int bookExists(String author, String title, int version)
Search for a Book object by author and title, and return the index of the book object if found. Return -1 if not found. The parameters are author and title of a Book object.
boolean addB00k(String author, String title, int version, String ISBN, double price)
Add a Book object to the book list and return true if such Object was added successfully. Return false if an Object with the same author, title, version, ISBN and price already exists or currentBooksCount is already same as maxSize, i.e., the array is full (the new object is not added).
boolean removeBook(String author, String title, int version)
Remove a Book object from the book list. Return true if the object was removed successfully. Return false if the object with the given author, title and version does not exist. Sort the list of Book objects by their information including its author, title and version. This method calls the sort method defined in the Sorts class, using an object of BookComparator class as its second parameter. You should also make a use of copy() method of Book class.
void sortBooks()
Sort the list of Book objects by their information including its author, title and version. This method calls the sort method defined in the Sorts class, using an object of BookComparator class as its second parameter. You should also make a use of copy() method of Book class.
String listBooks()
List all Book objects in the book list. The returned string is the concatenation of each Book object information in the list for the number of currentBooksCount (do not use the size of the array here) Hint: you can utilize Book's toString method to help you complete this method. If there is no object in the list, This method should return the string containing "nno booknn".
void closeBookManagement()
Closes the book management system by making the list empty. This can be done by making every slot of the bookList array to null, and also by setting currentBooksCount to be 0.
No input/output should occur in the book management system. User interaction should be handled only by the driver class.
You may add other methods to the class in order to make your life easier.
All input and output should be handled in Assignment8 class. The main method should start by displaying this updated menu in this exact format:
ChoicettActionn
------tt------n
AttAdd Bookn
CttCreate BookManagementn
DttSearch Bookn
LttList Booksn
OttSort Booksn
QttQuitn
RttRemove Book n
TttClose BookManagementn
UttWrite Text to Filen
VttRead Text from Filen
WttSerialize BookManagement to Filen
XttDeserialize BookManagement from Filen
?ttDisplay Helpnn
Next, the following prompt should be displayed:
What action would you like to perform?n
Read in the user input and execute the appropriate command. After the execution of each command, redisplay the prompt. Commands should be accepted in both lowercase and uppercase. The following commands are modified or new.
Add Book
This part is already implemented in Assignment8.java file. Please see the case A in the main of Assignment8.java.
Create BookManagement
This part is already implemented in Assignment8.java file. Please see the case C in the main of Assignment8.java.
Search by Book Title
This part is already implemented in Assignment8.java file. Please see the case D in the main of Assignment8.java.
Sort Books
This part is already implemented in Assignment8.java file. Please see the case O in the main of Assignment8.java.
Remove Book
This part is already implemented in Assignment8.java file. Please see the case R in the main of Assignment8.java.
List Books
Each Book object information in the book list should be displayed using the toString method provided in the Book class. (and use listBooks( ) method in the BookManagement class.) This part is already implemented in Assignment8.java file.
Please see the case L in the main of Assignment8.java.
Close BookManagement
Delete all Book objects. Then, display the following:
book management system closedn
This part is already implemented in Assignment8.java file. Please see the case R in the main of Assignment8.java
Write Text to File
Your program should display the following prompt:
Please enter a file name to write:n
Read in the filename and create an appropriate object to get ready to read from the file. Then it should display the following prompts:
Please enter a string to write in the file:n
Read in the string that a user types, say "input", then attach "n" at the end of the string, and write it to the file. (i.e. input+"n" string will be written in the file.)
If the operation is successful, display the following:
FILENAME was writtenn
Replace FILENAME with the actual name of the file.
Use try and catch statements to catch IOException. The file should be closed in a finally statement.
Read Text from File
Your program should display the following prompt:
Please enter a file name to read:n
Read in the file name create appropriate objects to get ready to read from the file. If the operation is successful, display the following (replace FILENAME with the actual name of the file):
FILENAME was readn
Then read only the first line in the file, and display:
The first line of the file is:n
CONTENTn
where CONTENT should be replaced by the actual first line in the file.
Your program should catch the exceptions if there are. (Use try and catch statement to catch, FileNotFoundException, and the rest of IOException.)
If the file name cannot be found, display
FILENAME was not foundn
where FILENAME is replaced by the actual file name.
Serialize BookManagement to File
Your program should display the following prompt:
Please enter a file name to write:n
Read in the filename and write the serialized BookManagement object out to it. Note that any objects to be stored must implement Serializable interface. The Serializable interface is defined in java.io.* package. If the operation is successful, display the following:
FILENAME was writtenn
Replace FILENAME with the actual name of the file.
Use try and catch statements to catch NotSerializableExeption and IOException.
Deserialize BookManagement from File
Your program should display the following prompt:
Please enter a file name to read:n
Read in the file name and attempt to load the BookManagement object from that file. Note that there is only one BookManagement object in the Assignment8 class, and the first object read from the file should be assigned to the BookManagement object. If the operation is successful, display the following (replace FILENAME with the actual name of the file):
*FILENAME was readn
Your program should catch the exceptions if there are.
(Use try and catch statement to catch ClassNotFoundException, FileNotFoundException, and the rest of IOException.)