入出力ページのデータや処理結果の保存に使用する Managed Beanを作成します。この項ではReserveBeanクラスを作成する手順を説明します。
なお、ReserveBean.javaの処理では、予約状況を管理するクラスとしてReserveCalendarクラスを使用しています。
ReserveCalendarクラスを作成する手順を説明します。
Interstage Studioのメイン画面で、[ファイル]メニューの[新規] > [クラス]を選択します。
以下のダイアログが表示されます。
設定する項目は以下のとおりです。
設定項目 | 設定内容 |
---|---|
ソースフォルダ | "hotelReserve/src"(必須) |
パッケージ | "hotelReserve"を指定します。 |
エンクロージング型 | チェックされていないことを確認してください。 |
名前 | "ReserveCalendar"を指定します。 |
修飾子 | "public"がチェックされていることを確認してください。また、"abstract"、"final"は非選択状態としてください。 |
スーパークラス | "java.lang.Object"となっていることを確認してください。 |
インタフェース | 空欄になっていることを確認してください。 |
作成するメソッドスタブの選択 | すべて非選択状態としてください。 |
コメントの生成 | チェックされていないことを確認してください。 |
[終了]をクリックしてひな型を作成します。Interstage Studioの左側に、"ReserveCalendar.java"が追加されていることを確認してください。
作成されたひな型に以下のソースをコピーしてください。
ReserveCalendar.java
/*
* 作成日:
*
* 生成されるコメントを変更したい場合は、ウィンドウ>設定>Java>テンプレート
* の"filecomment"テンプレート変数を編集してください。
* コメントを生成するかどうかについては、ウィンドウ>設定>Java>コード生成
* で設定してください。
*/ package hotelReserve; import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
* @author
*
* 生成されるコメントを変更したい場合は、ウィンドウ>設定>Java>テンプレート
* の"typecomment"テンプレート変数を編集してください。
* コメントを生成するかどうかについては、ウィンドウ>設定>Java>コード生成
* で設定してください。
*/ public class ReserveCalendar { public final static String RESERVE_CALENDAR_KEY = "hotelReserve.ReserveCalendar";
public final static int CALENDAR_DAYS = 10; //予約を扱う期間
public final static int ROOM_RESERVE_LIMIT = 6; //予約の限界
final static String[] ROOM_NAME = { "シングル(8000円/泊)","ツイン(20000円/泊)",
"ダブル(15000円/泊)"}; //部屋の種別
private final static String DATE_FORMAT = "yyyy年MM月dd日";
//日付
public String[] calendar = new String[CALENDAR_DAYS];
//室内の空き状況
private int[][] emptyRoom = new int[calendar.length][ROOM_NAME.length];
//コンストラクタ
public ReserveCalendar(){
//日付の取得
Calendar days = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
//実行日より10日間先を取得する
for (int x=0 ;x<calendar.length ;x++){
calendar[x] = formatter.format(days.getTime());
days.add(Calendar.DATE,1);
}
}
//室内の予約
//reservation(開始日,終了日,部屋種別)
public boolean reservation(int start,int end,int num,int roomType){
//予約状況のチェック
if (checkNoVacancy(start,end,num,roomType)){
return false;
}
//実行日より10日間先を取得する
for (int day=start ;day<end ;day++){
emptyRoom[day][roomType]+=num;
}
return true;
}
//予約状況の確認
//trueを戻すと、期間内の予約は可能
//(開始日から終了日までの間に予約の限界間で達している部屋があればfalse)
//reservation(開始日,終了日,部屋種別)
public boolean checkNoVacancy(int start,int end,int num,int roomType){
//実行日より10日間先を取得する
for (int day=start ;day<end ;day++){
//期間内に予約不可能な日付があるとfalseで復帰
if ( emptyRoom[day][roomType] + num >= ROOM_RESERVE_LIMIT ){
return true;
}
}
return false;
}
//指定された日付の予約状況の確認
//reservation(日付,部屋種別)
//戻り値 予約数
public int checkReservDay(int day,int roomType){
return emptyRoom[day][roomType];
}
public String[][] makeTableData(){
String[][] tableData = new String[CALENDAR_DAYS][ROOM_NAME.length+1];
for (int day=0;day<calendar.length;day++){
//テーブルに日付の挿入
tableData[day][0] = calendar[day];
//予約数の挿入
for (int room=0;room<ROOM_NAME.length;room++){
tableData[day][room+1] =
String.valueOf(checkReservDay(day,room));
}
}
return tableData;
} }
ReserveBeanクラスを作成する手順を説明します。
Interstage Studioのメイン画面で、[ファイル]メニューの[新規] > [クラス]を選択します。
以下のダイアログが表示されます。
設定する項目は以下のとおりです。
設定項目 | 設定内容 |
---|---|
ソースフォルダ | "hotelReserve/src"(必須) |
パッケージ | "hotelReserve"を指定します。 |
エンクロージング型 | チェックされていないことを確認してください。 |
名前 | "ReserveBean"を指定します。 |
修飾子 | "public"がチェックされていることを確認してください。また、"abstract"、"final"は非選択状態としてください。 |
スーパークラス | "java.lang.Object"となっていることを確認してください。 |
インタフェース | 空欄になっていることを確認してください。 |
作成するメソッドスタブの選択 | すべて非選択状態としてください。 |
コメントの生成 | チェックされていないことを確認してください。 |
[終了]をクリックしてひな型を作成します。Interstage Studioの左側に、"ReserveBean.java"が追加されていることを確認してください。
作成されたひな型にプロパティを追加していきます。追加するプロパティは次のとおりです。
型 | プロパティ名 | 説明 |
---|---|---|
com.fujitsu.uji.compo.ComboBox | arrivalDay | 到着日を表すコンボボックスです。 |
com.fujitsu.uji.compo.ComboBox | departureDay | 出発日を表すコンボボックスです。 |
com.fujitsu.uji.compo.ButtonList | roomType | 部屋の種別を表すボタンリストです。 |
com.fujitsu.uji.compo.ButtonList | options | 宿泊オプションを表すボタンリストです。 |
com.fujitsu.uji.compo.FieldString | message | エラーメッセージを表す文字列です。 |
com.fujitsu.uji.compo.FieldBigInteger | people | 宿泊人数を表す入力フィールドです。 |
com.fujitsu.uji.compo.TableView | reserveLookTable | 予約状況を表すテーブルです。 |
プロパティを追加するときに、Interstage Studioで自動的にアクセサメソッドを生成することができます。
まず、次のようにプロパティのフィールドを作成します。
ソースコードは以下のようになります。赤文字部分はひな形作成後に追加した部分です。
/*
* 作成日:
*
* 生成されるコメントを変更したい場合は、ウィンドウ>設定>Java>テンプレート
* の"filecomment"テンプレート変数を編集してください。
* コメントを生成するかどうかについては、ウィンドウ>設定>Java>コード生成
* で設定してください。
*/ package hotelReserve; import com.fujitsu.uji.compo.ButtonList;
import com.fujitsu.uji.compo.ComboBox;
import com.fujitsu.uji.compo.FieldBigInteger;
import com.fujitsu.uji.compo.FieldString;
import com.fujitsu.uji.compo.TableView;
/**
* @author
*
* 生成されるコメントを変更したい場合は、ウィンドウ>設定>Java>テンプレート
* の"typecomment"テンプレート変数を編集してください。
* コメントを生成するかどうかについては、ウィンドウ>設定>Java>コード生成
* で設定してください。
*/ public class ReserveBean { protected ComboBox arrivalDay;
protected ComboBox departureDay;
protected ButtonList roomType;
protected ButtonList options;
protected FieldBigInteger people;
protected FieldString message;
protected TableView reserveLookTable;
}
次に[ソース]メニューの[GetterおよびSetterの生成]を選択します。
ここで[すべて選択]ボタンをクリックして、[OK]ボタンをクリックするとフィールドに対応したアクセサメソッドが自動的に生成されます。
[作成するGetterおよびSetterの選択]項目以外の設定する項目はデフォルト設定値のままです。
次に、予約確認画面で表示する内容を取得するために以下の読み取り専用のプロパティを作成します。
ReserveBean.javaのソースは以下のとおりです。作成が完了したら[ファイル]>[保存]でファイルの保存を行います。
ソースコードは以下のようになります。赤文字部分は変更、追加した部分です。
ReserveBean.java
/* * 作成日: * * 生成されるコメントを変更したい場合は、ウィンドウ>設定>Java>テンプレート * の"filecomment"テンプレート変数を編集してください。 * コメントを生成するかどうかについては、ウィンドウ>設定>Java>コード生成 * で設定してください。 */ package hotelReserve; import java.util.Map;
import javax.faces.context.FacesContext; import com.fujitsu.uji.compo.ButtonList; import com.fujitsu.uji.compo.ComboBox; import com.fujitsu.uji.compo.FieldBigInteger; import com.fujitsu.uji.compo.FieldString; import com.fujitsu.uji.compo.TableView; /** * @author * * 生成されるコメントを変更したい場合は、ウィンドウ>設定>Java>テンプレート * の"typecomment"テンプレート変数を編集してください。 * コメントを生成するかどうかについては、ウィンドウ>設定>Java>コード生成 * で設定してください。 */ public class ReserveBean { protected ComboBox arrivalDay; protected ComboBox departureDay; protected ButtonList roomType; protected ButtonList options; protected FieldBigInteger people; protected FieldString message; protected TableView reserveLookTable; private int[] roomCharge = {8000,20000,15000};
private int[] optionCharge = {2000,-2000,1500,2000};
public ReserveBean() {
init();
} public ComboBox getArrivalDay() { return arrivalDay; } public void setArrivalDay(ComboBox arrivalDay) { this.arrivalDay = arrivalDay; } public ComboBox getDepartureDay() { return departureDay; } public void setDepartureDay(ComboBox departureDay) { this.departureDay = departureDay; } public FieldString getMessage() { return message; } public void setMessage(FieldString message) { this.message = message; } public ButtonList getOptions() { return options; } public void setOptions(ButtonList options) { this.options = options; } public FieldBigInteger getPeople() { return people; } public void setPeople(FieldBigInteger people) { this.people = people; } public TableView getReserveLookTable() { ReserveCalendar calendar = getReserveCalendar();
reserveLookTable.setValues(calendar.makeTableData());
return reserveLookTable; } public void setReserveLookTable(TableView reserveLookTable) { this.reserveLookTable = reserveLookTable; } public ButtonList getRoomType() { return roomType; } public void setRoomType(ButtonList roomType) { this.roomType = roomType; } public String getPeriodStr(){
int start = arrivalDay.getSelectedIndex();
int end = departureDay.getSelectedIndex();
String str = arrivalDay.getTextAt(start)+" から "+departureDay.getTextAt(end);
return str;
}
public String getRoomTypeStr(){
int type = roomType.getSelectedIndex();
String str = roomType.getTextAt(type);
return str;
}
public String getOptionsStr(){
//オプションの表示
int[] checkOptions = options.getSelectedIndexes();
String optionsStr = "なし";
if ( options.getSelectedIndexes() != null ){
optionsStr = "";
for (int x=0; x<checkOptions.length; x++){
optionsStr = optionsStr +
options.getTextAt(checkOptions[x]) + " ";
}
}
return optionsStr;
}
public String getCharge(){
int start = arrivalDay.getSelectedIndex();
int end = departureDay.getSelectedIndex();
int type = roomType.getSelectedIndex();
int charge = 0;
charge = roomCharge[type]* (end-start);
int[] checkOptions = options.getSelectedIndexes();
if ( options.getSelectedIndexes() != null ){
for (int x=0; x<checkOptions.length; x++){
charge = charge + optionCharge[checkOptions[x]];
}
}
return Integer.toString(charge);
}
//---------------------------------------------------------------
// 項目クラスの初期化
//---------------------------------------------------------------
public void init(){
ReserveCalendar calendar = getReserveCalendar();
String[] tableHeader = { "日付","シングル","ツイン","ダブル"};
reserveLookTable = new TableView(calendar.makeTableData());
reserveLookTable.setHeader(tableHeader);
arrivalDay = new ComboBox(calendar.calendar);
//到着日データの挿入
departureDay = new ComboBox(calendar.calendar);
//部屋
roomType = new ButtonList(ReserveCalendar.ROOM_NAME);
roomType.select("0");
//部屋
String[] optinsStr = { "ペット持ち込み(合計+2000円)","朝食抜き(合計-2000円)",
"温泉(合計+1500円)","インターネット(合計+2000円)"};
//部屋
options = new ButtonList(optinsStr);
//人数の入力
people = new FieldBigInteger("1");
message = new FieldString();
message.setVisible(false);
}
//---------------------------------------------------------------
// 予約カレンダーの取得
//---------------------------------------------------------------
public ReserveCalendar getReserveCalendar(){
FacesContext context = FacesContext.getCurrentInstance();
Map map = context.getExternalContext().getApplicationMap();
ReserveCalendar calendar =
(ReserveCalendar)map.get(ReserveCalendar.RESERVE_CALENDAR_KEY);
if(calendar == null){
calendar = new ReserveCalendar();
map.put(ReserveCalendar.RESERVE_CALENDAR_KEY,calendar);
}
return calendar;
} }
作成したBeanをJavaServer Facesアプリケーションで利用するためにManaged Beanに追加します。Managed Beanリストビュー上で右クリックし「Managed Beanの追加」を選択します。
以下のダイアログが表示されます。
上記画面で設定する項目は以下のとおりです。
設定項目 | 設定内容 | デフォルト設定値 |
---|---|---|
Bean名 | "reserveBean"を指定します。 | - |
クラス名 | "hotelReserve.ReserveBean"を指定します。 | - |
スコープ | "session"を選択して指定します。 | session |
すべての項目を入力したあと、[OK]ボタンをクリックします。
以下のようにManaged BeanリストビューにreserveBeanが追加されていることを確認してください。