-2

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

1

1 Answer 1

-1

This sounds like homework. You will want to do something like this:

public String[] titleSearch(String keyword) {
        for(int i = 0; i < books.length; i++){
            if(books[i].contains(keyword)){
                //Add matching books to some sort of list
            }
        }
        //Sort your new list
        return //sorted list;
    }

I think you would be better off storing your data in an ArrayList instead of an array of books[], if you did that you could implement comepareTo and make the final sorting step above trivial... take a look at this article here.

6
  • 1
    Why did you just give them a code sample simply doing the work for them? Please also have a look at How do I ask and answer Homework questions. Providing an answer that doesn't help a student learn is not in the student's own best interest. Therefore you might choose to treat homework questions differently than other questions. Nov 17, 2019 at 18:02
  • I did not answer their entire question. I also pointed them to a learning resource which will help them further understand the more difficult part of their question. Why are you complaining about the answer I give instead of trying to help the student?
    – Jacob B
    Nov 17, 2019 at 18:07
  • 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 But why do you provide a code sample for a question like this? They don't show any attempt to do it. Nov 17, 2019 at 18:11
  • if i didnt try a bunch of searches and ressources and if it was not due today lol i would not have even asked lol but yea thanks for the guidance jacob Nov 17, 2019 at 18:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.