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

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

import javax.ejb.*;

public interface PriceHome extends javax.ejb.EJBLocalHome {
  public Price create(int pricecode) throws CreateException;
  public Price findByPrimaryKey(PricePK pk) throws FinderException;
}
Localインターフェース
package refactor;

public interface Price extends javax.ejb.EJBLocalObject {
  public int getPricecode();
  public double getCharge(int daysRented);
  public int getFrequentRenterPoints(int daysRented);
}
Beanクラス
package refactor;

import javax.ejb.*;

abstract public class PriceBean implements EntityBean {
  EntityContext entityContext;
  public PricePK ejbCreate(int pricecode) throws CreateException {
    setPricecode(pricecode);
    return null;
  }
  public void ejbPostCreate(int pricecode) throws CreateException {
  }
  public void ejbRemove() throws RemoveException {
  }
  public abstract void setPricecode(int pricecode);
  public abstract int getPricecode();
  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;
  }

  /**
    * @param daysRented
    * @return
    */
  public double getCharge(int daysRented) {
    PricePK pk = (PricePK)entityContext.getPrimaryKey();
    if( pk.equals(PricePK.pkREGULAR) ) {
      double result = 2;
      if( daysRented > 2 )
        result += (daysRented - 2) * 1.5;
      return result;
    }
    else if( pk.equals(PricePK.pkNEW_RELEASE) ) {
      return daysRented * 3;
    }
    else if( pk.equals(PricePK.pkCHILDRENS) ) {
      double result = 1.5;
      if( daysRented > 3 )
        result += (daysRented - 3) * 1.5;
      return result;
    }
    throw new EJBException("Price Code Error!! : " + pk);
  }

  /**
    * @param daysRented
    * @return
    */
  public int getFrequentRenterPoints(int daysRented) {
    PricePK pk = (PricePK)entityContext.getPrimaryKey();
    if( pk.equals(PricePK.pkREGULAR) )
      return 1;
    else if( pk.equals(PricePK.pkNEW_RELEASE) )
      return (daysRented > 1) ? 2 : 1; // 新作を二日以上借りた場合はボーナスポイント
    else if( pk.equals(PricePK.pkCHILDRENS) )
      return 1;
    throw new EJBException("Price Code Error!! : " + pk);
  }
}
プライマリキークラス
package refactor;

import java.io.*;

public class PricePK implements Serializable {
  public static final int CHILDRENS = 2;
  public static final int REGULAR = 0;
  public static final int NEW_RELEASE = 1;
  public static final PricePK pkCHILDRENS = new PricePK(CHILDRENS);
  public static final PricePK pkREGULAR = new PricePK(REGULAR);
  public static final PricePK pkNEW_RELEASE = new PricePK(NEW_RELEASE);

  public int pricecode;

  public PricePK() {
  }
  public PricePK(int pricecode) {
    this.pricecode = pricecode;
  }
  public boolean equals(Object obj) {
    if (obj != null) {
      if (this.getClass().equals(obj.getClass())) {
        PricePK that = (PricePK) obj;
        return this.pricecode == that.pricecode;
      }
    }
    return false;
  }
  public int hashCode() {
    return pricecode;
  }
}

[ 戻る ]