I have designed and Online Book Reader System. You can find the classes below. I would be appreciated for the valuable reviews.
Assumptions: "Online Book Reader System" is a system includes online books and serve their customers to read books online. User should be login to reach their accounts. System can include many users and many books. Multiple users can access the same book. User can add any books to their reading list. After users login to system they can start to read any book they want and also can proceed with the latest page they have resumed.
Especially considering;
Multi-threading / SOLID principles / Design patterns
Book.java
package OOPDesign.onlineBookReaderSystem;
import java.util.ArrayList;
public abstract class Book {
long bookId;
String name;
String category;
String author;
int pageCount;
ArrayList<User> readers = new ArrayList<>();
public Book(String name, String category, String author, int pageCount){
this.bookId = name.hashCode();
this.name = name;
this.category = category;
this.author = author;
this.pageCount = pageCount;
}
ArrayList<User> getReaders(){
return readers;
}
}
BookProgress.java
package OOPDesign.onlineBookReaderSystem;
public class BookProgress {
long userId;
Book book;
int resumedPage;
public BookProgress(Book book, long userId) {
this.book = book;
this.userId = userId;
this.resumedPage = 0;
}
public void setResumedPage(int resumedPage) {
this.resumedPage = resumedPage;
}
public void pageForward(){
resumedPage++;
setResumedPage(resumedPage);
}
public void pageBackward(){
resumedPage--;
setResumedPage(resumedPage);
}
}
FictionBook.java
package OOPDesign.onlineBookReaderSystem;
public class FictionBook extends Book {
public FictionBook(String name, String category, String author, int pageCount) {
super(name,"Fiction", author, pageCount);
}
}
Novel.java
package OOPDesign.onlineBookReaderSystem;
public class Novel extends Book {
public Novel(String name, String category, String author, int pageCount) {
super(name, "Novel", author, pageCount);
}
}
OnlineReaderSystem.java
package OOPDesign.onlineBookReaderSystem;
public class OnlineReaderSystem {
public static void main(String[] args) {
// Create user
User userNes = new User("Nesly", "Nesly","Password");
// Create book
Book bookFiction = new FictionBook("Fiction Book", "Fiction", "James",320);
// User login
userNes.login("Nesly","password");
// Start reading book
userNes.addBook(bookFiction);
userNes.startReading(bookFiction);
userNes.finishReading(bookFiction);
}
}
User.java
package OOPDesign.onlineBookReaderSystem;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.Semaphore;
public class User {
private long userId;
private String name;
private String subcriptionType;
private Date subsciptionDate;
private String login_UserId;
private String login_Password;
private String lastLoginDate;
Semaphore semaphoreReader;
HashMap<Book, BookProgress> userBooks = new HashMap<>();
private String creditCardInfo;
public User(String name, String login_UserId, String login_Password) {
this.userId = name.hashCode();
this.name = name;
this.subcriptionType = "Classic";
this.login_UserId = login_UserId;
this.login_Password = login_Password;
}
public void login(String loginUser, String login_Password){
if(this.login_UserId == login_UserId && this.login_Password == login_Password) {
System.out.println("Welcome " + name);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
lastLoginDate = dtf.format(now);
}else {
System.out.println("Unsuccessful login " + name);
}
}
public void addBook(Book book){
userBooks.put(book, new BookProgress(book,userId));
}
public HashMap<Book, BookProgress> getRegisteredBooks(){
return this.userBooks;
}
public int startReading(Book book) throws InterruptedException {
int resumedPage = userBooks.get(book).resumedPage;
BookProgress progress = userBooks.get(book);
for(int i=0;i<50;i++){
progress.pageForward();
}
System.out.println("Started reading");
return resumedPage;
}
public void finishReading(Book book){
BookProgress progress = userBooks.get(book);
System.out.println("Finished reading at "+ progress.resumedPage);
}
}