为库程序中的书籍分配状态

时间:2015-12-09 22:22:29

标签: java arraylist

我是java的新手,很抱歉我的代码中存在不一致/混淆。

我正在创建一个简单的库程序,其中一个要求是显示库中的所有书籍及其状态(进出)

例如:

  

远大前程:IN

     

Kite Runner:OUT

     

呼啸山庄:OUT

然而,如果我只是检查一本书,它会将所有书籍状态“更改为”“更改”而不仅仅是其中一本。所以例如我继续得到:

  

远大前程:OUT

     

Kite Runner:OUT

     

呼啸山庄:OUT

在我的书类中,我有状态设置器/ getter,以及一个ArrayList BookList来保存所有的库书。

static ArrayList <String> BookList = new ArrayList <String> ();

public static String getStatus(String book)
    {
        return status; 
    }

  public static void setStatus(String newStatus)    
    {
        status = newStatus; 
    }

以下是赞助类中检查书籍的方法。

public static void CheckOutBook() 
{
      Scanner inputread = new Scanner(System.in);
      Scanner input = new Scanner(System.in);
      System.out.println("Enter full patron name: ");
      String borrower = inputread.nextLine();
      System.out.println("Enter book title to check out: ");
      String bookCheckOut = inputread.nextLine();

      if (Book.BookList.contains(bookCheckOut)) 
      {
        Book.BookList.remove(bookCheckOut);
        Book.setStatus("OUT");                   //?? doesnt work ??
        int bookIndex = Book.getIndexNumber(bookCheckOut);
        Book.setTrue(Book.BookList.get(bookIndex));
        Book.setBook(Book.BookList.get(bookIndex));
        Book.setBorrower(borrower);
        Book.setBook(bookCheckOut);
        System.out.println("----------" + bookCheckOut + " has been checked out!----------");

      }
}

如何才能使它只有数组列表中的指定书籍改变其状态?我试图创建不同的方法,但似乎不起作用:

Book.setTrue(bookCheckOut);              //true if book checked out
Book.BookList.get(indexNum).setTrue(bookCheckOut);
Book.setTrue(Book.BookList.get(i));

//here are some of the other attemps ive tried but they still dont change anything :(

书籍课程:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Book implements BookInterface {
  Scanner input = new Scanner(System.in);

  static ArrayList < String > UserList = new ArrayList < String > ();
  static ArrayList < String > BookList = new ArrayList < String > (); //display just titles// use when checking out books
  static ArrayList < String > OrigBookList = new ArrayList < String > (); //keep track of all titles ever entered

  public static String title;
  public String author;
  public static String book;
  public boolean checkIn;

  public static String status;
  public static String borrower;


  public Book(String t, String a) {
    title = t;
    author = a;
  }


  //constructor create new book
  public Book(String newTitle) {
    title = newTitle;
  }


  public String toString() {
    return title + " " + author;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public String getAuthor() {
    return author;
  }

  public void setAuthor(String author) {
    this.author = author;
  }

   public static String getStatus(String book) {
    return status;
  }

  public static void setStatus(String newStatus) 
    {
      status = newStatus;


  /**public static void setStatus(String newStatus)
    {
        status = newStatus; 
    }**/

  public static void setTrue(String bookCheckOut) {
    //status = true; 
  }

  public static void setFalse(String bookCheckIn) {
    //status = false; 
  }

  public static void setBorrower(String newBorrower) {
    borrower = newBorrower;
  }

  public static String getBorrower(String checkPatron) {
    return borrower;
  }

  public static String getBook(String checkPatron) {
    return book;
  }

  public static void setBook(String newBook) {
    book = newBook;
  }

  public static void addBook() {
    Scanner input = new Scanner(System.in);
    Scanner inputread = new Scanner(System.in);
    System.out.print("Enter book title: ");

    String title1 = inputread.nextLine();

    Scanner input1 = new Scanner(System.in);
    System.out.print("Enter book author: ");
    String author1 = inputread.next();

    Book fullBook = new Book(title1, author1); //create constructor w/ title & author
    Book book1 = new Book(title1); //constructor w/ just title to be used to display all books
    BookList.add(title1);
    OrigBookList.add(title1);
    //setStatus("IN");  //false = checked in 
    setFalse(title1);
    System.out.println("-----------------------------------------------");
    System.out.println("-----" + title1 + " is now in the library!-----");
    System.out.println("-----------------------------------------------");
  }

  public static void editBook() {
    Scanner inputread = new Scanner(System.in);
    System.out.println("Enter original book title: ");
    String origTitle = inputread.nextLine();
    System.out.println("Enter edited book title: ");
    String editedTitle = inputread.nextLine();
    Collections.replaceAll(Book.UserList, origTitle, editedTitle);
    System.out.println("------------------------------------------------------");
    System.out.println(origTitle + " has been changed to " + editedTitle + "!");
    System.out.println("------------------------------------------------------");

  }


  //print out list of books & if checked in or out
  public static void libraryInventory() {
    for (int i = 0; i <= OrigBookList.size() - 1; i++) {
      //Book Title: checked in/out
      System.out.println(OrigBookList.get(i) + ":" + getStatus(OrigBookList.get(i)));
    }
  }

  //method to go through BookList array and get index number for specifed 
  //book user has entered to check in/out 
  public static int getIndexNumber(String bookCheckOut) {
    int bookIndex = 0;
    for (int i = 0; i <= BookList.size() - 1; i++) {
      if (BookList.get(i).equals(bookCheckOut)) {
        bookIndex = i;
      }

    }
    return bookIndex;

  }

}

2 个答案:

答案 0 :(得分:1)

对于你的getStatus方法,你使用的是静态变量,这意味着它在alll Book实例中是相同的,你不想要它,只需从Book中的变量和方法中删除所有静态应该工作。

答案 1 :(得分:1)

为了对您的&#34;库&#34;进行建模,您需要拥有图书的实例。将您的Book类视为定义书籍内容的蓝图。然后,您需要使用此蓝图来创建实际的Book对象。

保持简单,这是Book类的一个例子。它定义了一本具有标题和签出状态的书。

public class Book {
  private String title;
  private boolean isCheckedOut;
  public Book(String title, boolean isCheckedOut) {
    this.title = title;
    this.isCheckedOut = isCheckdOut;
  }

  public void setTitle(String title) {
    this.title = title;
  }
  public String getTitle() {
    return title;
  }
  public void setCheckedOut(boolean isCheckedOut) {
    this.isCheckedOut = isCheckedOut;
  }
  public boolan isCheckedOut() {
    return isCheckedOut;
  }
  @Override
  public String toString() {
    return title + ": " + (isCheckedOut ? "OUT" : "IN");
  }
}

你会创建这样的特定书籍:

Book greatExpectations = new Book("Great Expectations", false);

这会创建一本标题为#34; Great Expectations&#34;的新书,但不会检出。 System.out.println(greatExpectations)会打印Great Expectations: OUT

因此,要设置您的&#34;库&#34;,请创建一系列图书并将其添加到List

List<Book> library = new ArrayList<>();
library.add(new Book("Great Expectations", false));
library.add(new Book("Moby Dick", false));
library.add(new Book("Catch-22", false));
library.add(new Book("The Great Gatsby", false));

现在,如果我想将 Catch-22 标记为已签出,我需要做的就是循环浏览列表,查找我正在寻找的书,并更改其选中的内容出州:

for(Book book : library) {
  if("Catch-22".equals(book.getTitle()) {
    book.setCheckedOut(true);
    break; // found the book, break out of the loop
  }
}

如果我现在打印出library的内容,我会看到只有 Catch-22 被标记为已签出:

System.out.println(library);

// prints:
// [Great Expectations: IN, Moby Dick: IN, Catch-22: OUT, The Great Gatsby: IN]

您的实现使用静态方法。静态方法不设置特定实例的值,而是设置在所有实例之间共享的值。

因此,就Book类而言,静态方法不是为特定书籍设置属性,而是所有书籍。这就是为什么当您尝试将图书设置为已签出时,它会将所有图书更新为已检出。