我是java的新手,对不一致感到抱歉。
我正在创建一个库程序,我在Patron类的Book类中调用方法时遇到了麻烦。
在Patron课程中,我有一个方法checkOutBook(),用户可以输入一本书来检查。但是我在Book中访问setStatus()方法时遇到了问题。我知道我必须针对Book类的实例调用它,但我不确定如何使用用户输入的字符串。
function getFreeCode(callback) {
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var code = "";
for (var i = 0; i < 4; i++) {
var rand = Math.floor(Math.random() * chars.length);
console.log(rand);
code += chars.charAt(rand);
}
console.log("Code: %s generated.", code);
client.execute("select * from codes where code="+code, function(err, result) {
if(!err) {
if(result.rows.length > 0) {
getFreeCode(callback);
} else {
callback(code);
}
}else {
console.log('DB ERR: %s', err);
}
console.log(codeFree);
});
console.log('here');
}
// in your main:
getFreeCode(function (code) {
console.log(' this code was free: ' + code)
})
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Book implements BookInterface {
Scanner input = new Scanner(System.in);
static ArrayList < String > UserList = new ArrayList < String > ();
static ArrayList < String > BookList = new ArrayList < String > ();
public String title;
public String author;
public Book book;
private String status;
private String borrower;
public Book(String t, String a) {
title = t;
author = a;
}
//constructor create new book
public Book(String newTitle) {
title = newTitle;
}
public String toString() {
return title + " " + author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getStatus(String book) {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public void setBorrower(String borrower) {
this.borrower = borrower;
}
public String getBorrower(String checkPatron) {
return borrower;
}
public String getBook(String checkPatron) {
return book;
}
public void setBook(Book bookCheckOut) {
this.book = bookCheckOut;
}
}
我试图这样做,但它也不起作用:( 谢谢您的帮助!
public void CheckOutBook() {
Scanner inputread = new Scanner(System.in);
Scanner input = new Scanner(System.in);
System.out.println("Enter full patron name: ");
String borrower = inputread.nextLine();
System.out.println("Enter book title to check out: ");
String bookCheckOut = inputread.nextLine();
if (Book.BookList.contains(bookCheckOut)) {
Book.BookList.remove(bookCheckOut);
Book.setStatus("OUT"); //error message
Book.setBorrower(borrower); //error message
System.out.println("----------" + bookCheckOut + " has been checked out!----------");
System.out.println("-------------------BOOK STATUS:---------------------");
System.out.println("Book Title: " + bookCheckOut);
System.out.println("Book Status: Checked out");
System.out.println("Borrower: " + borrower);
System.out.println("Due Date: " + dueDate);
System.out.println("----------------------------------------------------");
答案 0 :(得分:0)
Book bookCheckOut = new Book(bookCheckOut); &lt;〜我认为你很困惑,因为你已经将bookCheckOut定义为扫描仪中的一个字符串,在这一行中你将它设置为一个书本对象。尝试将此行中的第一个bookCheckOut更改为其他内容。语法:Book bookObjectName = new Book(“String”);
对于setStatus和setBorrower错误,您尝试使用这些方法,就好像它们是静态的一样。要解决此问题,请不要调用Book.setStatus,而是将Book替换为您实例化的book对象名称。示例:预订b =新书(“Asd”,“abc”); b.setStatus( “XYZ”);
另外,为什么要有两台扫描仪?
答案 1 :(得分:0)
将Book.BookList
从ArrayList<String>
更改为Map<String, Book>
,如下所示:
public class Book {
static Map<String, Book> BookList = new HashMap<>();
...
然后在CheckOutBook()
更改:
if (Book.BookList.contains(bookCheckOut)) {
Book.BookList.remove(bookCheckOut);
Book.setStatus("OUT"); //error message
Book.setBorrower(borrower); //error message
...
要:
Book book = Book.BookList.get(bookCheckOut);
if (book != null) {
Book.BookList.remove(bookCheckOut);
book.setStatus("OUT");
book.setBorrower(borrower);
...