Design the data structures for an online book reader system.
My initial thoughts:
I design this book reader system based on iBooks from Apple: you can search for books, add bookmarks and so on.
public class MyBook { private final String author; private final String title; private final int number_pages; private final List<String> content; private final List<Integer> chapterIndex; private List<Integer> bookmarks; public MyBook(String author, String title, int number_pages, ArrayList<String> content, List<Integer> chapterIndex) { super(); this.author = author; this.title = title; this.number_pages = number_pages; this.content = content; this.chapterIndex = chapterIndex; } public void addBookMark(int pageNumber) { bookmarks.add(pageNumber); } public List<Integer> getBookmarks() { return bookmarks; } public String getPage(int pageNumber) { return content.get(pageNumber); } } public class MyOnlineReader { private Map<String, Map<String, MyBook>> bookMap; private MyBook currentBook; private int currentPage; public void openBook(String author, String title) { currentBook = bookMap.get(author).get(title); List<Integer> bookmarks = currentBook.getBookmarks(); if (bookmarks.isEmpty()) bookmarks.add(0); currentPage = bookmarks.get(bookmarks.size() - 1); System.out.println(currentBook.getPage(currentPage)); } private boolean finished() { return currentPage == currentBook.number_pages - 1; } public void addBookMark() { if (!currentBook.getBookmarks().contains(currentPage)) currentBook.addBookMark(currentPage); } public void displayAllBookMark() { System.out.println(currentBook.getBookmarks()); } public void nextPage() { List<Integer> bookmarks = currentBook.getBookmarks(); bookmarks.remove(currentPage); currentPage = finished() ? currentPage : currentPage + 1; bookmarks.add(currentPage); System.out.println(currentBook.getPage(currentPage)); } public void previousPage() { List<Integer> bookmarks = currentBook.getBookmarks(); bookmarks.remove(currentPage); currentPage = currentPage == 0 ? currentPage : currentPage - 1; bookmarks.add(currentPage); System.out.println(currentBook.getPage(currentPage)); } public void goToPage(int pageNumber) { if (pageNumber < 0 || pageNumber >= currentBook.number_pages) return; List<Integer> bookmarks = currentBook.getBookmarks(); bookmarks.remove(currentPage); currentPage = pageNumber; bookmarks.add(currentPage); System.out.println(currentBook.getPage(currentPage)); } }Solution:
public class Book { private long ID; private String details; private static Set<Book> books; public long getID() { return ID; } public Book(long id, String details) { } public static void addBook(long iD, String details) { books.add(new Book(iD, details)); } public void update() { } public static void delete(Book b) { books.remove(b); } public static Book find(long id) { for (Book b : books) if (b.getID() == id) return b; return null; } } public class User { private long ID; private String details; private int accountType; private static Set<User> users; public long getID() { return ID; } 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 static void addUser(long ID, String details, int accountType) { users.add(new User(ID, details, accountType)); } public User(long iD, String details, int accountType) { } } public class OnlineReaderSystem { private Book b; private User u; public OnlineReaderSystem(Book b, User u) { } public void listenRequest() { } public Book searchBook(long ID) { return Book.find(ID); } public User searchUser(long ID) { return User.find(ID); } public void display() { } }One thing I learnt from the solution is to put a private static set of all users in a user class. Hence all of the single user shares a copy of references to all the other users. Brilliant design.
dd
May 06, 2013 @ 10:00:20
“Hence all of the single user shares a copy of references to all the other users. Brilliant design.” Thats stupid!
Kinshuk Chandra
Mar 22, 2014 @ 15:12:24
Hi Tian, I have also designed online reader system : here. Please let me know how you feel about it or if the design can be improved.