HI ! could you please help me here? i'm having a problem on marking the books in my library program if it is borrowed or returned? here's where im at,

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.Color;
public class Library
{

    ArrayList<String> bookName = new ArrayList<String>();
    {{
        bookName.add("Hunger Games");
        bookName.add("Catching Fire");
        bookName.add("Mocking Jay");
        bookName.add("The Da Vinci Code");
        bookName.add("Harry Potter and The Sorcerer's Stone");
        bookName.add("Harry Potter and The Chamber of Secrets");
        bookName.add("Harry Potter and The Prisoner of Azkaban");
        bookName.add("Harry Potter and The Goblet of Fire");
        bookName.add("Harry Potter and The Order of The Phoenix");
        bookName.add("Harry Potter and The Half-blood Prince");
        bookName.add("Harry Potter and The Deathly Hallows");   
    }}

    ArrayList<String> bookAuthor = new ArrayList<String>();
    {{
        bookAuthor.add("Suzanne Collins");
        bookAuthor.add("Suzanne Collins");
        bookAuthor.add("Suzanne Collins");
        bookAuthor.add("Dan Brown");
        bookAuthor.add("J.K. Rowling");
        bookAuthor.add("J.K. Rowling");
        bookAuthor.add("J.K. Rowling");
        bookAuthor.add("J.K. Rowling");
        bookAuthor.add("J.K. Rowling");
        bookAuthor.add("J.K. Rowling");
        bookAuthor.add("J.K. Rowling");
    }}

    ArrayList<String> bookPublisher = new ArrayList<String>();
    {{
        bookPublisher.add("Scholastic Press");
        bookPublisher.add("Scholastic Press");
        bookPublisher.add("Scholastic Press");
        bookPublisher.add("Doubleday Group");
        bookPublisher.add("Arthur A. Levine Books");
        bookPublisher.add("Arthur A. Levine Books");
        bookPublisher.add("Arthur A. Levine Books");
        bookPublisher.add("Arthur A. Levine Books");
        bookPublisher.add("Arthur A. Levine Books");
        bookPublisher.add("Arthur A. Levine Books");
        bookPublisher.add("Arthur A. Levine Books");
    }}

    ArrayList<Integer> bookID = new ArrayList<Integer>();
    {{
        bookID.add(1001);
        bookID.add(1002);
        bookID.add(1003);
        bookID.add(1004);
        bookID.add(1005);
        bookID.add(1006);
        bookID.add(1007);
        bookID.add(1008);
        bookID.add(1009);
        bookID.add(1010);
        bookID.add(1011);
    }}

    boolean borrowed;

    JFrame myFrame = new JFrame("Library");
    JLabel jLabel1 = new JLabel("***JhayJheDiegz Books***");
    JLabel jLabel2 = new JLabel("2nd Floor S-205, Science Centrum Building,");
    JLabel jLabel3 = new JLabel("Colegio de Dagupan, Arellano St. Dagupan City");
    JButton jButton1 = new JButton("Search");
    JButton jButton2 = new JButton("Rent");
    JButton jButton3 = new JButton("Return");
    JButton jButton4 = new JButton("About");
    JButton jButton5 = new JButton("Add Book To Our Library");
    FlowLayout layout = new FlowLayout(FlowLayout.CENTER);
    Font font = new Font("Serif", Font.BOLD, 20);


    private int pressed;


    public Library()
    {
        myFrame.setBounds(485,425, 305,200);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setVisible(true);
        myFrame.setLayout(layout);
        myFrame.setResizable(false);

        JPanel jPanel = new JPanel();

        jButton1.addActionListener(new searchButton());
        jButton2.addActionListener(new rentButton());
        jButton3.addActionListener(new returnButton());
        jButton4.addActionListener(new aboutButton());
        jButton5.addActionListener(new addButton());

        jButton1.setToolTipText("Search Book");
        jButton2.setToolTipText("Rent Book");
        jButton3.setToolTipText("Return Book");
        jButton4.setToolTipText("About Our Program");
        jButton5.setToolTipText("Add Book");

        myFrame.add(jLabel1);
        myFrame.add(jLabel2);
        myFrame.add(jLabel3);
        myFrame.add(jButton1);
        myFrame.add(jButton2);
        myFrame.add(jButton3);
        myFrame.add(jButton4);
        myFrame.add(jButton5);

        jLabel1.setFont(font);
        myFrame.getContentPane().setBackground(Color.LIGHT_GRAY);

    }


    public void searchBook()
    {       
        String input;
        int x;
        boolean found = false;

        input = JOptionPane.showInputDialog(null, "Enter Name: ");

        try
        {
        for(x=0;x<bookName.size();x++)
        {
            if(input.equalsIgnoreCase(bookName.get(x)))
            {
                JOptionPane.showMessageDialog(null, "Book Found !" + "\nTitle: " + bookName.get(x) + "\nAuthor: " + bookAuthor.get(x) + 
                    "\nPublisher: " + bookPublisher.get(x) + "\nISBN: " + bookID.get(x), "Book Search", JOptionPane.INFORMATION_MESSAGE);

                    found = true;
            }

        }

        if(!found)
        {
            JOptionPane.showMessageDialog(null, "BOOK NOT FOUND !" , "Book Search", JOptionPane.ERROR_MESSAGE);

            searchBook();
        }
        }

        catch(Exception e)
        {

        }

    }

    public void rentBook()
    {
        //this is where i want to put my rent book statements       
    }

    public void returnBook()
    {
        //this is where i want to put my return book statements
    }

    public void aboutBook()
    {
        try
        {

        JOptionPane.showMessageDialog(null, "Authors: " + "\nJeli Quides" + "\nEmmanuel Bulawan" + "\nJaymark Dacpano" + 
            "\n" + "\nWe made this program for our final requirement in our Java Programming 2, " + 
                "\nThis code is written in JCreator Pro from scratch, " + 
                    "\nIf you have any questions please approach the authors mentioned above. " + "\nThank You!");

        }

        catch(Exception e)
        {

        }
    }

    public void addBook()
    {
        try
        {
            String bN;
            String bA;
            String bP;
            int bI;

            bN = JOptionPane.showInputDialog(null, "Enter Book Title: ", "Add Book", JOptionPane.QUESTION_MESSAGE);

            bookName.add(bN);

            bA = JOptionPane.showInputDialog(null, "Enter Book Author: ", "Add Book", JOptionPane.QUESTION_MESSAGE);

            bookAuthor.add(bA);

            bP = JOptionPane.showInputDialog(null, "Enter Book Publisher: ", "Add Book", JOptionPane.QUESTION_MESSAGE);

            bookPublisher.add(bP);

            String a =JOptionPane.showInputDialog(null, "Enter Book ID: ", "Add Book", JOptionPane.QUESTION_MESSAGE);
            bI = Integer.parseInt(a);

            bookID.add(bI);

            JOptionPane.showMessageDialog(null, "Book Added ! Thank You ! ");

        }

        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Please Eneter Valid ISBN !" , "Invalid Book ISBN", JOptionPane.ERROR_MESSAGE);

            addBook();
        }
    }

    class searchButton implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            searchBook();
        }

    }

    class rentButton implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            rentBook();
        }

    }

    class returnButton implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            returnBook();
        }

    }

    class aboutButton implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            aboutBook();
        }
    }

    class addButton implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            addBook();
        }
    } 
}

class MainSystem
{

    public static void main(String[] args) 
    {   
        new Library();           
    }
}

Recommended Answers

All 9 Replies

You only seem to have one boolean for "borrowed" whereas you need one boolean per book. You could use another ArrayList, like for the names etc.
ps: In Java the "right" way to do this is to define a class called "Book" that has instance variables for name, borrowed etcetc, then just one ArrayList of Book objects.
You'll find that updating your books (especially to delete a book) is very tedious with your current data structure.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Jaymark
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.Color;
public class Book
{
    String title;
    boolean borrowed;

    public String Book(String bookTitle) 
    {
        title = bookTitle;
        borrowed = false;

        return title;
    }

    public void borrowed() 
    {
        borrowed = true;
    }


    public void returned() 
    {
        borrowed = false;
    }

    public boolean isBorrowed() 
    {
        return ((borrowed) ? true : false);
    }

    public String getTitle() 
    {
        return title;
    }
}
class Library
{
    // How Do i add books in my ArrayList?
    ArrayList<Book> bookName = new ArrayList<Book>();

    JFrame myFrame = new JFrame("Library");
    JLabel jLabel1 = new JLabel("***JhayJheDiegz Books***");
    JLabel jLabel2 = new JLabel("2nd Floor S-205, Science Centrum Building,");
    JLabel jLabel3 = new JLabel("Colegio de Dagupan, Arellano St. Dagupan City");
    JButton jButton1 = new JButton("Search");
    JButton jButton2 = new JButton("Rent");
    JButton jButton3 = new JButton("Return");
    JButton jButton4 = new JButton("About");
    JButton jButton5 = new JButton("Add Book To Our Library");
    FlowLayout layout = new FlowLayout(FlowLayout.CENTER);
    Font font = new Font("Serif", Font.BOLD, 20);


    private int pressed;


    public Library()
    {
        myFrame.setBounds(485,425, 305,200);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setVisible(true);
        myFrame.setLayout(layout);
        myFrame.setResizable(false);

        JPanel jPanel = new JPanel();

        jButton1.addActionListener(new searchButton());
        jButton2.addActionListener(new rentButton());
        jButton3.addActionListener(new returnButton());
        jButton4.addActionListener(new aboutButton());
        jButton5.addActionListener(new addButton());

        jButton1.setToolTipText("Search Book");
        jButton2.setToolTipText("Rent Book");
        jButton3.setToolTipText("Return Book");
        jButton4.setToolTipText("About Our Program");
        jButton5.setToolTipText("Add Book");

        myFrame.add(jLabel1);
        myFrame.add(jLabel2);
        myFrame.add(jLabel3);
        myFrame.add(jButton1);
        myFrame.add(jButton2);
        myFrame.add(jButton3);
        myFrame.add(jButton4);
        myFrame.add(jButton5);

        jLabel1.setFont(font);
        myFrame.getContentPane().setBackground(Color.LIGHT_GRAY);

    }


    public void searchBook()
    {       
        String input;
        int x;
        boolean found = false;

        input = JOptionPane.showInputDialog(null, "Enter Name: ");

        try
        {
        for(x=0;x<bookName.size();x++)
        {
            if(input.equalsIgnoreCase(bookName.get(x).toString()))
            {
                JOptionPane.showMessageDialog(null, "Book Found !" + "\nTitle: " + bookName.get(x).toString(), "Book Search", JOptionPane.INFORMATION_MESSAGE);

                    found = true;
            }

        }

        if(!found)
        {
            JOptionPane.showMessageDialog(null, "BOOK NOT FOUND !" , "Book Search", JOptionPane.ERROR_MESSAGE);

            searchBook();
        }
        }

        catch(Exception e)
        {

        }

    }

    public void rentBook()
    {

    }

    public void returnBook()
    {

    }

    public void aboutBook()
    {
        try
        {

        JOptionPane.showMessageDialog(null, "Authors: " + "\nJeli Quides" + "\nEmmanuel Bulawan" + "\nJaymark Dacpano" + 
            "\n" + "\nWe made this program for our final requirement in our Java Programming 2, " + 
                "\nThis code is written in JCreator Pro from scratch, " + 
                    "\nIf you have any questions please approach the authors mentioned above. " + "\nThank You!");

        }

        catch(Exception e)
        {

        }
    }

    public void addBook()
    {

    }

    class searchButton implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            searchBook();
        }

    }

    class rentButton implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            rentBook();
        }

    }

    class returnButton implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            returnBook();
        }

    }

    class aboutButton implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            aboutBook();
        }
    }

    class addButton implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            addBook();
        }
    } 
}

class MainSystem
{

    public static void main(String[] args) 
    {   
        new Library();           
    }
}

i defined a Book class like you said, but how am i going to add elements in my ArrayList? could you give me an example?

and wouldnt be the type of my ArrayList be Book and not String?

Yes, it's array list of Books.
Your Book class is a good start - but don't forget the instance variables for author, publisher etc
You just need to create new Books with their titles, autor, publisher etc etc and add those books to the arraylist. You add Books to an ArrayList<Book> exactly the same way that you added Strings to an ArrayList<String>

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Jaymark
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.Color;
public class Book
{
    String title;
    String author;
    String publisher;
    int isbn;
    boolean borrowed;

    public Book(String bookTitle) 
    {
        title = bookTitle;
        borrowed = false;
    }

    public void borrowed() 
    {
        borrowed = true;
    }


    public void returned() 
    {
        borrowed = false;
    }

    public boolean isBorrowed() 
    {
        return ((borrowed) ? true : false);
    }

    public void setAuthor(String bookAuthor)
    {
        author = bookAuthor;
    }

    public void setPublisher(String bookPublisher)
    {
        publisher = bookPublisher;
    }

    public void setIsbn(int bookIsbn)
    {
        isbn = bookIsbn;
    }

    public String getTitle() 
    {
        return title;
    }

    public String getAuthor() 
    {
        return author;
    }

    public String getPublisher() 
    {
        return publisher;
    }

    public int getIsbn() 
    {
        return isbn;
    }
}
class Library
{
    // How Do i add author, publisher and book id here?
    ArrayList<Book> bookName = new ArrayList<Book>();

    {{
        bookName.add(new Book("Hunger Games"));
        bookName.add(new Book("Catching Fire"));
        bookName.add(new Book("Mocking Jay"));
    }}

    JFrame myFrame = new JFrame("Library");
    JLabel jLabel1 = new JLabel("***JhayJheDiegz Books***");
    JLabel jLabel2 = new JLabel("2nd Floor S-205, Science Centrum Building,");
    JLabel jLabel3 = new JLabel("Colegio de Dagupan, Arellano St. Dagupan City");
    JButton jButton1 = new JButton("Search");
    JButton jButton2 = new JButton("Rent");
    JButton jButton3 = new JButton("Return");
    JButton jButton4 = new JButton("About");
    JButton jButton5 = new JButton("Add Book To Our Library");
    FlowLayout layout = new FlowLayout(FlowLayout.CENTER);
    Font font = new Font("Serif", Font.BOLD, 20);


    private int pressed;


    public Library()
    {
        myFrame.setBounds(485,425, 305,200);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setVisible(true);
        myFrame.setLayout(layout);
        myFrame.setResizable(false);

        JPanel jPanel = new JPanel();

        jButton1.addActionListener(new searchButton());
        jButton2.addActionListener(new rentButton());
        jButton3.addActionListener(new returnButton());
        jButton4.addActionListener(new aboutButton());
        jButton5.addActionListener(new addButton());

        jButton1.setToolTipText("Search Book");
        jButton2.setToolTipText("Rent Book");
        jButton3.setToolTipText("Return Book");
        jButton4.setToolTipText("About Our Program");
        jButton5.setToolTipText("Add Book");

        myFrame.add(jLabel1);
        myFrame.add(jLabel2);
        myFrame.add(jLabel3);
        myFrame.add(jButton1);
        myFrame.add(jButton2);
        myFrame.add(jButton3);
        myFrame.add(jButton4);
        myFrame.add(jButton5);

        jLabel1.setFont(font);
        myFrame.getContentPane().setBackground(Color.LIGHT_GRAY);

    }


    public void searchBook()
    {       
        String input;
        int x;
        boolean found = false;

        input = JOptionPane.showInputDialog(null, "Enter Name: ");

        try
        {
        for(x=0;x<bookName.size();x++)
        {
            if(input.equalsIgnoreCase(bookName.get(x).getTitle()))
            {
                                                              //the getAuhtor() method outputs null, how do i add data for the author?
                JOptionPane.showMessageDialog(null, "Book Found !" + "\nTitle: " + bookName.get(x).getTitle() + "\n" + bookName.get(x).getAuthor(), 
                    "Book Search", JOptionPane.INFORMATION_MESSAGE);

                    found = true;
            }

        }

        if(!found)
        {
            JOptionPane.showMessageDialog(null, "BOOK NOT FOUND !" , "Book Search", JOptionPane.ERROR_MESSAGE);

            searchBook();
        }
        }

        catch(Exception e)
        {

        }

    }

    public void rentBook()
    {
        for(int x = 0; x<bookName.size();x++)
        {
            JOptionPane.showMessageDialog(null, bookName.get(x).getTitle() + "\n" + bookName.get(x).getAuthor());
        }       
    }

    public void returnBook()
    {

    }

    public void aboutBook()
    {
        try
        {

        JOptionPane.showMessageDialog(null, "Authors: " + "\nJeli Quides" + "\nEmmanuel Bulawan" + "\nJaymark Dacpano" + 
            "\n" + "\nWe made this program for our final requirement in our Java Programming 2, " + 
                "\nThis code is written in JCreator Pro from scratch, " + 
                    "\nIf you have any questions please approach the authors mentioned above. " + "\nThank You!");

        }

        catch(Exception e)
        {

        }
    }

    public void addBook()
    {
        try
        {
            String bN;

            bN = JOptionPane.showInputDialog(null, "Enter Book Title: ", "Add Book", JOptionPane.QUESTION_MESSAGE);



            JOptionPane.showMessageDialog(null, "Book Added ! Thank You ! ");

        }

        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Please Eneter Valid ISBN !" , "Invalid Book ISBN", JOptionPane.ERROR_MESSAGE);

            addBook();
        }

    }

    class searchButton implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            searchBook();
        }

    }

    class rentButton implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            rentBook();
        }

    }

    class returnButton implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            returnBook();
        }

    }

    class aboutButton implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            aboutBook();
        }
    }

    class addButton implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            addBook();
        }
    } 
}

class MainSystem
{

    public static void main(String[] args) 
    {   
        new Library();           
    }
}

how do i add auhtor, publisher, and id? beacause it only shows the title. the getAuthor() returns null...

Easiest way is probably to create the Book completely (using your set... methods, or by passing all those values to the constructor)before adding it to the ArrayList
Book b = new Book...
b.setAuthor(...
b.setPublisher(...
...
books.add(b)

OR

books.add(newBook(title, author, publisher....

ps You should chjange the name of your ArryList to match how you use it - it's not just a list of book name any more

pps: Norm pointed out that you double-posted this (against DaniWeb rules). Is anyone helping you on the other site, becuase if so one of us is wasting our time, and you are going to get confused!

i got it to work now. thanks for the help James ! :)

Excellent! Please mark this "solved" for our knowledge base. And next time you have a question please don't double-post it - that's against the DaniWeb rules that you agreed to when you signed up, and will probably result in your post being deleted.

oops sorry bout that. i'll be taking note of that. :)

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.