CSE 214 – Data Structures Homework 3 – Spring 2022
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
CSE 214 – Data Structures
Homework 3 – Spring 2022
Problem 1: New Library
Imagine you’re a software developer for a new library that needs to digitize its book keeping. Every day, library card holders attempt to check out and turn in their favorite books. This new library has a collection of Books stored in an internal repository we’ll refer to as BookRepository.
BookRepository is composed of 10 endless shelves, one for each digit 0-9, represented using Linked Lists. Books are placed on each shelf depending on the first digit of their unique 13-digit ISBN number (everything else about the book need not be unique). Within each shelf, books are sorted lexicographically/numerically by some criteria including:
• 13-Digit ISBN Number (default at start of your program)
• Name
• Author
• Genre
• Year
• Condition (New, Good, Bad, Replace)
The library also has an external stack of returned books, which we’ll refer to as ReturnStack. Library card holders with a 10-digit userID return their books and submit a ReturnLog of their return into ReturnStack; these books can later be marked as checked in. Keep in mind, that books might be returned late, which means a librarian might want to check them in as soon as possible (and clean out the stack while they’re at it).
Write a program which will be responsible for managing both BookRepository and ReturnStack. For BookRepository, your program will be responsible for the following:
• Adding a new book to BookRepository
• Removing a book from BookRepository
• Checking in a book within BookRepository
• Checking out a book within BookRepository
• Re-arranging specific shelves by some criteria
For ReturnStack, you will be responsible for the following:
• Logging returned books
• Removing the log of the last returned book
• Emptying out the stack when a book is turned in late
By default, all books start as checked in.
YOU CANNOT USE:
• Collections.Sort() or any other built in JAVA sorting classes or methods
• ArrayList
When decompressed, your zip file should contain only your assignment’s .java files in a single folder labeled HW3.
Your program must run on its own and should not crash. Use exception handling as required. If your program does not compile, you will receive a 0 for it. You have to write all the CODE BY YOURSELF. Any form of plagiarism will result in 0 in the homework and possibly even a Q for the course.
NOTE: Although your program may run well on your Integrated Development Environment (IDE) of choice (i.e. Eclipse, Visual Studio, InteliJ, etc.), your program WILL NOT be compiled or graded using any IDE. Instead, your program will be compiled on a standard Unix shell in the same directory as your decompressed HW3 folder using the following 2 commands:
javac *.java
java LibraryManager
Ensure that your program compiles with these two commands into a usable state. If you have any questions contact any of the TAs and/or attend office hours.
Required Classes and More:
The following classes are required for this problem. Each class provides a description and the specifications necessary to complete its implementation. If you feel that additional methods or variables would be useful, feel free to add them during your implementation as you see fit. However, all the variables and methods in the following specifications must be included in your homework. You should declare the data fields of the classes as private and should provide the public getter and setter methods if required. Alongside these required classes, you will have to define enum variables used within these class definitions.
Additionally, “fully documented classes” are classes which have enough reasonable comments and documentation for TAs and the professor to understand your implementation of this assignment. There is no strict criteria, but at the minimum you must include comments that describe crucial steps of computation within your implementation.
1. Date [10 points] Write a fully documented class named Date that contains that contains the day, month, and year of a point in time. Also implement the compare() method, which should allow you to compare two dates with regards to chronological order using Java’s comparison operators.
public Date() constructor and also public Date(…parameters as needed…)
Three int variables:
o day
o month
o year
• public static int compare(x, y) - returns 0 if x = y, -1 if x < y, and 1 if x > y
2. Book (Linked List Node) [10 points] Write a fully documented class named Book that contains parameters of the information about itself. The Book class should contain variables for the book’s name, author, year of publication, genre, condition, and ISBN Number. In addition, it should also contain variables on whether it has been checked out, who checked it out, and its return date. Also implement the toString() method, which should print all of the book’s data in tabular form.
public Book() constructor and also public Book(…parameters as needed…) Three String variables:
o name
o author
o genre
• One (enum) Condition:
o bookCondition
Three int variables:
o ISBN
o yearPublished
o checkOutUserID
One Date variable
o checkOutDate
• One Book variable
o nextBook
• One Boolean
o checkedOut
• public String toString() - returns string of data members in tabular form.
3. Shelf (Linked List) [20 points] Write a fully documented class named Shelf that contains books in a particular shelf of BookRepository. The Shelf class should contain a Book reference to the start of your shelf and its length. Also implement the toString() method, which should print all of the shelve’s data in tabular form.
public Shelf() constructor
Two Book variables
o headBook
o tailBook
One int variable:
o length
• One (enum) SortCriteria variable
o shelfSortCriteria
public void addBook(Book addedBook)
o Brief:
▪ Adds a book to the shelf while maintaining that shelf’s current sorting criteria.
o Parameters:
▪ addedBook – book to be added.
o Preconditions
▪ None.
o Postconditions
▪ The book is added to the list in ascending order.
o Returns
▪ None.
o Throws
▪ BookAlreadyExistsException: A book with the same ISBN already exists
public void removeBook(String removedISBN)
o Brief:
▪ Removes the book with the name bookName from the shelf.
o Parameters:
▪ removedISBN– name of book to remove.
o Preconditions
▪ Shelf is not empty.
o Postconditions
▪ The book with the same name is removed from the shelf.
o Returns
▪ Book removed from the list.
o Throws
▪ BookDoesNotExistException: A book with the same ISBN does not exist
public void sort((enum) SortCriteria sortCriteria)
o Brief:
▪ Sorts books on shelf in lexicographical/numeric order given some sorting criteria in ascending order.
o Parameters:
▪ sortingCriteria– criteria to sort by
o Preconditions
▪ None
o Postconditions
▪ Shelf is sorted using parameter criteria.
o Returns
▪ None.
o Throws
▪ None.
4. BookRepository [20 points] Write a fully documented class named BookRepository that contains all shelves and is able to add, remove, check in, and check out Books. The BookRepository class should have a fixed-length array of 10 shelves.
Public BookRepository() constructor
• One Shelf Array
o shelves[10]
public void checkInBook(int checkedInISBN, int checkInUserID)
o Brief:
▪ Checks in book with the ISBN checkedInISBN from BookRepository.
o Parameters:
▪ int checkedInISBN – The ISBN of the book to be checked in.
▪ int checkInUserID – The ID number of the person checking in this book.
▪ Date checkInDate – The date the book was checked in.
o Preconditions
▪ A book with the same name exists in BookRepository.
o Postconditions
▪ The book with the same name is checked into BookRepository.
o Returns
▪ None.
o Throws
▪ None.
public void checkOutBook(int checkedOutISBN, int checkOutUserID, Date checkOutDate)
o Brief:
▪ Checks out book with the ISBN checkedOutISBN from BookRepository.
o Parameters:
▪ int checkedOutISBN – The ISBN of the book to be checked out.
▪ int checkOutUserID – The ID number of the person checking out this book.
▪ Date checkOutDate – The date the book was checked out.
o Preconditions
▪ A book with the same name exists in BookRepository.
o Postconditions
▪ The book with the same name is checked out from BookRepository.
o Returns
▪ None.
o Throws
▪ InvalidISBNException: Thrown if checked out book’s ISBN is invalid.
▪ InvalidUserIDException: Thrown if the person’s userID is invalid.
▪ BookAlreadyCheckedOutException: Thrown if a book has already been checked out.
public void addBook(int addISBN, String addName, String addAuthor, String addGenre, (enum)
Condition addCondition)
o Brief:
▪ Adds a book to BookRepository on the correct shelf.
o Parameters:
▪ int addISBN – The ISBN of the book to be added.
▪ String addName – The name of the book to be added.
▪ String addAuthor – The author of the book to be added.
▪ String addGenre – The genre of the book to be added.
▪ (enum) Condition addCondition – The condition of the book to be added.
o Preconditions
▪ None.
o Postconditions
▪ The book is added to the correct shelf based off the book’s name’s first letter.
o Returns
▪ None.
o Throws
▪ InvalidISBNException: Thrown if added book’s ISBN is invalid.
▪ BookAlreadyExistsException: Thrown if a book with the same ISBN already exists in BookRepository
public void removeBook(int removeISBN)
o Brief:
▪ Removes a book by name from BookRepository.
o Parameters:
▪ removeISBN – Name of book to be removed.
o Preconditions
▪ Book exists in BookRepository.
o Postconditions
▪ The book is removed from BookRepository.
o Returns
▪ None.
o Throws
▪ InvalidISBNException: Thrown if removed book’s ISBN is invalid.
public void sortShelf(int shelfInd, String sortCriteria)
o Brief:
▪ Sorts a specific shelf by some criteria.
o Parameters:
▪ removeName – Name of book to be removed.
o Preconditions
▪ None.
o Postconditions
▪ Shelf at shelfInd is sorted using sortCriteria’s corresponding enum.
o Returns
▪ None.
o Throws
▪ InvalidSortCriteraException: Thrown if sortCriteria has no corresponding enum.
5. ReturnLog [10 points] Write a fully documented class named ReturnLog that allows us to track which books have been returned to the library.
public ReturnLog() constructor and also public ReturnLog(…parameters as needed…)
• Two int variable:
o ISBN
o userID
• One Date variable:
o returnDate
• One ReturnLog variable:
o nextLog
Write a fully documented class named ReturnStack that allows us to take in returns (push) and check in the last returned book (pop). ReturnStack should implement the toString() method, which should print all of the ReturnStack’s data in tabular form.
Public ReturnStack() constructor
• One ReturnLog variable
o topLog
public bool pushLog(int returnISBN, int returnUserID, Date returnDate)
o Brief:
▪ Pushes a returned book’s ReturnLog into the stack.
o Parameters:
▪ int returnISBN – The ISBN of the book that was returned.
▪ int returnUserID – The ID number of the person returning this book.
▪ Date returnDate – The date the book was returned.
o Preconditions
▪ None.
o Postconditions
▪ A ReturnLog of the returned book is added to the top of ReturnStack
o Returns
▪ Whether the returned book was late or on-time.
o Throws
▪ InvalidISBNException: Thrown if the returned book’s ISBN is invalid.
▪ InvalidReturnDateException: Thrown if the return date is invalid.
▪ BookNotCheckedOutException: Thrown if a book with this ISBN is not checked out.
▪ BookCheckedOutBySomeoneElseException: Thrown if a book was originally checked out by someone else.
▪ InvalidUserIDException: Thrown if userID is invalid.
public ReturnLog popLog()
o Brief:
▪ Pops the last returned book’s ReturnLog from the top of ReturnStack.
o Parameters:
▪ None.
o Preconditions
▪ ReturnStack should be non-empty
o Postconditions
▪ ReturnStack should have had its top log removed.
o Returns
▪ None.
o Throws
▪ EmptyStackException: Thrown if ReturnStack is empty.
7. LibraryManager: [10 points]
Write a fully documented class named LibraryManager. This class will allow the user to interact with the Stack in order to manipulate them.
Must contain your main method
o public static void main (String [] args)
One SecurityCheck variable
o BookRepository
o ReturnStack
This method should implement the following menu options:
▪ (B) – Manage Book Repository
• (C) – Checkout Book
• (N) – Add New Book
• (R) – Remove Book
• (P) – Print Repository
• (S) – Sort Shelf:
o (I) – ISBN Number
o (N) – Name
o (A) – Author
o (G) – Genre
o (Y) – Year
o (C) – Condition
▪ (R) – Manage Return Stack
• (R) – Return Book
• (S) – See Last Return
• (C) – Check In Last Return
• (P) Print Return Stack
▪ (Q) – Quit
Sample Input / Output
// Comment in green, input in red, output in black
Example: Adding a Book
Starting...
Menu:
(B) – Manage Book Repository
(C) – Checkout Book
(N) – Add New Book
(R) – Remove Book
(P) – Print Repository
(S) – Sort Shelf
(I) – ISBN Number
(N) – Name
(A) – Author
(G) – Genre
(Y) – Year
(C) – Condition
(R) – Manage Return Stack
(R) – Return Book
(L) – See Last Return
(C) – Check In Last Return
(P) – Print Return Stack
(Q) – Quit
Please select what to manage: R
Please select an option: A
Please provide an ISBN number: 1234567891011
Please provide a name: How to Tell If Your Cat Is Plotting to Kill You
Please provide an author: Matthew Inman
Please provide a genre: Humor
Please provide a condition: New
Loading...
How to Tell If Your Cat Is Plotting to Kill You has been successfully added to the book repository!
Example: Removing a Book
Menu:
(B) – Manage Book Repository
(C) – Checkout Book
(N) – Add New Book
(R) – Remove Book
(P) – Print Repository
(S) – Sort Shelf
(I) – ISBN Number
(N) – Name
(A) – Author
(G) – Genre
(Y) – Year
(C) – Condition
(R) – Manage Return Stack
(R) – Return Book
(L) – See Last Return
(C) – Check In Last Return
(P) – Print Return Stack
(Q) – Quit
Please select what to manage: R
Please select an option: R
Please provide an ISBN Number: 1234567891011 Loading... How to Tell If Your Cat Is Plotting to Kill You has been successfully removed from the book repository!
Example: Checking Out a Book
Menu:
(B) – Manage Book Repository
(C) – Checkout Book
(N) – Add New Book
(R) – Remove Book
(P) – Print Repository
(S) – Sort Shelf
(I) – ISBN Number
(N) – Name
(A) – Author
(G) – Genre
(Y) – Year
(C) – Condition
(R) – Manage Return Stack
(R) – Return Book
(L) – See Last Return
(C) – Check In Last Return
(P) – Print Return Stack
(Q) – Quit
Please select what to manage: R
Please select an option: C
Please provide a library user ID: 6269239014
Please provide an ISBN Number: 1234567891011
Please provide a return date: 07/28/1998
How to Tell If Your Cat Is Plotting to Kill You has been checked out by 6269239014 and must be returned on 07/28/1998!
2022-04-09