2013-11-03 28 views
1

我想创建一个程序,创建不同的书籍库,我已经设置了库中的每个项目的副本数量,每次检出项目I希望它只从我检出的特定对象中扣除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()); 
    } 
} 

任何帮助将不胜感激。

+0

如果您在此行后面添加'break;'found = true;'? – vikingsteve

+1

'书'延长'项目'? –

回答

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

你想Book具有相同的copies的项目。所以当你checkOut(),产品编号等于你从Book构造函数输入。如果您没有将super()置于构造函数中,那么您的Itemcopies将保持为0,您将始终得到System.out.println("All copies of "+title+" are checked out!");,因为copies从不是> 0

+0

是的书确实扩展了项目,我有超级(id,副本,标题);在书的构造函数中。 –

+0

所以,让我解决这个问题。你的基本问题是,你得到这个输出''System.out.println(“所有副本+”标题+“被检出!”);'而不是这个'System.out.println(“你已经检出“+ title +”。谢谢你“);'?我对么。或者是否有其他问题出错? –

+0

你想要显示你的'WrittenItem'的片段,只是相关的代码,包括构造函数。 –

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

谢谢,我添加了break,现在它正在工作! –