我用对象,数组和方法练习更多。现在我正在编写一个简单的簿记程序来添加书籍,按书名,作者,价格,库存编号和流派搜索书籍。我有一些问题,修复这个程序(在方法fillBook()中添加一本书,两个,如何从对象类中删除一本书,三,如何使这个程序更有效率。
目前正在研究和阅读ioexception类。
这是我得到的错误
java:66:error:构造函数Book中的Book Book不能应用于给定的类型;
预订tempBook = new Book();
^
required:String,String,double,int,String
发现:没有参数
原因:实际和正式的参数列表长度不同
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
答案 0 :(得分:1)
您需要为Book类添加默认构造函数。 一旦提供了参数化构造函数,java就不会自动创建默认的conatructor,因此您需要提供它。
Book tempBook = new Book();
将在您的班级中提供以下构造函数后开始工作。
Public Book(){
//implementation
}