如题,设计一个在线电子图书阅读系统,主要设计底层数据结构。那不说了,直接贴代码吧。
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));
}
}
官方给了一个标准答案,也一起贴了吧,精妙之处是将所有的Book和Users设为静态,这样,每一个Book和User都可以共享到其他对象的资源。
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() { } }