0

I practicing more with objects, arrays, and methods. Right now I am writing a simple book keeping program to add a book, search books by title, author, price, inventory number, and genre. I have a few questions on one, fixing this program (adding a book in method fillBook(), two, how could I delete a book from the object class, and three, how can I make this program more efficient.

Currently researching, and reading on ioexception class.


This is the error I am getting
java:66: error: constructor Book in class Book cannot be applied to given types;
Book tempBook = new Book();
^
required: String,String,double,int,String
found: no arguments
reason: actual and formal argument lists differ in length
1 error

Tool completed with exit code 1

template object class

public class Book
{
private String title;
private String author;
private double price;
private int inventoryNumber;
private String category;

private static int numberOfBooks = 0;

public Book(String bookTitle, String bookAuthor, double bookPrice, int bookInventoryNumber, String bookCategory)
{
    title = bookTitle;
    author = bookAuthor;
    price = bookPrice;
    inventoryNumber = bookInventoryNumber;
    category = bookCategory;
    numberOfBooks++;
}//end 5 args Book Constructor

public String getTitle()  {return title;}
public void setTitle(String t)  {title = t;}

public String getAuthor() {return author;}
public void setAuthor(String a)  {author = a;}

public double getPrice() {return price;}
public void setPrice(double p)  {price = p;}

public int getInventoryNumber() {return inventoryNumber;}
public void setInventoryNumber(int i)  {inventoryNumber = i;}

public String getCategory()  {return category;}
public void setCategory(String c)  {category = c;}

public static int getNumberOfBooks()  {return numberOfBooks;}

public String toString ()
{
    StringBuffer sb = new StringBuffer();
    sb.append("Title: " + title);
    sb.append("\nAuthor: " + author);
    sb.append("\nPrice: " + price);
    sb.append("\nInventoryNumber: " + inventoryNumber);
    sb.append("\nCategory: " + category + "\n\n");
    return (new String (sb));
}//end toString

public static String searchByTitle(Book[] newBook, String seachName)
{
    String message = "";
    for(int i=0; i<getNumberOfBooks(); i++)
    {
        if (seachName.equalsIgnoreCase(newBook[i].getTitle()))
        {
            message += newBook[i].toString();
        }//end if
    }//end for
    return message;
}//end searchByTitle

public static String searchByAuthor(Book[] newBook, String seachName)
{
    String message = "";
    for(int i=0; i<getNumberOfBooks(); i++)
    {
        if (seachName.equalsIgnoreCase(newBook[i].getAuthor()))
        {
            message += newBook[i].toString();
        }//end if
    }//end for
    return message;
}//end searchByAuthor

public static String searchByPrice(Book[] newBook, double seachPrice)
{
    String message = "";
    for(int i=0; i<getNumberOfBooks(); i++)
    {
        if (seachPrice == newBook[i].getPrice())
        {
            message += newBook[i].toString();
        }//end if
    }//end for
    return message;
}//end searchByPrice

public static String searchByInventory(Book[] newBook, int seachInventory)
    {
        String message = "";
        for(int i=0; i<getNumberOfBooks(); i++)
        {
            if (seachInventory == newBook[i].getInventoryNumber())
            {
                message += newBook[i].toString();
            }//end if
        }//end for
        return message;
}//end searchByInventory

public static String searchByCategory(Book[] newBook, String seachName)
    {
        String message = "";
        for(int i=0; i<getNumberOfBooks(); i++)
        {
            if (seachName.equalsIgnoreCase(newBook[i].getCategory()))
            {
                message += newBook[i].toString();
            }//end if
        }//end for
        return message;
}//end searchByAuthor

}//end class

run program class

import javax.swing.JOptionPane;

class FindBook
{
public static void main(String[] args)
{
    Book[] newBook = new Book[2000];
    newBook[0] = new Book("Game of Thrones", "George R. R. Martin", 39.95, 3, "fiction");
    newBook[1] = new Book("A Song of Ice and Fire", "George R. R. Martin", 34.50, 3, "fiction");
    newBook[2] = new Book("Java Programming For Dummies", "Donald Koosis", 59.29, 12, "non fiction");
    newBook[3] = new Book("Java™ Programming: From Problem Analysis to Program Design, 5th Edition", "Malik", 140.49, 4, "non fiction");
    newBook[4] = new Book("Life of Pi", "Yann Martel", 12.50, 3, "childrens");

    boolean continueOption = true;
    //****************menu bar********************/
    do {
        int menuOption = getMenu();
        switch (menuOption)
        {
            case 1: addBook(newBook); break;
            case 2: searchByTitle(newBook); break;
            case 3: searchByAuthor(newBook); break;
            case 4: searchByPrice(newBook); break;
            case 5: searchByInventory(newBook); break;
            case 6: searchByCategory(newBook); break;
            case 7: displayAllBookInfo(newBook); break;
            case 8: continueOption = false; break;
            default: JOptionPane.showMessageDialog(null, "Invalid choice"); break;
        }//end menu
    }while (continueOption);

    JOptionPane.showMessageDialog(null, "Thank You Come Again");

}//end main

public static int getMenu()
{
    String message;
    int choice;
    message = "\n1. Add a book in the book database: \n"
    + "2. Search book database by title: \n"
    + "3. Search books database by author: \n"
    + "4. Search books database by price: \n"
    + "5. Search books database by inventory: \n"
    + "6. Search books database by category: \n"
    + "7. Display all book information: \n"
    + "8. Exit the program\n\n"
    + "Please enter in a number from the menu to choose.";
    choice = Integer.parseInt(JOptionPane.showInputDialog(null,message));
    return choice;
} // end getMenu method

// option to add another book
public static void addBook(Book[] aBook)
{
    int select;
    do{
        aBook[Book.getNumberOfBooks()] = fillBook();
        select = JOptionPane.showConfirmDialog(null, "Add another book?");
    }while (select == JOptionPane.YES_OPTION && Book.getNumberOfBooks() < 2000);
}//end method add book

//filling in a book into book array
public static Book fillBook()
{
    Book tempBook = new Book();
    tempBook.setTitle(JOptionPane.showInputDialog(null, "Enter a title"));
    tempBook.setAuthor(JOptionPane.showInputDialog(null, "Enter an author"));
    tempBook.setPrice(Double.parseDouble(JOptionPane.showInputDialog(null, "Enter in a Price")));
    tempBook.setInventoryNumber(Integer.parseInt(JOptionPane.showInputDialog(null, "Enter in how many book(s) are in inventory")));
    tempBook.setCategory(JOptionPane.showInputDialog(null, "Enter in the category"));
    return tempBook;
}//end fillBook


public static void searchByTitle(Book[] aBook)
{
    String message = "";
    String searchTitle = "";
    searchTitle = JOptionPane.showInputDialog(null, "What title do you want to search for?");
    message = Book.searchByTitle(aBook, searchTitle);
    JOptionPane.showMessageDialog(null, message);
}//end method searchByTitle

public static void searchByAuthor(Book[] aBook)
{
    String message = "";
    String searchAuthor = "";
    searchAuthor = JOptionPane.showInputDialog(null, "What Author do you want to search for?");
    message = Book.searchByAuthor(aBook, searchAuthor);
    JOptionPane.showMessageDialog(null, message);
}//end method searchByAuthor

public static void searchByPrice(Book[] aBook)
{
    String message = "";
    double searchPrice = Double.parseDouble(JOptionPane.showInputDialog(null, "What Author do you want to search for?"));
    message = Book.searchByPrice(aBook, searchPrice);
    JOptionPane.showMessageDialog(null, message);
}//end method searchByPrice

public static void searchByInventory(Book[] aBook)
{
    String message = "";
    int seachInventory = Integer.parseInt(JOptionPane.showInputDialog(null, "What Author do you want to search for?"));
    message = Book.searchByInventory(aBook, seachInventory);
    JOptionPane.showMessageDialog(null, message);
}//end method searchByInventory

public static void searchByCategory(Book[] aBook)
{
    String message = "";
    String searchCategory = "";
    searchCategory = JOptionPane.showInputDialog(null, "What Author do you want to search for?");
    message = Book.searchByCategory(aBook, searchCategory);
    JOptionPane.showMessageDialog(null, message);
}//end method searchByCategory

public static void displayAllBookInfo(Book[] aBook)
{
    String message ="";
    for(int i=0; i<Book.getNumberOfBooks(); i++)
    {
        message += aBook[i].toString();
    }//end for loop for displayAllBookInfo
    JOptionPane.showMessageDialog(null, message);
}//end method displayAllBookInfo


}//end class

1 Answer 1

1

You need a add a default constructor for Book class. Once you provide a parameterized constructor, java will not automatically create default conatructor, so you need to provide it.

Book tempBook = new Book(); will start working once you provide below constructor in your class.

Public Book(){
//implementation
}
Sign up to request clarification or add additional context in comments.

2 Comments

No need for an answer. Just a comment and the person can delete the question.
Thank you so much. I totally forgot to add a default constructor. I guess the hardest problems are the simple ones you overlook. Do you see anyway to make my code more efficient, or tips.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.