我正在使用java序列化开发一个图书馆系统。当我实现图书发行部分时,我需要存储图书的细节和会员的详细信息。
我用过" issueNewBook"方法。该方法属于" Book" class.so当我调用这个方法时,它有一个属性" libraryMember成员"并返回会员ID。
在我调用此方法后,它表示字符串无法转换为libraryMember? 我不知道如何解决这个问题。我尝试了所有我知道的事情。你的帮助在这个时候非常重要。谢谢你。
图书馆成员班:
public class libraryMember implements Serializable{
String userType;
String username;
String password;
String confirmPassword;
private String name;
public int memberNumber;
private SetOfBooks currentLoans = new SetOfBooks();
private static int memberCount = 0;
public libraryMember() {
userType=null;
username=null;
password=null;
confirmPassword=null;
}
public libraryMember(String aName){
name= aName;
memberNumber = memberCount++;
currentLoans = new SetOfBooks();
}
@Override
public String toString(){
return Integer.toString(memberNumber) + " " + name;
}
public void borrowBook(Book aBook){
currentLoans.addBook(aBook);
aBook.setBorrower(this);
}
public String getUserType(){
return this.userType;
}
public Object getName() {
return this.username;
}
public String getPassword() {
return this.password;
}
public String getConfirmPassword() {
return this.confirmPassword;
}
public void singUp(Object userType, String username, String password) {
this.userType = (String) userType;
this.username = username;
this.password = password;
}
}
图书课程:
public class Book implements Serializable{
public String title;
public String author;
public String publisher;
public String isbn;
public int noOfCopies;
private libraryMember borrower = null;
private static int bookCount = 0;
private int accessionNumber;
public String Issuedate;
public String returnDate;
public Book(String name){
title = name;
accessionNumber = bookCount++;
}
public void addNewBook(int bookID, String title, String author,String publisher,String isbn,int noOfCopies) {
this.accessionNumber = bookID;
this.title = title;
this.author = author;
this.publisher = publisher;
this.isbn=isbn;
this.noOfCopies=noOfCopies;
}
public void issueNewBook(int bookID,String title,libraryMember member,String issuedDate,String returnDate)
{
this.accessionNumber= bookID;
this.title= title;
this.borrower=member;
this.Issuedate = issuedDate;
this.returnDate=returnDate;
}
public int getBookID()
{
return accessionNumber+1;
}
public String getBookName()
{
return title;
}
void setBorrower(libraryMember theBorrower){
borrower = theBorrower;
}
libraryMember geMember(){
return borrower;
}
}
图书问题按钮操作:
// TODO在此处添加您的处理代码:
ArrayList<Book> bookList = new ArrayList();
int flag = 0;
try {
FileInputStream fis = new FileInputStream("issuedBook.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
Book book;
while ((book = (Book) ois.readObject()) != null) {
if (book.getBookName().equals(issue_BookNameArea.getText())) {
flag = 1;
break;
}
bookList.add(book);
}
ois.close();
fis.close();
}
catch(FileNotFoundException e)
{
}
catch (EOFException e) {
} catch (ClassNotFoundException ex2) {
} catch (IOException e) {
}
if (flag == 0) {
//write to file only if name doesn't exist
Book bk = new Book(getName());
String s = issue_BookNameArea.getText();
libraryMember member = null;
bk.issueNewBook((Integer.parseInt(issue_BookIdField.getText())),issue_BookNameArea.getText(),issue_MemberID.getText(), issueDate_dateChooser.getText(), returndaateField.getText());
bookList.add(bk);
try {
FileOutputStream fos;
ObjectOutputStream oos;
fos = new FileOutputStream("newMember.dat");
oos = new ObjectOutputStream(fos);
Book b;
Iterator i = bookList.iterator();
while (i.hasNext()) {
b = (Book) i.next();
oos.writeObject(b);
oos.flush();
}
fos.close();
oos.close();
// dispose();
} catch (IOException e) {
}
}
}
问题包含方法:
bk.issueNewBook((Integer.parseInt(issue_BookIdField.getText())),issue_BookNameArea.getText(),issue_MemberID.getText(),issueDate_dateChooser.getText(),returndaateField.getText());
错误来自:
issue_MemberID.getText()
错误:
无法将字符串转换为libraryMember
答案 0 :(得分:0)
而不是:
bk.issueNewBook((Integer.parseInt(issue_BookIdField.getText())),issue_BookNameArea.getText(),issue_MemberID.getText(), issueDate_dateChooser.getText(), returndaateField.getText());
尝试使用libraryMember
中的字符串创建一个新的getText()
对象(因为您已经定义了一个带有字符串的构造函数)并将其作为第三个参数传递:
bk.issueNewBook((Integer.parseInt(issue_BookIdField.getText())),issue_BookNameArea.getText(),new libraryMember(issue_MemberID.getText()), issueDate_dateChooser.getText(), returndaateField.getText());