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();
- library.addBook("Java For Begginers", "2017", "Jakub Niedzielski");
- // library.addBook("Java For Begginers", "2017", "Jakub Niedzielski");
- // library.addBook("Java For Begginers", "2017", "Jakub Niedzielski");
- library.addBook("Java Podstawy Wydanie X", "2016", "Cay S. Horstmann");
- library.addBook("Java. Przewodnik dla początkujących. Wydanie V", "2012", "Herbert Schildt");
- library.addBook("Capitalism: The unknown ideal", "1966", "Ayn Rand");
- library.addBook("Atlas shrugged", "1957", "Ayn Rand");
- library.addBook("Java For Dumbasses", "2017", "Jakub Niedzielski");
- library.addBook("Effective Java: Programming Language Guide", "2017", "Joshua Bloch");
- library.addBook("Java Performance", "2016", "Charlie Hunt");
- library.addBook("Biografia", "2010", "Jakub Niedzielski");
- library.addBook("Java Podstawy Wydanie X", "2016", "Cay S. Horstmann");
- 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 int numberOfCopies;
- private Map<Integer, Book> bookMap = new HashMap<>();
- private List<Book> copy = new ArrayList<>(bookMap.values());
- public void addBook(String title, String year, String author) {
- uniqueID++;
- if (bookMap.isEmpty() == false) {
- for (Book book : copy) {
- if (book.getTitle().equals(title) && (book.getYear().equals(year))
- && (book.getAuthor().equals(author))) {
- book.addCopy();
- } else {
- Book newBook = new Book(title, year, author, uniqueID, numberOfCopies);
- bookMap.put(uniqueID, newBook);
- }
- }
- } else {
- Book newBook = new Book(title, year, author, uniqueID, numberOfCopies);
- bookMap.put(uniqueID, newBook);
- }
- }
- public void getInfoByID(Integer bookID) {
- Book book = bookMap.get(bookID);
- if (book != null) {
- book.print();
- }
- }
- public void getStatusInfo(Integer bookID) {
- Book statusInfo = bookMap.get(bookID);
- System.out.println("The book of ID: " + bookID + " is " + statusInfo.getBookStatus());
- }
- public void lendBook(Integer bookID, String borrowersName) {
- Book bookStatus = bookMap.get(bookID);
- if (bookStatus.getEnumBookStatus().equals(BookStatus.AVAILABLE)) {
- bookStatus.lendBook(borrowersName);
- } else {
- System.out.println("This book is already lent by " + bookStatus.getBorrowersName());
- }
- }
- public void returnBook(Integer bookID) {
- Book bookStatus = bookMap.get(bookID);
- bookStatus.returnBook();
- }
- 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();
- }
- }
- public void getAllByTitle(String title) {
- for (Book book : bookMap.values()) {
- if (title.equals(book.getTitle())) {
- book.print();
- System.out.println("Number of copies: " + numberOfCopies);
- }
- }
- }
- 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 void getNumberOfCopies(String title) {
- // for (Book book : bookMap.values()) {
- // if (title.equals(book.getTitle())) {
- // book.getNumberOfCopies();
- // }
- //
- // }
- //
- // }
- 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;
- private String borrowersName;
- private int numberOfCopies = 1;
- public Book(String title, String year, String author, int uniqueID, int numberOfCopies) {
- this.id = uniqueID;
- this.title = title;
- this.year = year;
- this.author = author;
- this.numberOfCopies = numberOfCopies;
- }
- public String getTitle() {
- return title;
- }
- public String getYear() {
- return year;
- }
- public String getAuthor() {
- return author;
- }
- public int getId() {
- return id;
- }
- public void lendBook(String borrowersName) {
- numberOfCopies--;
- if (numberOfCopies == 0) {
- bookStatus = BookStatus.NON_AVAILABLE;
- }
- this.borrowersName = borrowersName;
- }
- public void returnBook() {
- numberOfCopies++;
- bookStatus = BookStatus.AVAILABLE;
- }
- public String getBookStatus() {
- return bookStatus.toString();
- }
- public BookStatus getEnumBookStatus() {
- return bookStatus;
- }
- public String getBorrowersName() {
- return borrowersName;
- }
- public void addCopy() {
- numberOfCopies++;
- }
- public int getNumberOfCopies() {
- return numberOfCopies;
- }
- 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());
- if (this.bookStatus.equals(BookStatus.NON_AVAILABLE)) {
- System.out.println("This book is " + this.getBookStatus() + ", " + borrowersName + " borrowed it");
- } else {
- System.out.println("This book is " + this.getBookStatus());
- }
- System.out.println("Number of copies: " + this.getNumberOfCopies());
- }
- }
- package pl.kimes.core;
- enum BookStatus {
- AVAILABLE {
- @Override
- public String toString() {
- return "available";
- }
- },
- NON_AVAILABLE {
- @Override
- public String toString() {
- return "non available";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement