CTCI: Design the data structures for an online book reader system

The main entities will be –

Each user can have multiple book and each book can be subscribed to multiple user. Library maintains a list of all books.

User –
Each User will have few functions – addMemberShip(), renewMemberShip(), cancelMemberShip(), accessSubscribedBook(), readBook()

Book –
This can actually be split into two parts – One class for creating Book as an entity as it can be either a magazine, audio book, ebook etc. This will allow creation and deletion and addToLibrary() and removeFromLibrary() function.
Book can be an abstract class (which can implement an interface as well ?? ).
Each Book will have few basic functions – bookDetails(), bookSubscribed(), bookDetails()
Other classes that extend it will be Magazine, Ebook, AudioBook, Manual etc.

Library –
Used for searching book mainly.

For maintaining a relationship b/w book and users we will have to maintains the data in DB. like,
book -> user ( bookid, uid, subscribeDate, unsubDate)
user -> book ( bookid, uid, subscribeDate, unsubDate)

We can have a single table whee bookid and uid will be indexed.

In a DS we can use a hash map.

 private HashMap<Book,Set<User>> bookUsers;
 private HashMap<User,Set<Book>> userBooks;

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s