Public paste
BookShelf-new
By: microhaxo | Date: Apr 11 2009 20:56 | Format: None | Expires: never | Size: 2.42 KB | Hits: 880

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