Answer to Question #291514 in Java | JSP | JSF for Silly

Question #291514

Using ArrayList create a book borrowing system. Create ListBook.txt that list all the book information such as bookId, bookTitle, bookAuthor, bookGenre. 

  • Display all the book available from ListBook.txt
  • User can search the book that want to borrow by bookId or bookTitle
  • User can sort the book by bookGenre
  • count and display how many book user borrow
  • display the expected time the user should returned the book
1
Expert's answer
2022-01-28T10:33:18-0500
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
    private static ArrayList<Book> books;
    private static boolean[] isBorrowed;

    public static void readBookFile() {
        try (BufferedReader reader = new BufferedReader(new FileReader("ListBook.txt"))) {
            String line;
            books = new ArrayList<>();
            while ((line = reader.readLine()) != null) {
                String[] data = line.split(",");
                books.add(new Book(Integer.parseInt(data[0]), data[1], data[2], data[3]));
            }
            isBorrowed = new boolean[books.size()];
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int getIndexByID(int id) {
        for (int i = 0; i < books.size(); i++) {
            if (books.get(i).getId() == id) {
                return i;
            }
        }
        return -1;
    }

    public static int getIndexByTitle(String title) {
        for (int i = 0; i < books.size(); i++) {
            if (books.get(i).getTitle().equals(title)) {
                return i;
            }
        }
        return -1;
    }

    public static void borrowBook(int index, Scanner in) {
        if (index != -1) {
            System.out.println(books.get(index));
            if (!isBorrowed[index]) {
                System.out.print("Borrow( Y | N )? ");
                if (in.nextLine().equalsIgnoreCase("Y")) {
                    isBorrowed[index] = true;
                }
            } else {
                System.out.println("The book has already been borrowed.");
            }
        } else {
            System.out.println("Book not found.");
        }
    }

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

    public static void sortByGenre() {
        books.sort(Comparator.comparing(Book::getGenre));
    }

    public static void displayBorrowed() {
        int total = 0;
        System.out.println("You borrow: ");
        for (int i = 0; i < isBorrowed.length; i++) {
            if (isBorrowed[i]) {
                total++;
                System.out.println(books.get(i) + "; Return time: 1 week");
            }
        }
        System.out.println("Total: " + total);
    }

}


public class Book {
    private int id;
    private String title;
    private String author;
    private String genre;

    public Book(int id, String title, String author, String genre) {
        this.id = id;
        this.title = title;
        this.author = author;
        this.genre = genre;
    }

    public int getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public String getGenre() {
        return genre;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", genre='" + genre + '\'' +
                '}';
    }
}

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