当前位置: 首页 > 知识库问答 >
问题:

代码效率、调试、对象方法、数组

通学真
2023-03-14

我更多地练习对象、数组和方法。现在我正在编写一个简单的簿记程序来添加一本书,按标题、作者、价格、库存编号和流派搜索书籍。我有几个问题,一是修复这个程序(在方法fillBook()中添加一本书),二是如何从对象类中删除一本书,三是如何使这个程序更高效

目前正在研究和阅读ioexception类。


这是我得到的错误java:66:错误:类手册中的构造函数手册不能应用于给定的类型
Book tempBook=新书()
^
必需:字符串、字符串、double、int、字符串
找到:没有参数
原因:实际参数列表和形式参数列表长度不同
1个错误

工具已完成,退出代码为1模板对象类

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

运行程序类

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个答案

程树
2023-03-14

您需要为Book类添加默认构造函数。一旦您提供了参数化构造函数,java将不会自动创建默认conatructor,因此您需要提供它。

Book temBook=new Book();将在您的类中提供以下构造函数后开始工作。

Public Book(){
//implementation
}
 类似资料:
  • 【代码效率】页面主要反馈和分析项目运行过程中代码的 “CPU占用时间” 和 “堆内存分配” 情况。其中,这里所分析的代码既包含Unity引擎的自身模块代码,也包含您自己书写的逻辑脚本。通过该部分,您将快速掌握项目中逻辑代码的运行性能瓶颈,从而有的放矢地进行改进和优化。 CPU占用时间 函数汇总 该项主要显示项目运行过程中具有较高CPU占用的函数列表,包括函数的 “总CPU耗时”、“最大单次CPU占

  • 问题内容: 我有一个可能包含数千个对象的模型。我想知道什么是最有效的方式来存储它们并在获得ID后检索单个对象。ID是长数字。 这些是我正在考虑的两个选项。在选项一中,它是一个带有递增索引的简单数组。在选项2中,如果有区别,它是一个关联数组,也可能是一个对象。我的问题是,当我主要需要检索单个对象时,有时又遍历它们并进行排序时,哪一个效率更高。 具有非关联数组的选项一: 选项2与关联数组: 更新: 好

  • 本文向大家介绍java代码效率优化方法(推荐),包括了java代码效率优化方法(推荐)的使用技巧和注意事项,需要的朋友参考一下 1、 尽量指定类的final修饰符 带有final修饰符的类是不可派生的。 如果指定一个类为final,则该类所有的方法都是final。Java编译器会寻找机会内联(inline)所有的 final方法(这和具体的编译器实现有关)。此举能够使性能平均提高50% 。 2、

  • 本文向大家介绍盘点提高 Python 代码效率的方法,包括了盘点提高 Python 代码效率的方法的使用技巧和注意事项,需要的朋友参考一下 第一招:蛇打七寸:定位瓶颈 首先,第一步是定位瓶颈。举个简单的栗子,一个函数可以从1秒优化到到0.9秒,另一个函数可以从1分钟优化到30秒,如果要花的代价相同,而且时间限制只能搞定一个,搞哪个?根据短板原理,当然选第二个啦。 一个有经验的程序员在这里一定会迟疑

  • 我是Java的初学者,我想弄清楚这一点。在方法中传递对象比通过类实例调用方法更有效吗?在我看来是一样的。假设下面的示例代码: 如果我定义了类,这个传递给对象方法的方法将在控制台上打印。而且,通常当我使用void方法时,我需要在main方法中编写内容,以便在控制台上打印void方法,但是当我在方法中传递对象时,我不需要这样做。

  • 我最近开始用Java编程。我想编写一个包含以下内容的窗口: 1帧 1个集装箱 2个JPanel对象(确保不会混淆面板、容器和框架对象) 1滚动的对象 1个JTextArea和1个JTextField 1个JButtonGroup,与3个JRadioButton关联 它的目的就像一个人聊天。在文本字段中写入,提交按钮,并将其打印在文本区域中,作为之前任何消息的附件。下一步,我将3个单选按钮命名为“用