(图书馆)签出状态全部返回" true"

时间:2015-07-09 21:28:26

标签: java

我没有使用布尔来查看是否已检出书籍,而是使用字符串来表示它是我想要的,但是当我调用该方法签出书籍时,该字符串将应用于书籍对象的整个数组列表。我怎样才能改变那本书的状态?

import java.util.*;

public class Library 
{

  String owner;
  int numBooks;
  boolean isCheckedOut;
  String status = "false";

  ArrayList<Book> bookList = new ArrayList<Book>();



public void addBook(Book b)
{
    bookList.add(b);

}
 public  String checkout(Book c) {

    status = "true";
    return status;

  }

  public Library(String o)
  {
    owner = o;
  }
  public String toString()
  {


   String s = "Owner:    \t" + owner + "\nSize:    \t" + 
               bookList.size()+"\nBooks:   \t";
   for( int i = 0;i < bookList.size();i++)
    { 
      String title = bookList.get(i).getTitle();
      //System.out.println(title);
      s+="\n\t\t\t" +title +"     Checked out: " + status;  //bookList.get(i).getStatus();
    }





    return s;
  }

  public static void main(String [] args)
  {
    Library l = new Library("Jeremiah");
    System.out.println(l);



  }
}

public class Mainn
{
  public static void main(String [] args)
  {
    Book book1  = new Book("Aambi","Aisney","Aalt","1942",1453);
    Book book2  = new Book("Bambi","Bisney","Balt","1942",1453);
    Book book3  = new Book("Cambi","Cisney","Calt","1942",1453);
    Book book4  = new Book("Dambi","Disney","Dalt","1942",1453);
    Book book5  = new Book("Eambi","Eisney","Ealt","1942",1453);
    Book book6  = new Book("Fambi","Fisney","Falt","1942",1453);
    Book book7  = new Book("Gambi","Gisney","Galt","1942",1453);
    Book book8  = new Book("Hambi","Hisney","Halt","1942",1453);
    Book book9  = new Book("Iambi","Iisney","Ialt","1942",1453);
    Book book10 = new Book("Jambi","Jisney","Jalt","1942",1453);
    Book book11 = new Book("Kambi","Kisney","Kalt","1942",1453);
    Library l = new Library("Jeremiah"); 

    // l.addBook(new Book("Xambi","Xisney","Xalt","1579",765)); 

     l.addBook(book1);
     l.addBook(book2);
     l.addBook(book3);
     l.addBook(book4);
     l.addBook(book5);
     l.addBook(book6);
     l.addBook(book7);
     l.addBook(book8);
     l.addBook(book9);
     l.addBook(book10);
     l.addBook(book11);


    // this changes all of the values to true  instead of just this one
     l.checkout(book1);
     l.checkout(book11);

     l.addBook(book1);

      System.out.println(l);
  }
}

3 个答案:

答案 0 :(得分:1)

因此,我在这里看到的一个大问题是,你没有“#34;状态&#34;分别在每本书的属性上,你只需要一个整个库的状态字符串。

它没有存储每本书的状态。

您可以做的是将一个名为status的属性添加到Book类,并在您检出它的方法中,将其设置为false。打印时,您可以通过访问每个书籍对象中的此变量来打印每本书的状态。

答案 1 :(得分:1)

您正在针对库存储状态,而不是针对每本书。要查看单本图书,您需要将状态字段放入图书中,然后设置图书的状态:

public class Book
{
    public String status;
    // etc...
}

public String checkout(Book c) {
    c.status = "true";
    return c.status;
}

将一个布尔值存储为字符串通常不是最好的想法 - 你通常最好使用一个实际的布尔字段。

更清洁(更面向对象)的实现看起来像:

public class Book {

    private boolean checkedOut = false;

    public void checkout() {
        checkedOut = true;
    }

    public void checkin() {
        checkedOut = false;
    }

    public boolean isCheckedOut() {
        return checkedOut;
    }
}

这保留了用书本身管理书籍的逻辑,并允许您轻松添加验证,例如,如果字段已设置为true,则阻止结帐。

答案 2 :(得分:1)

我认为,以下代码可以帮助您:

public class Book {
    private boolean status;

    public boolean getStatus() {
        return this.status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }
}

public class Library {
    public  boolean checkout(Book c) {
         c.setStatus(true);
         return c.getStatus(); 
    }
}

我建议您使用boolean原始类型而不是String对象。