brainfrz

Untitled

Oct 23rd, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.60 KB | None | 0 0
  1. /**************************************************************************************************
  2.  *  Program Name:     Programming Project 5.7: Bookshelf
  3.  *  Class Name:       BookshelfFilestore
  4.  *  Author:           Terry Weiss
  5.  *  Date Written:     October 15, 2015
  6.  *  Program Description:
  7.  *     This class loads and saves all instance data for a book that will be used in `Bookshelf`.
  8.  **************************************************************************************************/
  9. package pp5_7;
  10.  
  11. //import Book
  12. //import Bookshelf
  13. import java.io.File;
  14. import java.io.FileNotFoundException;
  15. import java.io.FileWriter;
  16. import java.io.IOException;
  17. import java.io.PrintWriter;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Scanner;
  21.  
  22.  
  23. /**
  24.  *
  25.  * @author Terry
  26.  */
  27. public final class BookshelfFilestore {
  28.     private static final String DEFAULT_SHELF_FILENAME = "shelf.bl";
  29.     private static File shelfFile;
  30.     private static PrintWriter writer;
  31.     private static List<Book> shelf;
  32.  
  33.  
  34.     BookshelfFilestore() throws IOException, FileNotFoundException {
  35.         shelfFile = new File(DEFAULT_SHELF_FILENAME);
  36.         writer = new PrintWriter(new FileWriter(DEFAULT_SHELF_FILENAME));
  37.         shelf = new ArrayList<>();
  38.     }
  39.  
  40.     BookshelfFilestore( String filename ) throws IOException, FileNotFoundException {
  41.         shelfFile = new File(filename);
  42.         writer = new PrintWriter(new FileWriter(filename));
  43.         shelf = new ArrayList<>();
  44.  
  45.         if (shelfFile.exists()) {
  46.             loadBookshelf();
  47.         }
  48.     }
  49.  
  50.     BookshelfFilestore( List<Book> booklist ) throws IOException, FileNotFoundException {
  51.         shelfFile = new File(DEFAULT_SHELF_FILENAME);
  52.         writer = new PrintWriter(new FileWriter(DEFAULT_SHELF_FILENAME));
  53.         shelf = booklist;
  54.     }
  55.  
  56.     BookshelfFilestore( List<Book> booklist, String filename )
  57.         throws IOException, FileNotFoundException
  58.     {
  59.         shelfFile = new File(filename);
  60.         writer = new PrintWriter(new FileWriter(filename));
  61.         shelf = booklist;
  62.  
  63.         if (shelfFile.exists()) {
  64.             saveBookshelf();
  65.         }
  66.     }
  67.  
  68.     BookshelfFilestore( Bookshelf bookshelf ) throws IOException, FileNotFoundException {
  69.         shelfFile = new File(bookshelf.getName() + ".bl");
  70.         writer = new PrintWriter(new FileWriter(bookshelf.getName() + ".bl"));
  71.         shelf = bookshelf.getBookList();
  72.     }
  73.  
  74.     BookshelfFilestore( Bookshelf bookshelf, String filename )
  75.         throws IOException, FileNotFoundException
  76.     {
  77.         shelfFile = new File(filename);
  78.         writer = new PrintWriter(new FileWriter(filename));
  79.         shelf = bookshelf.getBookList();
  80.  
  81.         if (shelfFile.exists()) {
  82.             saveBookshelf();
  83.         }
  84.     }
  85.  
  86.  
  87.  
  88.  
  89.     /**
  90.      * Updates the working file using the specified filename.
  91.      *
  92.      * @param filename Relative path of new file to be used
  93.      * @return <tt>true</tt> if new file currently exists
  94.      * @throws IOException
  95.      */
  96.     public boolean useFile( String filename ) throws IOException {
  97.         shelfFile = new File(filename);
  98.  
  99.         return shelfFile.exists();
  100.     }
  101.  
  102.  
  103.  
  104.  
  105.     /**
  106.      * Writes all {@link pp5_7.Book}s to the current file.
  107.      *
  108.      * @return <tt>true</tt> if any books were saved
  109.      */
  110.     public boolean saveBookshelf() {
  111.         if (shelf.isEmpty()) {
  112.             return false;
  113.         }
  114.         else {
  115.             for (Book book : shelf) {
  116.                 writer.write(book.getTitle() + "\n");
  117.                 writer.write(book.getPages() + "\n");
  118.                 writer.write(book.getAuthor() + "\n");
  119.                 writer.write(book.getGenre() + "\n");
  120.                 writer.write("\n");
  121.                 writer.flush();
  122.             }
  123.  
  124.             return true;
  125.         }
  126.     }
  127.  
  128.  
  129.     /**
  130.      * Replaces current booklist with all the {@link pp5_7.Book}s from the current working file.
  131.      *
  132.      * @return List of Books loaded
  133.      * @throws FileNotFoundException
  134.      */
  135.     public List<Book> loadBookshelf() throws FileNotFoundException {
  136.         Scanner fileReader = new Scanner(shelfFile);
  137.         fileReader.useDelimiter("\n\n");
  138.  
  139.         shelf.clear();
  140.         while (fileReader.hasNext()) {
  141.             String bookStr = fileReader.next();
  142.             Scanner bookReader = new Scanner(bookStr);
  143.             String title, author, genre;
  144.             int pages;
  145.  
  146.             // For each book in the file's list
  147.             for (int i = 0; bookReader.hasNext(); i++) {
  148.                 title  = bookReader.nextLine();
  149.                 pages  = Integer.parseInt(bookReader.nextLine());
  150.                 author = bookReader.nextLine();
  151.                 genre  = bookReader.nextLine();
  152.  
  153.                 shelf.add(new Book(title, pages, author, genre));
  154.             }
  155.         }
  156.  
  157.         return shelf;
  158.     }
  159.  
  160.  
  161.     public static void main( String[] args ) throws IOException
  162.     {
  163.         List<Book> list = new ArrayList<>();
  164.  
  165.         list.add(new Book("Pizza Cookbook", 751, "Pizza Chef", "Non-fiction"));
  166.         list.add(new Book("Space Cowboys", 152, "Somebody Cool", "Sci-Fi"));
  167.         list.add(new Book("Space Cadets", 852, "Somebody Cool", "History"));
  168.         list.add(new Book("Cable News", 659, "Johny", "Non-fiction"));
  169.         list.add(new Book("Pizza Cookbook", 751, "Pizza Chef", "Non-fiction"));
  170.  
  171.  
  172.         BookshelfFilestore bfs = new BookshelfFilestore(list);
  173.         bfs.saveBookshelf();
  174.  
  175.  
  176.         List<Book> list2 = bfs.loadBookshelf();
  177.  
  178.         for (Book book : list2) {
  179.             System.out.println(book.displayDetails() + "\n");
  180.         }
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment