Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.kimes.core;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class Library {
- private int uniqueID;
- private Map<Integer, Book> bookMap = new HashMap<>();
- private Map<Book, Integer> copiesMap = new HashMap<>();
- private Map<Book, List<String>> borrowersMap = new HashMap<>();
- public void addBook(Book newAddbook) {
- Integer id = ++uniqueID;
- for (Book book : bookMap.values()) {
- if (book.equals(newAddbook)) {
- int numberOfCopies = getNumberOfCopies(newAddbook);
- newAddbook.setId(id);
- numberOfCopies++;
- bookMap.put(id, newAddbook);
- copiesMap.put(newAddbook, numberOfCopies);
- return;
- }
- }
- int numberOfCopies = 1;
- newAddbook.setId(id);
- bookMap.put(id, newAddbook);
- copiesMap.put(newAddbook, numberOfCopies);
- }
- public int getNumberOfCopies(Book book){
- return copiesMap.get(book);
- }
- public void getInfoByID(Integer bookID) {
- Book book = bookMap.get(bookID);
- if (book != null) {
- book.print();
- }
- }
- public void lendBook(Integer bookID, String borrowersName) {
- Book bookStatus = bookMap.get(bookID);
- }
- public void returnBook(Integer bookID, String returnersName) {
- Book bookStatus = bookMap.get(bookID);
- }
- public void removeBook(Integer bookID) {
- Book value = bookMap.get(bookID);
- if (value != null) {
- if (value.getEnumBookStatus().equals(BookStatus.AVAILABLE)) {
- bookMap.remove(bookID);
- } else {
- System.out.println("Can't remove that book right now, it's lent by " + value.getBorrowersName());
- }
- } else {
- System.out.println("Invalid ID number");
- }
- }
- public void getAllBooks() {
- for (Book book : bookMap.values()) {
- book.print();
- System.out.println("Number of copies: " + getNumberOfCopies(book));
- }
- }
- public void getAllByTitle(String title) {
- for (Book book : bookMap.values()) {
- if (title.equals(book.getTitle())) {
- book.print();
- }
- }
- }
- public void getAllByAuthor(String author) {
- for (Book book : bookMap.values()) {
- if (author.equals(book.getAuthor())) {
- book.print();
- }
- }
- }
- public void getAllByYear(String year) {
- for (Book book : bookMap.values()) {
- if (year.equals(book.getYear())) {
- book.print();
- }
- }
- }
- public void getAllByAuthorAndYear(String author, String year) {
- for (Book book : bookMap.values()) {
- if (author.equals(book.getAuthor()) && (year.equals(book.getYear()))) {
- book.print();
- }
- }
- }
- public Library() {
- }
- }
- package pl.kimes.core;
- public class Book {
- private int id;
- private String title;
- private String year;
- private String author;
- private BookStatus bookStatus = BookStatus.AVAILABLE;
- public Book(String title, String year, String author) {
- this.title = title;
- this.year = year;
- this.author = author;
- }
- public String getTitle() {
- return title;
- }
- public String getYear() {
- return year;
- }
- public String getAuthor() {
- return author;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public void setBookStatusToNonAvailable() {
- bookStatus = BookStatus.NON_AVAILABLE;
- }
- public void setBookStatusToAvailable() {
- bookStatus = BookStatus.AVAILABLE;
- }
- public String getBookStatus() {
- return bookStatus.toString();
- }
- public BookStatus getEnumBookStatus() {
- return bookStatus;
- }
- public void print() {
- System.out.println("-----");
- System.out.println("ID number: " + this.getId());
- System.out.println("Title: " + this.getTitle());
- System.out.println("Year: " + this.getYear());
- System.out.println("Author: " + this.getAuthor());
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((author == null) ? 0 : author.hashCode());
- result = prime * result + ((title == null) ? 0 : title.hashCode());
- result = prime * result + ((year == null) ? 0 : year.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Book other = (Book) obj;
- if (author == null) {
- if (other.author != null)
- return false;
- } else if (!author.equals(other.author))
- return false;
- if (title == null) {
- if (other.title != null)
- return false;
- } else if (!title.equals(other.title))
- return false;
- if (year == null) {
- if (other.year != null)
- return false;
- } else if (!year.equals(other.year))
- return false;
- return true;
- }
- // @Override
- // public boolean equals(Object arg0) {
- // if (Book.class.isInstance(arg0)) {
- //
- // return this.author.equals(((Book) arg0).author) &&
- // this.year.equals(((Book) arg0).year) &&
- // this.title.equals(((Book) arg0).title);
- // }
- // return false;
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement