I am practicing designing OOP projects. And this is my first OOP design.
I need some reviews for the code and the design please.
The code.
import java.util.HashMap;
public class Library {
private static HashMap<Book, Boolean> store = new HashMap<Book, Boolean>();
public static HashMap<Book, Boolean> getStore() {
return store;
}
public static boolean isBookExist(Book book) {
if(store.containsKey(book))
return store.get(book);
return false;
}
}
public class Book {
private String name, author;
Book(String name, String author) {
this.name = name;
this.author = author;
}
String getName() {
return name;
}
String getAuthor() {
return author;
}
}
public class Action {
void readBook(Book book) {
if(Library.isBookExist(book))
System.out.println("An user is reading book: \"" + book.getName() + "\" for the author: " + book.getAuthor());
else
System.out.println("Book \"" + book.getName() + "\" for the author: " + book.getAuthor() + " is not exist");
}
}
public class AdminAction extends Action {
protected void addBook(Book book) {
if(Library.isBookExist(book)) {
System.out.println("This book is already in the library");
return;
}
Library.getStore().put(book, true);
System.out.println("Book: \"" + book.getName() + "\" for the author: " + book.getAuthor() + "is added to the library");
}
protected void removeBook(Book book) {
if(!Library.isBookExist(book)) {
System.out.println("This book is not in the library");
return;
}
Library.getStore().remove(book);
System.out.println("Book: \"" + book.getName() + "\" for the author: " + book.getAuthor() + "is removed from the library");
}
}
public class User extends Action {
private String name;
User (String name) {
this.name = name;
}
public String getName() {
return name;
}
public void changeName(String newName) {
this.name = name;
}
}
public class Admin extends AdminAction {
private String name;
Admin(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void changeName(String newName) {
this.name = name;
}
}
public class LibrarySystem {
public static void main(String[] args) {
Admin admin = new Admin("Omar");
Admin admin2 = new Admin("Ahmed");
User user = new User("user1");
User user2 = new User("user2");
Book book = new Book("Introduction to algorithms", "Omar");
Book book2 = new Book("Clean Code", "Ghada");
Book book3 = new Book("Clean Arch", "Waled");
Book book4 = new Book("Head First", "Hend");
user.readBook(book);
admin2.addBook(book);
admin2.addBook(book2);
admin2.addBook(book3);
admin.addBook(book4);
user2.readBook(book);
user.readBook(book);
user.readBook(book2);
admin2.removeBook(book);
user.readBook(book);
user2.readBook(book);
user.readBook(book4);
}
}