gpt4 book ai didi

java - 对象库的方法存在问题

转载 作者:行者123 更新时间:2023-12-01 04:12:45 25 4
gpt4 key购买 nike

我正在尝试编写一个程序来创建一个包含不同书籍的图书馆,我为图书馆中的每个项目设置了多个副本,每次我 checkout 一个项目时,我希望它仅从该项目中扣除一份副本我检查了特定的对象,但它从所有对象中获取了一个副本。不知道如何解决这个问题。

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!");}
}
}

在我的 addBook 方法中,每次制作一本新书时,我都会创建一个名为 book1 的新对象,因此我认为每次添加一本书时,它可能会更改所有书籍对象。我不太确定编写该方法的更好方法。

这也是我的书籍方法

public class Book extends WrittenItem{
public Book(){
super();
}
public Book(String date, String a, int copy, int id, String t){
super(a, date, copy, id, t);

}
public void viewDetails(){
System.out.println("ID: "+getIdentificationNumber()+"\nTitle: "+getTitle()+"\nAuthor: "+getAuthor()+" Date written: "+getDate()+"\nCopies available: "+getNumberCopies());
}
}

任何帮助将不胜感激。

最佳答案

我无法从您的代码中 100% 判断,但我假设 Book 扩展了 Item。在这种情况下,请为 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 在Item 上具有相同的副本。因此,当您 checkOut() 时,Item 编号等于您从 Book 构造函数输入的值。如果您不将 super() 放入构造函数中,您的 Item copies 将保持为 0,并且您将始终获得 System .out.println(““+title+”的所有副本均已 check out !”); 因为 copies 永远不会 > 0

关于java - 对象库的方法存在问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19757706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com