/************************************************************************************************** * Program Name: Programming Project 5.7: Bookshelf * Class Name: BookshelfFilestore * Author: Terry Weiss * Date Written: October 15, 2015 * Program Description: * This class loads and saves all instance data for a book that will be used in `Bookshelf`. **************************************************************************************************/ package pp5_7; //import Book //import Bookshelf import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * * @author Terry */ public final class BookshelfFilestore { private static final String DEFAULT_SHELF_FILENAME = "shelf.bl"; private static File shelfFile; private static PrintWriter writer; private static List shelf; BookshelfFilestore() throws IOException, FileNotFoundException { shelfFile = new File(DEFAULT_SHELF_FILENAME); writer = new PrintWriter(new FileWriter(DEFAULT_SHELF_FILENAME)); shelf = new ArrayList<>(); } BookshelfFilestore( String filename ) throws IOException, FileNotFoundException { shelfFile = new File(filename); writer = new PrintWriter(new FileWriter(filename)); shelf = new ArrayList<>(); if (shelfFile.exists()) { loadBookshelf(); } } BookshelfFilestore( List booklist ) throws IOException, FileNotFoundException { shelfFile = new File(DEFAULT_SHELF_FILENAME); writer = new PrintWriter(new FileWriter(DEFAULT_SHELF_FILENAME)); shelf = booklist; } BookshelfFilestore( List booklist, String filename ) throws IOException, FileNotFoundException { shelfFile = new File(filename); writer = new PrintWriter(new FileWriter(filename)); shelf = booklist; if (shelfFile.exists()) { saveBookshelf(); } } BookshelfFilestore( Bookshelf bookshelf ) throws IOException, FileNotFoundException { shelfFile = new File(bookshelf.getName() + ".bl"); writer = new PrintWriter(new FileWriter(bookshelf.getName() + ".bl")); shelf = bookshelf.getBookList(); } BookshelfFilestore( Bookshelf bookshelf, String filename ) throws IOException, FileNotFoundException { shelfFile = new File(filename); writer = new PrintWriter(new FileWriter(filename)); shelf = bookshelf.getBookList(); if (shelfFile.exists()) { saveBookshelf(); } } /** * Updates the working file using the specified filename. * * @param filename Relative path of new file to be used * @return true if new file currently exists * @throws IOException */ public boolean useFile( String filename ) throws IOException { shelfFile = new File(filename); return shelfFile.exists(); } /** * Writes all {@link pp5_7.Book}s to the current file. * * @return true if any books were saved */ public boolean saveBookshelf() { if (shelf.isEmpty()) { return false; } else { for (Book book : shelf) { writer.write(book.getTitle() + "\n"); writer.write(book.getPages() + "\n"); writer.write(book.getAuthor() + "\n"); writer.write(book.getGenre() + "\n"); writer.write("\n"); writer.flush(); } return true; } } /** * Replaces current booklist with all the {@link pp5_7.Book}s from the current working file. * * @return List of Books loaded * @throws FileNotFoundException */ public List loadBookshelf() throws FileNotFoundException { Scanner fileReader = new Scanner(shelfFile); fileReader.useDelimiter("\n\n"); shelf.clear(); while (fileReader.hasNext()) { String bookStr = fileReader.next(); Scanner bookReader = new Scanner(bookStr); String title, author, genre; int pages; // For each book in the file's list for (int i = 0; bookReader.hasNext(); i++) { title = bookReader.nextLine(); pages = Integer.parseInt(bookReader.nextLine()); author = bookReader.nextLine(); genre = bookReader.nextLine(); shelf.add(new Book(title, pages, author, genre)); } } return shelf; } public static void main( String[] args ) throws IOException { List list = new ArrayList<>(); list.add(new Book("Pizza Cookbook", 751, "Pizza Chef", "Non-fiction")); list.add(new Book("Space Cowboys", 152, "Somebody Cool", "Sci-Fi")); list.add(new Book("Space Cadets", 852, "Somebody Cool", "History")); list.add(new Book("Cable News", 659, "Johny", "Non-fiction")); list.add(new Book("Pizza Cookbook", 751, "Pizza Chef", "Non-fiction")); BookshelfFilestore bfs = new BookshelfFilestore(list); bfs.saveBookshelf(); List list2 = bfs.loadBookshelf(); for (Book book : list2) { System.out.println(book.displayDetails() + "\n"); } } }