Design data structures for an online book reader system ~ KodeKnight
Design the data structures for an online book reader system.
Managing books :
managing users :
Now we need the 1:1 mapping between book and user, which is managed by Book User, and online reader finally manages everything :
Unfortunately, BookUser will change when we take into account db relational model, as we dont have to keep things in-memory.
https://xmruibi.gitbooks.io/interview-preparation-notes/content/OOD/DesignExamples/OnlineReader.html
Read full article from Design data structures for an online book reader system ~ KodeKnight
Design the data structures for an online book reader system.
public
class
Book {
private
long
ID;
private
String details;
public
long
getID() {
return
ID;
}
public
Book(
long
id, String details) {
}
}
public
class
User {
private
long
ID;
private
String details;
private
int
accountType;
public
long
getID() {
return
ID;
}
public
Book searchLibrary(
long
id) {
return
Book.find(id);
}
public
void
renewMembership() {
}
}
Managing books :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| public class Books{ private Set<Book> books; public void addBook( long iD, String details) { books.add( new Book(iD, details)); } public void update() { } public void delete(Book b) { books.remove(b); } public Book find( long id) { for (Book b : books) if (b.getID() == id) return b; return null ; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| public class Users { private Set<User> users; public Book searchLibrary( long id) { return Book.find(id); } public void renewMembership() { } public static User find( long ID) { for (User u : users) { if (u.getID() == ID) return u; } return null ; } public void addUser( long ID, String details, int accountType) { users.add( new User(ID, details, accountType)); } public User( long iD, String details, int accountType) { } } |
Now we need the 1:1 mapping between book and user, which is managed by Book User, and online reader finally manages everything :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
| public class BookUsers { private HashMap<Book,Set<User>> bookUsers; private HashMap<User,Set<Book>> userBooks; private void addUserToBookUserList(Book b, User u){ if (bookUsers.containsKey(b)){ Set<User> users = bookUsers.getValue(b); users.add(u); } else { bookUsers.add(b, new HashMap<User> (){u}); } } private void addBookToUserBookList(Book b, User u){ if (userBooks.containsKey(u)){ Set<Book> books = userBooks.getValue(u); books.add(b); } else { userBooks.add(u, new HashMap<Book> (){b}); } } public void assignBookToUser(Book b, User u) { addUserToBookUserList(b,u); addBookToUserBookList(b,u); } } public class OnlineReaderSystem { private Books books; private Users users; private BookUsers bookUsers; private HashMap<Book,Set<User>> bookUsers; private HashMap<User,Set<Book>> userBooks; public void assignBookToUser() { bookUsers.assignBookToUser(b,u); } public OnlineReaderSystem(Books books, Users users) { } public void listenRequest() { } public Book searchBook( long ID) { return books.find(ID); } public User searchUser( long ID) { return users.find(ID); } public void display() { } } |
Unfortunately, BookUser will change when we take into account db relational model, as we dont have to keep things in-memory.
https://xmruibi.gitbooks.io/interview-preparation-notes/content/OOD/DesignExamples/OnlineReader.html
This question comes from the book named "Cracking Code Interview", Chapter 7; It is very easy problem with thinking about the insert/remove/update/retrieve action.
- Functionality
- User Membership Creation and Extension
- Search the book in memory
- Reading the book
- Book:
- ID
- Title
- Author
- Content
- Method
- showContent
- Books: (In-memory storage for many book objects)
- Set
- Method
- find
- add
- delete
- update
- Set
- User
- ID
- Name
- accoutnType
- Method
- findBook
- read (book.showContent)
- Users
- Set
- Method
- find
- add
- delete
- update
- Set