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);
}
}
}
}
Comments
Leave a comment