- import java.io.*;
- import java.text.SimpleDateFormat;
- import java.text.NumberFormat;
- /**
- *
- * 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()
- {
- System.out.println(shelf[0].getBookTitle());
- /* for(int i = 0; i<shelf.length; i++)
- {
- try
- {
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
- NumberFormat currency = NumberFormat.getCurrencyInstance();
- FileWriter write = new FileWriter ("books.txt", true);
- //PrintWriter out = new PrintWriter("books.txt");
- write.write(shelf[i].getBookTitle());
- write.write(shelf[i].getBookAuthor());
- write.write(currency.format(shelf[i].getBookPrice()));
- write.write(shelf[i].getBookPublisher());
- write.write(formatter.format(shelf[i].getCopyRight()));
- write.write("");
- write.flush();
- write.close();
- }
- catch ( Exception e)
- {
- e.printStackTrace();
- }
- *///}// end for
- }
- }
BookShelf-new
By: microhaxo | Date: Apr 11 2009 20:56 | Format: None | Expires: never | Size: 2.42 KB | Hits: 972
Latest pastes
1 hours ago
11 hours ago
1 days ago
2 days ago
2 days ago