Created
September 16, 2018 13:50
-
-
Save codegagan/a72c76dce0c5f857e67bc09569d5c530 to your computer and use it in GitHub Desktop.
HackerRank Library system design in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashSet; | |
import java.util.Optional; | |
import java.util.Set; | |
import java.util.stream.Collectors; | |
public class Solution { | |
public static void main(String args[]) throws Exception { | |
/* Enter your code here. Read input from STDIN. Print output to STDOUT */ | |
} | |
} | |
class User { | |
private String name; | |
private String id; | |
private String email; | |
private RegistrationStatus status; | |
public User(String name, String id, String email, RegistrationStatus status) { | |
super(); | |
this.name = name; | |
// Id cannot be changed once user is created. | |
this.id = id; | |
this.email = email; | |
this.status = status; | |
} | |
/** | |
* ID has to be unique across, if ID is same its the same user | |
*/ | |
@Override | |
public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + ((id == null) ? 0 : id.hashCode()); | |
return result; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) { | |
return true; | |
} | |
if (obj == null) { | |
return false; | |
} | |
if (!(obj instanceof User)) { | |
return false; | |
} | |
User other = (User) obj; | |
if (id == null) { | |
if (other.id != null) { | |
return false; | |
} | |
} else if (!id.equals(other.id)) { | |
return false; | |
} | |
return true; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getEmail() { | |
return email; | |
} | |
public void setEmail(String email) { | |
this.email = email; | |
} | |
public RegistrationStatus getStatus() { | |
return status; | |
} | |
public void setStatus(RegistrationStatus status) { | |
this.status = status; | |
} | |
public String getId() { | |
return id; | |
} | |
} | |
enum RegistrationStatus { | |
PENDING, APPROVED, REJECTED | |
} | |
class UserUtility { | |
private static Library library = Library.getInstance(); | |
/** | |
* Register new user | |
* | |
* @param id | |
* is must field, other values can be default | |
* @return | |
*/ | |
public static boolean registerUser(String id) { | |
return registerUser(id, ""); | |
} | |
public static boolean registerUser(String id, String name) { | |
return registerUser(id, name, ""); | |
} | |
/** | |
* Register the user | |
* | |
* @param id | |
* @param name | |
* @param email | |
* @return false if user already exist with same id and does register user. | |
*/ | |
public static boolean registerUser(String id, String name, String email) { | |
return library.getUsers().add(new User(id, name, email, RegistrationStatus.PENDING)); | |
} | |
public static Set<User> getApprovedUsers() { | |
return getUsersByStatus(RegistrationStatus.APPROVED); | |
} | |
public static Set<User> getPendingUsers() { | |
return getUsersByStatus(RegistrationStatus.PENDING); | |
} | |
public static Set<User> getRejectedUsers() { | |
return getUsersByStatus(RegistrationStatus.REJECTED); | |
} | |
private static Set<User> getUsersByStatus(RegistrationStatus status) { | |
return library.getUsers().stream().filter(user -> user.getStatus() == status).collect(Collectors.toSet()); | |
} | |
public void approveUser(String id) { | |
setUserStatus(id, RegistrationStatus.APPROVED); | |
} | |
private static void setUserStatus(String id, RegistrationStatus status) { | |
library.getUsers().stream().filter(user -> user.getId() == id).findFirst().get().setStatus(status); | |
} | |
public void rejectUser(String id) { | |
setUserStatus(id, RegistrationStatus.REJECTED); | |
} | |
} | |
class BooksUtility { | |
private static Library library = Library.getInstance(); | |
public Set<Book> findBook(String author, String category) { | |
return library.getBooks().stream().filter(book -> book.getAuthor() == author || book.getCategory() == category) | |
.collect(Collectors.toSet()); | |
} | |
public static void loanBook(Book book) { | |
Optional<Book> bookFound = library.getBooks().stream().filter(b -> b.equals(book)).findFirst(); | |
synchronized (library) { | |
Book found = bookFound.get(); | |
if (found.getStatus() == LoanStatus.AVAILABLE) { | |
bookFound.get().setStatus(LoanStatus.LOANED); | |
} else { | |
throw new IllegalStateException("Book already loaned"); | |
} | |
} | |
} | |
} | |
enum LoanStatus { | |
LOANED, AVAILABLE | |
} | |
class Book { | |
private String name; | |
private String author; | |
private String category; | |
private LoanStatus status; | |
@Override | |
public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + ((author == null) ? 0 : author.hashCode()); | |
result = prime * result + ((category == null) ? 0 : category.hashCode()); | |
result = prime * result + ((name == null) ? 0 : name.hashCode()); | |
return result; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) { | |
return true; | |
} | |
if (obj == null) { | |
return false; | |
} | |
if (!(obj instanceof Book)) { | |
return false; | |
} | |
Book other = (Book) obj; | |
if (author == null) { | |
if (other.author != null) { | |
return false; | |
} | |
} else if (!author.equals(other.author)) { | |
return false; | |
} | |
if (category == null) { | |
if (other.category != null) { | |
return false; | |
} | |
} else if (!category.equals(other.category)) { | |
return false; | |
} | |
if (name == null) { | |
if (other.name != null) { | |
return false; | |
} | |
} else if (!name.equals(other.name)) { | |
return false; | |
} | |
return true; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getAuthor() { | |
return author; | |
} | |
public void setAuthor(String author) { | |
this.author = author; | |
} | |
public String getCategory() { | |
return category; | |
} | |
public void setCategory(String category) { | |
this.category = category; | |
} | |
public LoanStatus getStatus() { | |
return status; | |
} | |
public void setStatus(LoanStatus status) { | |
this.status = status; | |
} | |
} | |
class Library { | |
private static Library instance = new Library(); | |
private Set<User> users = new HashSet<>(); | |
// Assuming there is only one book of each type. | |
private Set<Book> books = new HashSet<>(); | |
private Library() { | |
} | |
public static Library getInstance() { | |
return instance; | |
} | |
public Set<User> getUsers() { | |
return users; | |
} | |
public Set<Book> getBooks() { | |
return books; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment