brainfrz

Untitled

Oct 20th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. public class BookshelfFilestore {
  2.     private static final String DEFAULT_SHELF_FILE = "shelf.txt";
  3.  
  4.     public static void main( String[] args ) throws IOException
  5.     {
  6.         File shelfFile = new File(DEFAULT_SHELF_FILE);
  7.         PrintWriter writer = new PrintWriter(DEFAULT_SHELF_FILE);
  8.         Bookshelf shelf = new Bookshelf();
  9.  
  10.         shelf.addBook(new Book("Pizza Cookbook", 751, "Pizza Chef", "Non-fiction"));
  11.         shelf.addBook(new Book("Space Cowboys", 152, "Somebody Cool", "Sci-Fi"));
  12.         shelf.addBook(new Book("Space Cadets", 852, "Somebody Cool", "Sci-Fi"));
  13.         shelf.addBook(new Book("Cable News", 659, "Johny", "Non-fiction"));
  14.         shelf.addBook(new Book("Pizza Cookbook", 751, "Pizza Chef", "Non-fiction"));
  15.  
  16.         for (Book book : shelf.getBookshelf()) {
  17.             writer.write(book.getTitle() + "\n");
  18.             writer.write(book.getPages() + "\n");
  19.             writer.write(book.getAuthor() + "\n");
  20.             writer.write(book.getGenre() + "\n");
  21.             writer.write("\n");
  22.             writer.flush();
  23.         }
  24.  
  25.  
  26.         Bookshelf shelf2 = new Bookshelf();
  27.         Scanner fileReader = new Scanner(shelfFile);
  28.         fileReader.useDelimiter("\n\n");
  29.         while (fileReader.hasNext()) {
  30.             String bookStr = fileReader.next();
  31.             Scanner bookReader = new Scanner(bookStr);
  32.             String title, author, genre;
  33.             int pages;
  34.  
  35.             // For each book in the file's list
  36.             for (int i = 0; bookReader.hasNext(); i++) {
  37.                 title  = bookReader.nextLine();
  38.                 pages  = Integer.parseInt(bookReader.nextLine());
  39.                 author = bookReader.nextLine();
  40.                 genre  = bookReader.nextLine();
  41.  
  42.                 shelf2.addBook(new Book(title, pages, author, genre));
  43.             }
  44.         }
  45.  
  46.         for (Book book : shelf2.getBookshelf()) {
  47.             System.out.println(book.displayDetails() + "\n");
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment