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!