Customer ローカルエンティティビーン

[ 戻る ]
LocalHomeインターフェース
package refactor;

import javax.ejb.*;

public interface CustomerHome extends javax.ejb.EJBLocalHome {
  public Customer create(String custname) throws CreateException;
  public Customer findByPrimaryKey(String custname) throws FinderException;
}
Localインターフェース
package refactor;

import java.util.*;

public interface Customer extends javax.ejb.EJBLocalObject {
  public String getCustname();
  public void setRental(Collection rental); // Rentalエンティティビーンへのリレーションシップ
  public Collection getRental(); // Rentalエンティティビーンへのリレーションシップ
  public String statement();
  public String htmlStatement();
}
Beanクラス
package refactor;

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

abstract public class CustomerBean implements EntityBean {
  EntityContext entityContext;
  public java.lang.String ejbCreate(java.lang.String custname) throws CreateException {
    setCustname(custname);
    return null;
  }
  public void ejbPostCreate(java.lang.String custname) throws CreateException {
  }
  public void ejbRemove() throws RemoveException {
  }
  public abstract void setCustname(java.lang.String custname);
  public abstract void setRental(java.util.Collection rental);
                                              // Rentalエンティティビーンへのリレーションシップ

  public abstract java.lang.String getCustname();
  public abstract java.util.Collection getRental();
                                              // Rentalエンティティビーンへのリレーションシップ

  public void ejbLoad() {
  }
  public void ejbStore() {
  }
  public void ejbActivate() {
  }
  public void ejbPassivate() {
  }
  public void unsetEntityContext() {
    this.entityContext = null;
  }
  public void setEntityContext(EntityContext entityContext) {
    this.entityContext = entityContext;
  }

  /**
    * @return
    */
  private double getTotalCharge() {
    double result = 0;
    Iterator rentals = getRental().iterator();
    while( rentals.hasNext() ) {
      Rental each = (Rental)rentals.next();
      result += each.getCharge();
    }
    return result;
  }

  /**
    * @return
    */
  private int getTotalFrequentRenterPoints() {
    int result = 0;
    Iterator rentals = getRental().iterator();
    while( rentals.hasNext() ) {
      Rental each = (Rental)rentals.next();
      result += each.getFrequentRenterPoints();
    }
    return result;
  }

  /**
    * @return
    */
  public java.lang.String statement() {
    Iterator rentals = getRental().iterator();
    String result = "Rental Record for " + getCustname() + "\n";
    while( rentals.hasNext() ) {
    Rental each = (Rental)rentals.next();
      // この貸し出しに関する数値の表示
      result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(each.getCharge()) + "\n";
    }
    // フッタ部分の追加
    result += "Amount owed is " + String.valueOf(getTotalCharge()) + "\n";
    result += "You earned " + String.valueOf(getTotalFrequentRenterPoints()) + " frequent renter points";
    return result;
  }

  /**
    * @return
    */
  public java.lang.String htmlStatement() {
    Iterator rentals = getRental().iterator();
    String result = "<H1>Rentals for <EM>" + getCustname() + "</EM></H1><P>\n";
    while( rentals.hasNext() ) {
      Rental each = (Rental)rentals.next();
      // この貸し出しに関する数値の表示
      result += each.getMovie().getTitle() + ": " + String.valueOf(each.getCharge()) + "<BR>\n";
    }
    // フッタ部分の追加
    result += "<P>You owe <EM>" + String.valueOf(getTotalCharge()) + "</EM><P>\n";
    result += "On this rental you earned <EM>" + String.valueOf(getTotalFrequentRenterPoints()) +
    "</EM> frequent renter points<P>";
    return result;
  }
}

[ 戻る ]