Java 对象库的方法问题
我正在尝试制作一个程序,创建一个不同书籍的库,我为库中的每个项目设置了一个副本数量,每次我签出一个项目时,我希望它仅从我签出的特定对象中扣除一个副本,但它会从所有对象中拿走一个副本。不知道如何解决这个问题Java 对象库的方法问题,java,Java,我正在尝试制作一个程序,创建一个不同书籍的库,我为库中的每个项目设置了一个副本数量,每次我签出一个项目时,我希望它仅从我签出的特定对象中扣除一个副本,但它会从所有对象中拿走一个副本。不知道如何解决这个问题 public abstract class Item{ private int identify; private String title; private int copies; public Item(){ identify=0; title="N/A"
public abstract class Item{
private int identify;
private String title;
private int copies;
public Item(){
identify=0;
title="N/A";
copies=0;
}
public Item(int id, int copy, String t){
identify=id;
copies=copy;
title=t;
}
public void setIdentificationNumber(int id){
identify = id;
}
public void setTitle(String t){
title=t;
}
public void setNumberCopies(int num){
copies=num;
}
public int getIdentificationNumber(){
return identify;
}
public String getTitle(){
return title;
}
public int getNumberCopies(){
return copies;
}
public void checkOut(){
if(copies>0){
copies-=1;
System.out.println("You have checked out "+title+". Thank You");
}
else{
System.out.println("All copies of "+title+" are checked out!");
}
}
public void checkIn(){
copies+=1;
}
}
问题也可能出在我的客户端方法中,我已经在下面发布了相应的代码
import java.util.Arrays;
import java.util.Scanner;
public class Library{
static String title;
static String author;
static int id;
static int copies;
static String date;
static Book[] database = new Book[100];
static int count=0;
public static void main(String[] args){
int i;
Scanner s = new Scanner(System.in);
do{
addBook();
System.out.println("would you like to add another book?");
i=s.nextInt();
}while(i == 0);
database[0].viewDetails();
database[1].viewDetails();
checkingOut();
database[0].viewDetails();
database[1].viewDetails();
}
public static void addBook(){
Scanner s = new Scanner(System.in);
System.out.println("Enter the title of the book you want to add to the collection");
title=s.nextLine();
System.out.println("Enter the author of the book you want to add to the collection");
author=s.nextLine();
System.out.println("Enter the publishing date of the book you want to add to the collection");
date=s.nextLine();
System.out.println("Enter the ID number of the book you want to add to the collection");
id=s.nextInt();
System.out.println("Enter the the number of copies that will be added into the collection");
copies=s.nextInt();
Book Book1 = new Book(date, author, copies, id, title);
database[count] = Book1;
count++;
}
public static void checkingOut(){
boolean found=false;
int idSearch;
int i=0;
Scanner s = new Scanner(System.in);
System.out.println("Enter the ID number of the book you want to check out");
idSearch=s.nextInt();
while(i<database.length && found!=true){
if(database[i].getIdentificationNumber() == idSearch){
found = true;
}
i++;
}
if(found==true){
database[i].checkOut();
System.out.println("There are "+database[i].getNumberCopies()+" copies left");
}
else{System.out.println("There is no book with that ID number!");}
}
}
任何帮助都将不胜感激。我不能100%地从您的代码中看出,但我假设这本书扩展了这个项目。在这种情况下,请为Book构造函数尝试类似的方法
public class Book extends Item {
String author;
String date;
public Book(String date, String author, int copies, int id, String title) {
super(id, copies, title); // Item constructor matches this super() call
// public Item(int id, int copy, String t)
this.author = author;
this.date = date;
}
}
您希望这本书在项目中有相同的副本。因此,当您签出时,项目编号等于您从Book构造函数中输入的内容。如果您不将super放入构造函数中,您的项目副本将保持为0,并且您将始终获得System.out.println+title+的所有副本都已签出!;因为拷贝数永远不会超过0。如果你的书是这样的
public class Book extends Item {
private String date;
private String author;
public Book() {
}
public Book(String date, String author, int copies, int id, String title) {
super(id, copies, title);
this.author = author;
this.date = date;
}
void viewDetails() {
System.out.println("date:" + date + " author:" + author + " copies:" + getNumberCopies() + " id:" + getIdentificationNumber() + " title:" + getTitle());
}
}
那么,正如我所测试的,如果您在checkingOut方法中添加一个中断,那么您的代码应该可以正常工作
你应该加上休息;在这一行之后,found=true?书是否扩展项目?是的书是否扩展项目,我有上级、副本、标题;在这本书的构造器中,让我来纠正一下。您的基本问题是,您正在获取此输出System.out.PrintLn+的所有副本都已签出!;您已签出+title+,而不是这一个System.out.println。非常感谢。我说得对吗。或者还有其他问题吗?您想显示WrittenItem的一个片段,仅显示相关代码,包括构造函数。@Julien Kerr,不,您的书构造函数中没有superid、copies和title。您的书本构造函数有一个super5参数
public class Book extends Item {
private String date;
private String author;
public Book() {
}
public Book(String date, String author, int copies, int id, String title) {
super(id, copies, title);
this.author = author;
this.date = date;
}
void viewDetails() {
System.out.println("date:" + date + " author:" + author + " copies:" + getNumberCopies() + " id:" + getIdentificationNumber() + " title:" + getTitle());
}
}
public static void checkingOut() {
boolean found = false;
int idSearch;
int i = 0;
Scanner s = new Scanner(System.in);
System.out.println("Enter the ID number of the book you want to check out");
idSearch = s.nextInt();
while (i < database.length && found != true) {
if (database[i].getIdentificationNumber() == idSearch) {
found = true;
break; //add this
}
i++;
}
if (found == true) {
database[i].checkOut();
System.out.println("There are " + database[i].getNumberCopies() + " copies left");
} else {
System.out.println("There is no book with that ID number!");
}
}