import java.io.*; import java.util.*; public class BookDAOImpl implements BookDAO { private final String filePath; private final List books = new ArrayList<>(); public BookDAOImpl(String filePath) { this.filePath = filePath; load(); } private void load() { try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { br.readLine(); // skip header String line; while ((line = br.readLine()) != null) { String[] arr = line.split(","); books.add(new Book(arr[0], arr[1])); } } catch (IOException e) { e.printStackTrace(); } } @Override public List getAllBooks() { return books; } @Override public Book getByISBN(String isbn) { return books.stream() .filter(b -> b.getIsbn().equals(isbn)) .findFirst().orElse(null); } @Override public void addBook(Book book) { books.add(book); } @Override public void saveAll() { try (PrintWriter pw = new PrintWriter(new FileWriter(filePath))) { pw.println("ISBN,Title"); for (Book b : books) { pw.println(b.getIsbn() + "," + b.getTitle()); } } catch (IOException e) { e.printStackTrace(); } } }