Answer to Question #291253 in Java | JSP | JSF for NANA

Question #291253

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
  • count and display how many book user borrow
  • display the expected time the user should returned the book
1
Expert's answer
2022-01-27T13:27:00-0500
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;
    }

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



import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
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 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 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 static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        readBookFile();
        while (true) {
            System.out.println("1. Display books\n2. Find book by ID\n0. Exit");
            switch (in.nextLine()) {
                case "1":
                    displayBooks();
                    break;
                case "2":
                    System.out.print("ID: ");
                    borrowBook(getIndexByID(Integer.parseInt(in.nextLine())), in);
                    break;
                case "0":
                    displayBorrowed();
                    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