topic.java

来自「JAVA Servlet2.3外文书籍源码」· Java 代码 · 共 105 行

JAVA
105
字号
package forum;

import java.util.*;

/**
 * Represents a topic in the discussion forum.
 *
 * @author  Simon Brown
 */
public class Topic extends Posting {

  /** the topic title */
  private String title;

  /** a collection of all responses made to this topic */
  private ArrayList responses = new ArrayList();

  /**
   * Creates a new topic with the specified information.
   *
   * @param the     id for the new topic
   * @param user    the User instance representing the topic starter/author
   * @param title   the title of the new topic
   * @param text    the first posting in the topic
   */
  public Topic(int id, User user, String title, String text) {
    super(user, text);

    this.id = id;
    this.title = title;
  }

  /**
   * Getter for the title property.
   *
   * @return  the title of this topic
   */
  public String getTitle() {
    return this.title;
  }

  /**
   * Adds a response to this topic.
   *
   * @param response    the Response to be added
   */
  public void add(Response response) {
    responses.add(response);

    response.setId(responses.size()-1);
    response.setTopic(this);
  }

  /**
   * Removes a response from this topic.
   *
   * @param response    the Response to be removed
   */
  public void remove(Response response) {
    response.setDeleted(true);
  }

  /**
   * Gets a specific response.
   *
   * @param id    the id of the response required
   * @return  a Response instance
   */
  public Response getResponse(int id) {
    return (Response)responses.get(id);
  }

  /**
   * Gets a collection of all responses to this topic.
   *
   * @return  a Collection containing Response instances
   */
  public Collection getResponses() {
    return responses;
  }

  /**
   * Gets the number of (non-deleted) responses that have been made
   * to this topic.
   *
   * @return  the number of responses that have not been deleted
   */
  public int getResponseCount() {
    int count = 0;
    Response response;

    Iterator it = responses.iterator();
    while (it.hasNext()) {
      response = (Response)it.next();

      // do not include in the count those responses that have been deleted
      if (!response.isDeleted()) {
        count++;
      }
    }

    return count;
  }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?