Public paste
bookshelf
By: microhaxo | Date: Apr 11 2009 20:00 | Format: None | Expires: never | Size: 2.16 KB | Hits: 891

  1. import java.io.*;
  2. /**
  3.  *
  4.  * This class is the bookshelf. It carries all the properties that allow you to
  5.  * add, remove or find a book that is created and added to this shelf.
  6.  *
  7.  */
  8.  
  9. public class BookShelf
  10. {
  11.         // new book type array called shelf with 200 spots.
  12.         private Book[] shelf = new Book[200];  
  13.        
  14.          /**
  15.      * Adds a book to the array.
  16.      *
  17.      * @param abook  this is the variable of type book being added.
  18.      */
  19.         public void add(Book aBook)
  20.         {
  21.                 // this is the for loop that runs through the array and checks for empty spots.
  22.                 for(int i = 0; i<shelf.length; i++)
  23.                 {
  24.                         if(shelf[i] == null)
  25.                         {
  26.                                 shelf[i] = aBook;
  27.                                 break;
  28.                         }      
  29.                 }
  30.         }      
  31.          /**
  32.      * Removes a book from the array.
  33.      *
  34.      * @param abook  this is the variable of type book being removed.
  35.      */
  36.  
  37.         public void remove(Book aBook)
  38.         {
  39.                 // this for loop finds non empty spots in the array and removes them if they are a match.
  40.                 for(int i = 0; i<shelf.length; i++)
  41.                 {
  42.                         if(aBook == shelf[i])
  43.                         {
  44.                                 shelf[i] = null;
  45.                                 break;
  46.                         }
  47.                 }
  48.         }
  49.          /**
  50.      * Finds a book in the array.
  51.      *
  52.      * @param abook  this is the variable of type book being added.
  53.           * @return returns the book
  54.      */        
  55.         public Book find(String search)
  56.         {
  57.                 // this for loop searches the array and matches the user input to the title, if it is a match it returns that section of the array.
  58.                 for(int i = 0; i<shelf.length; i++)
  59.                 {
  60.                         if(shelf[i] != null && search.equals (shelf[i].getBookTitle()))
  61.                         {
  62.                                 return(shelf[i]);
  63.                         }      
  64.                 }
  65.                 return(null);
  66.         }              
  67.        
  68.         public void writetoFile()
  69.         {
  70.                 for(int i=0;i<shelf.length;i++)
  71.                 {
  72.                         try
  73.                                 {
  74.                                         FileWriter write = new FileWriter ("books.txt");
  75.                                         PrintWriter out = new PrintWriter("books.txt");
  76.                                         out.println(abook.getBookTitle(shelf[i]));
  77.                                         out.println(abook.getbookAuthor(shelf[i]));
  78.                                         out.println(currency.format(abook.getBookPrice(shelf[i])));
  79.                                         out.println(abook.getBookPublisher(shelf[i]));
  80.                                         out.println(formatter.format(abook.getBookCopyright(shelf[i])));
  81.                                         out.println("");
  82.                                         out.close();
  83.                                 }
  84.                                 catch ( Exception e)
  85.                                 {
  86.                                         e.printStackTrace();
  87.                                 }
  88.                 }// end for
  89.         }
  90. }