I dont know how to search by keywords after some research im guessing i have to use the contain method but i dont know how thanks
import javax.swing.JOptionPane;
/**
* A collection of {@link Book}.
*/
public class BookStorage {
private Book[] books = new Book[100];
public BookStorage() {
}
/**
* Initializes the book storage with some arbitrary book objects.
*/
public void initBooks() {
books[0] = new Book("Crickey", "Mahmoud", 200, Book.BookCategory.Design );
books[1] = new Book("Thinking in Java", "Bruce Eckel", 1520, Book.BookCategory.Programming );
books[2] = new Book("Easy Life", "Jeff Bezos", 10, Book.BookCategory.Database );
books[3] = new Book("Soccer Star", "Ronaldo", 1000, Book.BookCategory.Design );
books[4] = new Book("Hells Kitchen", "Gordon Ramsay", 500, Book.BookCategory.Programming );
books[5] = new Book("York Itec", "John", 1600, Book.BookCategory.Database );
books[6] = new Book("Gaming Designer", "Rebecca", 1100, Book.BookCategory.Design );
books[7] = new Book("Computer Science", "Mahmoud", 200, Book.BookCategory.Programming );
books[8] = new Book("Google", "Jason", 333, Book.BookCategory.Database );
books[9] = new Book("York Soccer", "Carmine", 777, Book.BookCategory.Design );
}
/**
* Uses the given book to update the existing book with the same title.
*/
public void update(Book book) {
for(int i = 0; i<books.length;i++) {
if(books[i].getTitle().equals(book.getTitle())) {
books[i].setAuthors(book.getAuthors());
books[i].setPages(book.getPages());
books[i].setCategory(book.getCategory());
}
}
}
/**
* Removes a book by title.
*/
public void remove(String bookTitle) {
// TODO Add your code here...
for(int i=0; i<books.length;i++) {
if(books[i].getTitle().equals(bookTitle)){
books[i]= null;
}
}
}
/**
* Adds a new book.
*/
public void add(Book book) {
// TODO Add your code here...
int index = 0;
int counter = 0;
boolean invalidArg;
if(books[index].getTitle().equals(book.getTitle())) {
invalidArg = false;
}
else {
invalidArg = true;
}
if (!invalidArg) {
JOptionPane.showMessageDialog(null, "The book has been added");
}
else {
int tempCounter = counter;
for (int i = 0; i<tempCounter+1;i++) {
if (books[i] !=null) {
continue;
}else {
books[i] = book;
}
}
}
}
/**
* Gets a book by title.
*/
public Book getByTitle(String title) {
// TODO Add your code here...
for(int i=0; i<books.length;i++) {
if(books[i].getTitle() == title){
return books[i] ;
}
}
return null;
}
/**
* Searches for books whose title contains the keyword and returns them ordered by titles (in alphabet order).
*/
public Book[] titleSearch(String keyword) {
// TODO Add your code here...
return new Book[0];
}
/**
* Returns all books sorted by their titles (in alphabet order).
*/
public Book[] getAll() {
// TODO Add your code here...
return new Book[0];
}
/**
* Sorts an array of books by their titles in alphabet order.
*/
private Book[] sortByTitle(Book[] bookArray) {
// TODO Add your code here...
Book temp;
for (int i = 0; i < books.length; i++)
{
for (int j = i + 1; j < books.length; j++)
{
if (books[i].getTitle().compareTo(books[j].getTitle())>0)
{
temp = books[i];
books[i] = books[j];
books[j] = temp;
}
}
}
return bookArray;
}
}
the code im looking for help is the titlesearch() the rest is for another time haha the goal is to search on the textfield a title and it will appear on the list and i will call this method to provide it