Skip to content

Practical Work 02: Managing a library

Intro


In this exercise, you will create your own Library Manager using what you learn about generics and collections.

Instructions


Creation of the project

Inside your favorite IDE, create a new Java Project.

Warning

The minimum required version of Java is JDK 11

All your classes should be in the isen.java2.library package. You will create sub packages when needed to keep your project nicely organized.

Create a Library class that will represent our library, it will be the main class of our project.

Create an Application class with a main method. You will use this class to test your work throughout the creation of your project. You can start by instantiating a Library object.

items

At the start of all good libraries are items that people wants to borrow. Your library can contain books and movies.

Create an enum class called Genre. It could have the following values: COMEDY, DRAMA, HORROR, SCIENCE_FICTION, THRILLER or any genre you want.

Then, create an abstract class CulturalItem. It will have the following attributes:

  • A String called title
  • A collection of Genre called genres

The title attribute is self-explanatory. The genres attribute should be a Collection implementation of all the genre of the item. Choose the correct type of Collection: we can't have the same genre twice.

Add getters, setters, a fitting constructor and an abstract method print:

1
public abstract void print();

Hint

This can be done easily in Eclipse using the Alt+Shift+S submenu

Create two classes that extend CulturalItems: Book and Movie.

Book class Movie class
Additional attributes author (a String)
  • director (a String),
  • actors (a set of String)
Print method result "Title" written by author "Title" directed by director with actor1, actor2, actor3

Add all constructors, getters and setters necessary. Test your code by creating and printing some books and Movies.

Hint

The Arrays and Collections utility classes can help you keeping your instantiations tidy. See the course for more info.

Storing and printing items

In the Library class, add a list of movies and a list of books. In the constructor of the class, instantiate both list.

Create the following methods:

Signature Description
public void add(Movie movie) Add a movie in the library
public void add(Book book) Add a book in the library
public void printCatalogue() Print all books and movies of the library in the standard output. Both lists should be sorted by title.
public void printByGenre(Genre genre) Similar to the printCatalogue method but print only items with the correct genre.

You should try to factorize your code. For instance, a private method that prints all the content of a list of items will be useful.

Hint

Generics will come handy when you want to factorize your code.

Hint

Comparing stuff can be done in at least two ways. Choose the most efficient one. Why not enhance the class CulturalItem...

Test your methods by adding books and movies to your library and printing its content.

Borrowing items

You will now add the functionality to borrow items from the library. Start by adding the following attributes to the CulturalItem class with their associated getter method:

  • A boolean called borrowed
  • A String called borrower

Create the following methods:

Signature Description
public void borrow(String borrower) Set the borrower and mark the item as borrowed
public void returnBack() Untag the item as borrowed and delete the borrower

Change the print methods to show if the item is borrowed (B) or available (A).

Now in the Library class, add the following methods:

Signature
public Movie borrowMovie(String movieTitle, String borrower) throws ItemNotFoundException, ItemAlreadyBorrowedException
public Movie borrowMovie(String movieTitle, String borrower) throws ItemNotFoundException, ItemAlreadyBorrowedException
public Book borrowBook(String bookTitle, String borrower) throws ItemNotFoundException, ItemAlreadyBorrowedException
public void printBorrowedItems(String borrower)

You will need to create the 2 exceptions.

These two borrow methods have the same implementation:

  • Browse the library for the appropriate items : same type (book or movie) and title
  • If there is no item, throw the ItemNotFoundException
  • If there are one or more items:
    • If all are borrowed : throw the ItemAlreadyBorrowedException
    • Or else borrow one of the item and returns it

Ratings

The last functionality of your library is to add ratings to items. Add a ratings attribute in the CulturalItem class that is a Map with a person name (String) as key and a rating (Integer) as value. Add a getter method.

Instantiate the Map in the constructor and add the following methods to the CulturalItem class:

Signature Description
public void addRating(String person, Integer rating) Adds a rating to the item
public Double getAverageRating() Returns the average rating

Change the print() methods to add the average rating of the item.

In the Library class, add the following methods:

Signature
public void rateMovie(String movieTitle, String person, Integer rating) throws ItemNotFoundException
public void rateBook(String bookTitle, String person, Integer rating) throws ItemNotFoundException
public void printTopTenItems()

The printTopTenItems method prints in the standard output the items with the best average rating regardless of the type (book or movie).