How to get the status of something in an arraylist

时间:2018-03-25 20:08:00

标签: java

So I have a project dealing with Books and their status as "INLIBRARY", "ONLOAN", or "LOST". I am trying to create a method that will return all books in that arraylist as well as their status. Here is my Catalog Class:

import java.util.*;
import javax.*;


public class Catalog extends JLabel{

ArrayList<Book> books;


public Catalog() {
    books = new ArrayList<Book>();
}//end constructor

public void addBook(Book b) {

    books.add(b);

}//end addBook

public String showAllBooks() {

    for(int i = 0; i < books.size(); i++)       
    return books.title + books.getStatus();


}//end showAllBooks


public String showLost() {
    for(int i = 0; i < books.size(); i++)
        if(i == Status.LOST)
    return books.get(i).status.LOST;

}


}//end class

As you can tell it does not accept this, but I am lost on what I need to do in order to get this to work because I have to create 3 other methods that are similar to this. Here is my Book class:

import java.util.ArrayList;

public class Book {


ArrayList<Author> authors = new ArrayList<Author>();
private int yearPublished;
private Status status;
private String title, isbn, publisherName;

public Status getStatus() {
    return this.status;
}//end getStatus

public Book() {
    title = null;
    isbn = null;
    yearPublished = 0;
    publisherName = null;
    status = null;
}//end book constructor

public Book(String title, String publisherName, int yearPublished, Status status, String isbn) {

    this.title = title;
    this.publisherName = publisherName;
    this.yearPublished = yearPublished;
    this.status = status;
    this.isbn = isbn;

}//end book constructor


public void loan() {
    status = status.ONLOAN;
}//end loan


public void inLibrary() {
    status = status.INLIBRARY;
}//end inLibrary


public void lost() {
     status = status.LOST;
}//end lost


public void addAuthor(Author a) {
    authors.add(a);

}//end addAuthor


public String listAuthors() {
    return authors.toString();

}//end listAuthors


public String printReferences() {
    return null;

}//end printReferences


}//end class

As for the "INLIBRARY", "ONLOAN", and "LOST" they are in a enum class as the 3 constants. So how exactly can I create the showAllBooks method to not only show all the books but their status as well. Thank you in advance for your advice/help.

2 个答案:

答案 0 :(得分:1)

Assuming you're trying to write the method to return a String with the book and its status, use for-each loop instead of a for loop, like so:

public String showAllBooks()
{
    StringBuilder sb = new StringBuilder();

    for(Book b : books)
    {
        sb.append("Name: " + b.title + "; Status: " + b.getStatus().name() + "\n");
    }

    return sb.toString().trim();
}

Obviously, change the String used in sb.append() to suit your needs.

答案 1 :(得分:0)

This cannot compile as title and getStatus() are not members of ArrayList but of Book :

public String showAllBooks() {

    for(int i = 0; i < books.size(); i++)       
    return books.title + books.getStatus();        
}

Besides you don't want to return the information for the first book but for all of them. So iterating and storing the information of each book in a StringBuilder and at last returning its toString() representation makes more sense :

public String showAllBooks() {
    StringBuilder msg = new StringBuilder();

    for(Book book : books) {
       // separator char between books
       if (msg.size() > 0){
         msg.append("-");    
       } 
       msg.append(book.getTitle() + book.getStatus());
    }     
    return msg.toString();       
}
相关问题