Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class main {
- public static void main(String[] args) {
- Lib lib = new Lib();
- lib.newBook("Into the Wild","Jon Krakauer","Villard","1996", "9789544745349");
- lib.newBook("My Biography","Roberto Vasilev","My Publishment","2019", "0-1111-1111-1");
- lib.newBook("Harry Potter and the Philosopher Stone","J.K. Rowling","Bloomsbury","1997", "0-7475-3269-9");
- lib.newBook("Darkmouth","Shane Hegarty","Ciela","2015", "978-954-28-1898-4");
- lib.getAllBook();
- System.out.println();
- lib.getBookByAuthor("J.K. Rowling");
- lib.deleteBookByAuthor("J.K. Rowling");
- System.out.println();
- lib.getAllBook();
- }
- }
- ------------
- import java.util.*;
- public class Lib {
- Map<authors, book> libraryBooks = new HashMap<>();
- public void newBook(String header, String author, String publisher, String dateOfPublishment, String ISBN) {
- libraryBooks.put(new authors(author), new book(header, author, publisher, dateOfPublishment, ISBN));
- }
- public void getBookByAuthor(String author) {
- for (authors authors : libraryBooks.keySet()) {
- if (authors.getAuthor().equals(author)) {
- System.out.println(libraryBooks.get(authors));
- }
- }
- }
- public void deleteBookByAuthor(String author) {
- for(Iterator<Map.Entry<authors, book>> it = libraryBooks.entrySet().iterator(); it.hasNext(); ) {
- Map.Entry<authors, book> entry = it.next();
- if(entry.getKey().getAuthor().equals(author)) {
- it.remove();
- }
- }
- }
- public void deleteBookByName(String name) {
- for(Iterator<Map.Entry<authors, book>> it = libraryBooks.entrySet().iterator(); it.hasNext(); ) {
- Map.Entry<authors, book> entry = it.next();
- if(entry.getValue().getHeader().equals(name)) {
- it.remove();
- }
- }
- }
- public void getAllBook() {
- for (authors authors : libraryBooks.keySet()) {
- System.out.println(libraryBooks.get(authors));
- }
- }
- public void getBookByName(String name) {
- for (book book : libraryBooks.values()) {
- if (book.getHeader().equals(name)) {
- for (authors authors : libraryBooks.keySet()) {
- if (authors.getAuthor().equals(book.getAuthor())) {
- System.out.println(libraryBooks.get(authors));
- }
- }
- }
- }
- }
- }
- ------------
- public class book extends Lib {
- protected String header;
- protected String author;
- protected String publisher;
- protected String dateOfPublishment;
- protected String ISBN;
- public book(String header, String author, String publisher, String dateOfPublishment, String ISBN) {
- this.header = header;
- this.author = author;
- this.publisher = publisher;
- this.dateOfPublishment = dateOfPublishment;
- this.ISBN = ISBN;
- }
- public String getHeader() {
- return header;
- }
- public String getAuthor() {
- return author;
- }
- public String getPublisher() {
- return publisher;
- }
- public String getDateOfPublishment() {
- return dateOfPublishment;
- }
- public String getISBN() {
- return ISBN;
- }
- @Override
- public String toString() {
- return String.format("Book: '%s' Author: '%s' Publisher: '%s' Published: '%s' Date of publishment: '%s'",this.getHeader(),this.getAuthor(),this.getPublisher(),this.getDateOfPublishment(),this.getISBN());
- }
- }
- -------------
- public class authors {
- private String author;
- public String getAuthor() {
- return author;
- }
- public authors(String author) {
- this.author = author;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement