1

我正在尝试制作一个程序来创建一个包含不同书籍的图书馆,我为图书馆中的每个项目设置了多个副本,并且每次我签出一个项目时,我希望它只从我的特定对象中扣除 1 个副本签出,但它会从所有对象中取出一个副本。不知道如何解决这个问题。

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());
  }
}

任何帮助将不胜感激。

4

2 回答 2

0

我不能 100% 从你的代码中看出,但我假设Book extends 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;
    }
}

你想在 ItemBook上也一样。copies因此,当您 时checkOut(),项目编号等于您从Book构造函数输入的内容。如果您不将 thesuper()放入构造函数中,您Item copies将保持 0 并且您将始终得到System.out.println("All copies of "+title+" are checked out!");,因为copiesis never > 0

于 2013-11-03T20:52:25.047 回答
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()

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!");
        }
    }
于 2013-11-03T21:19:12.367 回答