[英]How can i use a constructor using JOptionPane to pass objects from my method into a empty array?
I'm having trouble figuring out how to store the objects from the constructor. 我在弄清楚如何从构造函数中存储对象时遇到了麻烦。 so far all I get is one object and the remaining are all null.
到目前为止,我得到的只是一个对象,其余全部为null。 If someone can explain it to me so that a beginner can understand that would be much appreciated.
如果有人可以向我解释它,以便初学者可以理解,将不胜感激。
public class Bookstore
{
/*
Main Class Bookstore to be made modular
*/
public static void main(String args[])
{
Book catalogue[] = new Book[3];
int select;
do
{
select = bookMenu();
switch(select)
{
case 1:
int i =0;
if(catalogue[i] != null)
{
JOptionPane.showMessageDialog(null,"Test");
break;
}
catalogue[i] = addBook();
case 2:
sortBook();
break;
case 3:
searchBook(catalogue);
break;
case 4:
displayBook(catalogue);
break;
case 5:
break;
}
}
while(select != 5);
}
public static int bookMenu()
{
int select;
String menuOptions = "--Book store--\n"
+ "\n1. Add book to catalogue"
+ "\n2.Sort and display books by price"
+ "\n3. Search for a book by title"
+ "\n4. Display all books"
+ "\n\n5. Exit";
do
{
select = Integer.parseInt(JOptionPane.showInputDialog(menuOptions));
}
while(select < 1 || select > 5);
return select;
}
public static Book addBook()
{
int isbn;
String title, author;
Book catalogue = null;
double price;
for(int i=0; i<3;i++)
{
isbn = Integer.parseInt(JOptionPane.showInputDialog
("Enter Book ISBN or: "));
title = JOptionPane.showInputDialog
("Enter Book Title: ");
author = JOptionPane.showInputDialog
("Enter Book Author: ");
price = Double.parseDouble(JOptionPane.showInputDialog
("Enter Book Price: "));
catalogue = new Book(isbn, title, author, price);
}
return catalogue;
}
public static void sortBook()
{
}
public static void searchBook(Book catalogue[])//remain void
{
String searchValue = JOptionPane.showInputDialog("Enter the title of the book you are searching for");
boolean found = true;
for(int i=0; i<catalogue.length && catalogue[i] != null ; i++)
{
if(searchValue.equalsIgnoreCase(catalogue[i].getTitle()))
{
JOptionPane.showMessageDialog(null, "Book details: " + catalogue[i].toString());
found = true;
}
}
if(found == false)
JOptionPane.showMessageDialog(null, "The title does not exist in the collection ");
}
public static void displayBook(Book catalogue[])//remain void
{
String output = "";
for(Book bk:catalogue)
{
output += bk + "\n";
}
JOptionPane.showMessageDialog(null, output);
}
}
So, this is always false... 因此,这始终是错误的...
if(count == max)
You set the values immediately before that statement. 您在该语句之前设置值。 Zero never equals ten.
零永远不等于十。
Some IDEs would even point that out to you. 一些IDE甚至会向您指出这一点。
If you want to get a variable that can be used between methods, you need to learn variable scoping. 如果要获取可以在方法之间使用的变量,则需要学习变量作用域。
For example, using a static class variable 例如,使用静态类变量
private static Book[] books;
private static final int MAX_BOOKS = 10;
private static int count;
public static void main(String[] args) {
books = getBooks();
bookMenu();
}
public static Book[] getBooks() {
if(count == MAX_BOOKS) {
JOptionPane.showMessageDialog(null, "Catalogue full - cannot add any more books");
} else {
for(; count < MAX_BOOKS; count++) {
books[count]= addBook();
}
}
return books;
}
Then, if you want to repeat the menu, use a loop. 然后,如果要重复菜单,请使用循环。 Don't place it at the end of every method.
不要将其放在每种方法的末尾。
Also searchBook(Book)
isn't searching for a title. 另外
searchBook(Book)
不在搜索标题。 You want to pass a string to the method, not a Book class 您想将字符串传递给方法,而不是Book类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.