Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.HashMap;
- /**
- * Library to organize books
- *
- * @author (redacted)
- * @version (0.21)
- */
- public class BookLibrary
- {
- //ArrayList to store book objects
- private ArrayList<Book> library;
- //Creates a Library
- public BookLibrary()
- {
- library = new ArrayList<>();
- }
- public void addBook(Book book)
- {
- library.add(book);
- }
- //Adds book to library
- public void addBook(String author, String title,String publisher,int pages, int year,String ean,Boolean rented)
- {
- library.add(new Book(title,author,year,publisher,pages,ean,rented));
- }
- //Searches through the library and returns first book matching the string "title"
- public Book findBook(String title)
- {
- boolean notFound = true;
- Iterator<Book> it = library.iterator();
- Book book = null;
- while(it.hasNext()&& notFound)
- {
- Book b = it.next();
- if(b.getTitle().toLowerCase().equals(title.toLowerCase()))
- {
- book = b;
- notFound = false;
- }
- }
- return book;
- }
- public Book findBookByEan(String ean)
- {
- boolean notFound = true;
- Iterator<Book> it = library.iterator();
- Book book = null;
- while(it.hasNext()&& notFound)
- {
- Book b = it.next();
- if(b.getEan().toLowerCase().equals(ean.toLowerCase()))
- {
- book = b;
- notFound = false;
- }
- }
- return book;
- }
- //Goes down the library and returns all books where the author matches the string "author" in a HashMap
- public HashMap booksFromAuthor(String author)
- {
- HashMap<Double,Book> collection = new HashMap<>();
- Iterator<Book> it = library.iterator();
- double index = 1;
- while(it.hasNext())
- {
- Book b = it.next();
- if(b.getAuthor().toLowerCase().equals(author))
- {
- collection.put(index,b);
- index++;
- }
- }
- return collection;
- }
- public void removeBook(String title)
- {
- Iterator<Book> it = library.iterator();
- boolean notFound =true;
- while(it.hasNext() && notFound)
- {
- Book b = it.next();
- if(b.getTitle().toLowerCase().equals(title))
- {library.remove(b);
- notFound= false;
- }
- }
- }
- //Returns iterator to go through the library
- public Iterator<Book> getIterator()
- {
- Iterator <Book> it = library.iterator();
- return it;
- }
- //Goes down the library and returns the first book with a matching title
- public int findIndexByName(String title)
- {
- boolean notFound = true;
- int ind = 0;
- int bookIndex = 9999999;
- while(notFound == true && ind < library.size())
- {
- Book book = library.get(ind);
- if(title.equals(book.getTitle()))
- {
- notFound = false;
- bookIndex = ind;
- }
- ind++;
- }
- return bookIndex;
- }
- //Returns title of book with index "index"
- public void getTitle(int index)
- {
- Book book = library.get(index);
- System.out.println(book.getTitle());
- }
- //Returns author of book with index "index"
- public void getAuthor(int index)
- {
- Book book = library.get(index);
- System.out.println(book.getAuthor());
- }
- //Returns year of book with index "index"
- public void getYear(int index)
- {
- Book book = library.get(index);
- System.out.println(book.getYear());
- }
- //Returns pages of book with index "index"
- public void getPages(int index)
- {
- Book book = library.get(index);
- System.out.println(book.getPages());
- }
- //Returns publisher of book with index "index"
- public void getPublisher(int index)
- {
- Book book = library.get(index);
- System.out.println(book.getPublisher());
- }
- //Prints all books in the library
- public void listAllBooks()
- {
- Iterator<Book> it = library.iterator();
- while(it.hasNext())
- {
- Book book = it.next();
- System.out.println(book.getTitle());
- }
- }
- //Adds 6 books to the library for testing
- public void tester()
- {
- library.add(new Book("The Hunger Games", "Suzanne Collins", 2008,"Scholastic Corporation" , 374,"1627384950843",false));
- library.add(new Book("Gone", "Michael Grant", 2008, "HarperCollins", 576,"1029384756648",false));
- library.add(new Book("Harry Potter", "J. K. Rowling", 1997,"Schooltastic" , 309,"6574839201985",false));
- library.add(new Book("BlueJ", "David J. Barnes and Michael Kölling", 2003,"Pearson" , 630,"1920384756123",false));
- library.add(new Book("The Hobbit", "J.R.R. Tolkien", 1937,"Allen & Unwin" , 310,"1230987654321",false));
- library.add(new Book("S","Michael Grant",12,"",1, "1234567890123",false));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement