Answer to Question #258628 in Java | JSP | JSF for sjk

Question #258628
class Book
{
    int bookIsbnNo;
    String author;
    String publisher;
   
}

class Library
{
    String libraryName;
    String address
    String libraryRegno;
    Book[] books;// A library has books
}
Methods

    - getBooks() : returns Array<Book> 
    - showAllBooks() : Print all the books details


class MemberAccount
{
    String name;
    int accountNo;
    Books[] borrowed;
}

Methods

- borrowBooks(Book b) 
- returnBooks(String bookIsbnNo) : returns a Book

Class LibraryHack { Main method class }

Broadly the below task needs to be done

Need to initialize 5 books in Library class default constructor

LibraryHack is the client program, where you have main methord

Based on the user choice , they can view books, borrowbooks or returnbooks

1- View all books in library class

2- Borrowbooks --> add the book in borrowedBooks list of memberaccountclass

3- Returnbooks -> remove the book from borrowedBooks list
1
Expert's answer
2021-10-30T03:13:45-0400
public class Book {
    private String bookIsbnNo;
    private String author;
    private String publisher;

    public Book(String bookIsbnNo, String author, String publisher) {
        this.bookIsbnNo = bookIsbnNo;
        this.author = author;
        this.publisher = publisher;
    }

    public String getBookIsbnNo() {
        return bookIsbnNo;
    }

    @Override
    public String toString() {
        return bookIsbnNo + " " + author + " " + publisher;
    }
}


public class Library {
    private String libraryName;
    private String address;
    private String libraryRegNo;
    private Book[] books;


    public Library() {
        libraryName = "Library";
        address = "Earth";
        libraryRegNo = "1";
        books = new Book[]{new Book("1", "Me", "Me"), new Book("2", "She", "She"),
                new Book("3", "It", "It"), new Book("4", "They", "They"), new Book("5", "He", "He")};
    }

    public Library(String libraryName, String address, String libraryRegNo, Book[] books) {
        this.libraryName = libraryName;
        this.address = address;
        this.libraryRegNo = libraryRegNo;
        this.books = books;
    }

    public Book[] getBooks() {
        return books;
    }

    public void showAllBooks() {
        for (Book book : books) {
            System.out.println(book);
        }
    }

    @Override
    public String toString() {
        return libraryName + " " + address + " " + libraryRegNo;
    }
}


public class MemberAccount {
    private String name;
    private int accountNo;
    private Book[] borrowed;

    public MemberAccount(String name, int accountNo, int maxBorrowedBooks) {
        this.name = name;
        this.accountNo = accountNo;
        borrowed = new Book[maxBorrowedBooks];
    }

    public void borrowBook(Book book) {
        for (int i = 0; i < borrowed.length; i++) {
            if (borrowed[i] == null) {
                borrowed[i] = book;
                break;
            }
        }
    }

    public Book returnBook(String bookIsbnNo) {
        Book book = null;
        for (int i = 0; i < borrowed.length; i++) {
            if (borrowed[i] != null && borrowed[i].getBookIsbnNo().equals(bookIsbnNo)) {
                book = borrowed[i];
                borrowed[i] = null;
            }
        }
        return book;
    }
}


import java.util.Scanner;

public class LibraryHack {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Library library = new Library();
        MemberAccount memberAccount = new MemberAccount("Me", 1, 3);
        String choice;
        while (true) {
            System.out.println("1. View all books in library" +
                    "\n2. Borrow book" +
                    "\n3. Return book" +
                    "\n0. Exit");
            choice = in.nextLine();
            switch (choice) {
                case "1":
                    library.showAllBooks();
                    break;
                case "2":
                    System.out.println("Enter an ISBN number:");
                    choice = in.nextLine();
                    for (Book book : library.getBooks()) {
                        if (book != null && book.getBookIsbnNo().equals(choice)) {
                            memberAccount.borrowBook(book);
                            break;
                        }
                    }
                    break;
                case "3":
                    System.out.println("Enter an ISBN number:");
                    choice = in.nextLine();
                    System.out.println(memberAccount.returnBook(choice));
                    break;
                case "0":
                    System.exit(0);
            }
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS