Hi, I'm new in this forum and I'm here to ask help on a topic the lecture I didn't turn up. I'm new to creating a search function for my Java application where the user can view the information of the books, such as title, author and the price of the book. The following below is the code I came up with:

- Book, the class to represent a book.

package JPRG;
public class Book {
private String title;
private String author;
private double price;

public Book(String Title,String Author, double Price){
title = Title;
author = Author;
price = Price;
}
public String getTitle(){
return title;
}
public String getAuthor(){
return author;
}
public double getPrice(){
return price;
}}

- BookCollection, the class to represent the collection of the books. This is where I want to include the search function in.

package JPRG;
public class BookCollection {
private Book[] books = new Book[9];
private String [] title = {"The Java Tutorial 4th Edition","Core Java Red","Core Java Blue","Java Generics","JasperReports for Java Developers","Java Development with ANT","Java in a Nutshell","Java Program and Progress","Java Network Programming"};
private String [] author = {"Sharon Zakhour,Scott Hommel,Jacob Royal,Isaac Rabinovitch,Tom Risser,Mark Hoeber","Cay S. Horstmann, Gary Cornell","Cay S. Horstmann, Gary Cornell","Maurice Naftalin,Filid Wadler","David R. Heffelflinger","Erik Hatcher and Steve Loughran","David Flanagan","Mahmood Shanbedi","O'Reilly"};
private double [] price = {36.95,3.45,6.50,27.95,38.60,34.95,44.95,29.95,48.50};
}

- BookFrame, the class to represents the GUI of the application.

package JPRG;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BookFrame extends JFrame implements ActionListener{
private JLabel welcomeLbl,titleLbl,priceLbl,authorLbl;
private JButton btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,searchbtn,previousbtn,nextbtn,exitbtn;
private JTextField titleTxt,authorTxt,priceTxt,searchTxt;
private JPanel northPanel,centerPanel,southPanel,titlepanel;
private ImageIcon image0,image1,image2,image3,image4,image5,image6,image7,image8;
private int count = 1;

public BookFrame(){
northPanel = new JPanel();
welcomeLbl = new JLabel("DMIT Java Mini Book Shop");
welcomeLbl.setForeground(Color.red);
welcomeLbl.setFont(new Font("arial", Font.BOLD, 28));
northPanel.add(welcomeLbl);
add(northPanel,BorderLayout.NORTH);

image0 = new ImageIcon("javaimage/image0.jpg");
image1 = new ImageIcon("javaimage/image1.jpg");
image2 = new ImageIcon("javaimage/image2.jpg");
image3 = new ImageIcon("javaimage/image3.jpg");
image4 = new ImageIcon("javaimage/image4.jpg");
image5 = new ImageIcon("javaimage/image5.jpg");
image6 = new ImageIcon("javaimage/image6.jpg");
image7 = new ImageIcon("javaimage/image7.jpg");
image8 = new ImageIcon("javaimage/image8.jpg");

centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(3,3,0,0));
btn1 = new JButton(image0);
btn1.setBackground(Color.orange);
btn2 = new JButton(image1);
btn3 = new JButton(image2);
btn4 = new JButton(image3);
btn5 = new JButton(image4);
btn6 = new JButton(image5);
btn7 = new JButton(image6);
btn8 = new JButton(image7);
btn9 = new JButton(image8);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
centerPanel.add(btn1);
centerPanel.add(btn2);
centerPanel.add(btn3);
centerPanel.add(btn4);
centerPanel.add(btn5);
centerPanel.add(btn6);
centerPanel.add(btn7);
centerPanel.add(btn8);
centerPanel.add(btn9);
add(centerPanel,BorderLayout.CENTER);

southPanel = new JPanel();
southPanel.setBorder(BorderFactory.createTitledBorder("Details"));
southPanel.setLayout(new GridLayout(3, 1, 3, 1));
titleLbl = new JLabel("Title:");
titleTxt = new JTextField(28);
titleTxt.setEditable(false);
authorLbl = new JLabel("Author:");
authorTxt = new JTextField(28);
authorTxt.setEditable(false);
priceLbl = new JLabel("Price:");
priceTxt = new JTextField(28);
priceTxt.setEditable(false);
searchbtn = new JButton("Search");
searchbtn.setBackground(Color.yellow);
searchTxt = new JTextField(28);
previousbtn = new JButton("Previous");
previousbtn.setBackground(Color.cyan);
nextbtn = new JButton("Next");
nextbtn.setBackground(Color.cyan);
exitbtn = new JButton("Exit");
exitbtn.setBackground(Color.red);
searchbtn.addActionListener(this);
nextbtn.addActionListener(this);//registering the button for event
previousbtn.addActionListener(this);
exitbtn.addActionListener(this);
southPanel.add(titleLbl);
southPanel.add(titleTxt);
southPanel.add(authorLbl);
southPanel.add(authorTxt);
southPanel.add(priceLbl);
southPanel.add(priceTxt);
southPanel.add(searchbtn);
southPanel.add(searchTxt);
southPanel.add(previousbtn);
southPanel.add(nextbtn);
southPanel.add(exitbtn);
add(southPanel,BorderLayout.SOUTH);


}
public void actionPerformed(ActionEvent e){
if(e.getSource() == btn1){
count = 1;
btn1.setBackground(Color.orange);
btn2.setBackground(null);
btn3.setBackground(null);
btn4.setBackground(null);
btn5.setBackground(null);
btn6.setBackground(null);
btn7.setBackground(null);
btn8.setBackground(null);
btn9.setBackground(null);
}
if(e.getSource() == btn2){
count = 2;
btn2.setBackground(Color.orange);
btn1.setBackground(null);
btn3.setBackground(null);
btn4.setBackground(null);
btn5.setBackground(null);
btn6.setBackground(null);
btn7.setBackground(null);
btn8.setBackground(null);
btn9.setBackground(null);
}
if(e.getSource() == btn3){
count = 3;
btn3.setBackground(Color.orange);
btn1.setBackground(null);
btn2.setBackground(null);
btn4.setBackground(null);
btn5.setBackground(null);
btn6.setBackground(null);
btn7.setBackground(null);
btn8.setBackground(null);
btn9.setBackground(null);
}
if(e.getSource() == btn4){
count = 4;
btn4.setBackground(Color.orange);
btn1.setBackground(null);
btn2.setBackground(null);
btn3.setBackground(null);
btn5.setBackground(null);
btn6.setBackground(null);
btn7.setBackground(null);
btn8.setBackground(null);
btn9.setBackground(null);
}
if(e.getSource() == btn5){
count = 5;
btn5.setBackground(Color.orange);
btn1.setBackground(null);
btn2.setBackground(null);
btn3.setBackground(null);
btn4.setBackground(null);
btn6.setBackground(null);
btn7.setBackground(null);
btn8.setBackground(null);
btn9.setBackground(null);
}
if(e.getSource() == btn6){
count = 6;
btn6.setBackground(Color.orange);
btn1.setBackground(null);
btn2.setBackground(null);
btn3.setBackground(null);
btn4.setBackground(null);
btn5.setBackground(null);
btn7.setBackground(null);
btn8.setBackground(null);
btn9.setBackground(null);
}
if(e.getSource() == btn7){
count = 7;
btn7.setBackground(Color.orange);
btn1.setBackground(null);
btn2.setBackground(null);
btn3.setBackground(null);
btn4.setBackground(null);
btn5.setBackground(null);
btn6.setBackground(null);
btn8.setBackground(null);
btn9.setBackground(null);
}
if(e.getSource() == btn8){
count = 8;
btn8.setBackground(Color.orange);
btn1.setBackground(null);
btn2.setBackground(null);
btn3.setBackground(null);
btn4.setBackground(null);
btn5.setBackground(null);
btn6.setBackground(null);
btn7.setBackground(null);
btn9.setBackground(null);
}
if(e.getSource() == btn9){
count = 9;
btn9.setBackground(Color.orange);
btn1.setBackground(null);
btn2.setBackground(null);
btn3.setBackground(null);
btn4.setBackground(null);
btn5.setBackground(null);
btn6.setBackground(null);
btn7.setBackground(null);
btn8.setBackground(null);
}
if (e.getSource() == nextbtn) {
if(count <9)
count++;
}
if (e.getSource() == previousbtn ) {
if (count >1)
count--;
}
switch(count)
{
case 1:btn1.setBackground(Color.orange);
btn2.setBackground(null);
break;
case 2:btn2.setBackground(Color.orange);
btn1.setBackground(null);
btn3.setBackground(null);
break;
case 3:btn3.setBackground(Color.orange);
btn2.setBackground(null);
btn4.setBackground(null);
break;
case 4:btn4.setBackground(Color.orange);
btn3.setBackground(null);
btn5.setBackground(null);
break;
case 5:btn5.setBackground(Color.orange);
btn4.setBackground(null);
btn6.setBackground(null);
break;
case 6:btn6.setBackground(Color.orange);
btn5.setBackground(null);
btn7.setBackground(null);
break;
case 7:btn7.setBackground(Color.orange);
btn6.setBackground(null);
btn8.setBackground(null);
break;
case 8:btn8.setBackground(Color.orange);
btn7.setBackground(null);
btn9.setBackground(null);
break;
case 9:btn9.setBackground(Color.orange);
btn8.setBackground(null);
break;
}
if(e.getSource() == exitbtn){
System.exit(1);
}}}

- BookFrameUser, the class to run the application, for those who wish to test-run their code.

package JPRG;
import javax.swing.*;
public class BookFrameUser {
    public static void main(String [] args){
        BookFrame f = new BookFrame();
        f.setTitle("Tic Tac Toe Game");
        f.setSize(400,550);
        f.setVisible(true);
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}

That's all for the coding. Thanks!

Recommended Answers

All 6 Replies

Your book collection is wrong

package JPRG;
public class BookCollection {
private Book[] books = new Book[9];
private String [] title = {"The Java Tutorial 4th Edition","Core Java Red","Core Java Blue","Java Generics","JasperReports for Java Developers","Java Development with ANT","Java in a Nutshell","Java Program and Progress","Java Network Programming"};
private String [] author = {"Sharon Zakhour,Scott Hommel,Jacob Royal,Isaac Rabinovitch,Tom Risser,Mark Hoeber","Cay S. Horstmann, Gary Cornell","Cay S. Horstmann, Gary Cornell","Maurice Naftalin,Filid Wadler","David R. Heffelflinger","Erik Hatcher and Steve Loughran","David Flanagan","Mahmood Shanbedi","O'Reilly"};
private double [] price = {36.95,3.45,6.50,27.95,38.60,34.95,44.95,29.95,48.50};
}

You been supposed to do something like this

private Book[] books = new Book[9];
books[0] =new Book("The Java Tutorial 4th Edition", "Sharon Zakhour,Scott Hommel,Jacob Royal,Isaac Rabinovitch,Tom Risser,Mark Hoeber","Cay S. Horstmann, Gary Cornell",36.95 );

Then you can implement Comparator on top of book like in this example

Oh... but if I did what you suggest, how do I classify the information of the searched result into the 3 respective text field of Title, Author and Price?

*Sorry, my English is poor*

Anyway I'm still stuck on the search function, beginning from parsing what is typed in the SearchTxt, comparing result and finally input the information of the result into the 3 information field as mentioned above.

Using comparable with allow you to search by specific field without need of for/while/do-while loops.
Try-out linked example and try to understand what is going there...

I re-did my book collection class as of the following:

Book book[] = new Book[8];

        book[0] = new Book();
        book[0].setPrice(36.95);
        book[0].setTitle("The Java Tutorial 4th Edition");
        book[0].setAuthor("Sharon Zakhour,Scott Hommel,Jacob Royal,Isaac Rabinovitch,Tom Risser,Mark Hoeber");

        book[1] = new Book();
        book[1].setPrice(3.45);
        book[1].setTitle("Core Java Red");
        book[1].setAuthor("Cay S. Horstmann, Gary Cornell");
 
        book[2] = new Book();
        book[2].setPrice(6.50);
        book[2].setTitle("Core Java Blue");
        book[2].setAuthor("Cay S. Horstmann, Gary Cornell");

        book[3] = new Book();
        book[3].setPrice(27.95);
        book[3].setTitle("Java Generics");
        book[3].setAuthor("Maurice Naftalin,Filid Wadler");
 
        book[4] = new Book();
        book[4].setPrice(38.60);
        book[4].setTitle("JasperReports for Java Developers");
        book[4].setAuthor("David R. Heffelflinger");

        book[5] = new Book();
        book[5].setPrice(34.95);
        book[5].setTitle("Java Development with AN");
        book[5].setAuthor("Erik Hatcher and Steve Loughran");
 
        book[6] = new Book();
        book[6].setPrice(44.95);
        book[6].setTitle("Java Program and Progress");
        book[6].setAuthor("David Flanagan");

        book[7] = new Book();
        book[7].setPrice(3.45);
        book[7].setTitle("Core Java Red");
        book[7].setAuthor("Mahmood Shanbedi");
 
        book[8] = new Book();
        book[8].setPrice(48.50);
        book[8].setTitle("Java Network Programming");
        book[8].setAuthor("O'Reilly");

But still, I've no idea on how to extract the user's search input and compare it with the book's title as I'm new in this topic. All I could begin is :

if (e.getSource() == searchbtn) {

Can anyone teach me step by step on how to do starting from extracting the user's input?

Why do you insist on creating Book object through setter methods? You created that Book contractor for a reason so use it!
I did not run your code, but all you have to do on submit button get value from search field that can be something like String search = textField.getText()

If you end up having a lot of items to search over you may want to look at an Inverted Index which is specifically designed for high speed searching of large collections (:

(very-basic) you create an index of all words from your documents, with each word is a table that defines where you will find the word (what document) and the position of the word in the document.

Example from the wiki:

"a": {(2, 2)}
"banana": {(2, 3)}
"is": {(0, 1), (0, 4), (1, 1), (2, 1)}
"it": {(0, 0), (0, 3), (1, 2), (2, 0)}
"what": {(0, 2), (1, 0)
If we run a phrase search for "what is it" we get hits for all the words in both document 0 and 1. But the terms occur consecutively only in document 1.

Using Mapping and Reducing you can create a means by which you can return the most pertinent information first. AKA for each word search you distribute the work load across several asynchronous threads which comb through the inverted index looking for the words and locations.

You can then compile the output and determine exactly where a word is and in which places all of the words appear together or at least a large portion of them :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.