Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.kimes.core;
- public class Main {
- public static void main(String[] args) {
- Library library = new Library();
- Book b = new Book("Java For Begginers", "2017", "Jakub Niedzielski");
- Book b2 = new Book("Java For Begginers", "2017", "Jakub Niedzielski");
- Book b3 = new Book("Java for dummies", "2010", "Unknown");
- library.addBook(b);
- library.addBook(b2);
- library.addBook(b3);
- library.removeBook(1);
- library.getAllBooks();
- }
- }
- package pl.kimes.core;
- import java.util.ArrayList;
- 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 void lendBook(Integer bookID, String borrowersName) {
- Book bookValues = bookMap.get(bookID);
- int numberOfCopies = copiesMap.get(bookValues);
- if (numberOfCopies == 0) {
- System.out.println("Can't lend this book, it's been alredy lent by "
- + borrowersMap.get(bookValues).toString().replace("[", "").replace("]", ""));
- } else {
- List<String> borrowersList = borrowersMap.get(bookValues);
- if (borrowersList == null) {
- borrowersList = new ArrayList<>();
- borrowersMap.put(bookValues, borrowersList);
- }
- borrowersList.add(borrowersName);
- copiesMap.put(bookValues, copiesMap.get(bookValues) - 1);
- }
- }
- public void returnBook(Integer bookID, String returnersName) {
- Book bookValues = bookMap.get(bookID);
- List<String> borrowersList = borrowersMap.get(bookValues);
- if (borrowersList == null) {
- System.out.println("This book wasn't lent by you, you can't return it");
- return;
- }
- if (!borrowersList.contains(returnersName)) {
- System.out.println("Invalid name");
- }
- borrowersList.remove(returnersName);
- copiesMap.put(bookValues, copiesMap.get(bookValues) + 1);
- }
- public void removeBook(Integer bookID) {
- Book bookValues = bookMap.get(bookID);
- if (bookValues != null) {
- if (borrowersMap.get(bookValues) == null) {
- bookMap.remove(bookID);
- copiesMap.put(bookValues, copiesMap.get(bookValues) - 1);
- } else {
- System.out.println("Can't remove that book right now, it's lent");
- }
- } else {
- System.out.println("Invalid ID number");
- }
- }
- public void getInfoByID(Integer bookID) {
- Book book = bookMap.get(bookID);
- if (book != null) {
- book.print();
- printCopies(book);
- }
- }
- public void getAllBooks() {
- for (Book book : bookMap.values()) {
- book.print();
- printCopies(book);
- }
- }
- public void getAllByTitle(String title) {
- for (Book book : bookMap.values()) {
- if (title.equals(book.getTitle())) {
- book.print();
- printCopies(book);
- }
- }
- }
- public void getAllByAuthor(String author) {
- for (Book book : bookMap.values()) {
- if (author.equals(book.getAuthor())) {
- book.print();
- printCopies(book);
- }
- }
- }
- public void getAllByYear(String year) {
- for (Book book : bookMap.values()) {
- if (year.equals(book.getYear())) {
- book.print();
- printCopies(book);
- }
- }
- }
- public void getAllByAuthorAndYear(String author, String year) {
- for (Book book : bookMap.values()) {
- if (author.equals(book.getAuthor()) && (year.equals(book.getYear()))) {
- book.print();
- printCopies(book);
- }
- }
- }
- private void printCopies(Book book) {
- copiesMap.get(book);
- System.out.println("Number of copies: " + getNumberOfCopies(book));
- }
- private int getNumberOfCopies(Book book) {
- return copiesMap.get(book);
- }
- }
- package pl.kimes.core;
- public class Book {
- private int id;
- private String title;
- private String year;
- private String author;
- 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 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;
- }
- // old implementation of equals method
- // @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