オブジェクト、配列、メソッドを使ってもっと練習しています。現在、本を追加し、タイトル、著者、価格、在庫数、およびジャンルで本を検索する簡単な簿記プログラムを作成しています。1 つはこのプログラムの修正 (メソッド fillBook() に本を追加する方法)、2 はオブジェクト クラスから本を削除するにはどうすればよいか、3 はこのプログラムをより効率的にするにはどうすればよいかについていくつか質問があります。
現在調査中です。 ioexception クラスを読んでいます。
これは私が取得しているエラーです
java:66: error: class Book のコンストラクタ Book cannot be applied to given types;
本 tempBook = new Book();
^
必須: 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