- import java.io.*;
- /**
- *
- * This class is the bookshelf. It carries all the properties that allow you to
- * add, remove or find a book that is created and added to this shelf.
- *
- */
- public class BookShelf
- {
- // new book type array called shelf with 200 spots.
- private Book[] shelf = new Book[200];
- /**
- * Adds a book to the array.
- *
- * @param abook this is the variable of type book being added.
- */
- public void add(Book aBook)
- {
- // this is the for loop that runs through the array and checks for empty spots.
- for(int i = 0; i<shelf.length; i++)
- {
- if(shelf[i] == null)
- {
- shelf[i] = aBook;
- break;
- }
- }
- }
- /**
- * Removes a book from the array.
- *
- * @param abook this is the variable of type book being removed.
- */
- public void remove(Book aBook)
- {
- // this for loop finds non empty spots in the array and removes them if they are a match.
- for(int i = 0; i<shelf.length; i++)
- {
- if(aBook == shelf[i])
- {
- shelf[i] = null;
- break;
- }
- }
- }
- /**
- * Finds a book in the array.
- *
- * @param abook this is the variable of type book being added.
- * @return returns the book
- */
- public Book find(String search)
- {
- // 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.
- for(int i = 0; i<shelf.length; i++)
- {
- if(shelf[i] != null && search.equals (shelf[i].getBookTitle()))
- {
- return(shelf[i]);
- }
- }
- return(null);
- }
- public void writetoFile()
- {
- for(int i=0;i<shelf.length;i++)
- {
- try
- {
- FileWriter write = new FileWriter ("books.txt");
- PrintWriter out = new PrintWriter("books.txt");
- out.println(abook.getBookTitle(shelf[i]));
- out.println(abook.getbookAuthor(shelf[i]));
- out.println(currency.format(abook.getBookPrice(shelf[i])));
- out.println(abook.getBookPublisher(shelf[i]));
- out.println(formatter.format(abook.getBookCopyright(shelf[i])));
- out.println("");
- out.close();
- }
- catch ( Exception e)
- {
- e.printStackTrace();
- }
- }// end for
- }
- }
bookshelf
By: microhaxo | Date: Apr 11 2009 20:00 | Format: None | Expires: never | Size: 2.16 KB | Hits: 976
Latest pastes
1 hours ago
11 hours ago
1 days ago
2 days ago
2 days ago