アクションで許可されているタスクを検索する Java の以下のサンプル コードを検討してください。
public boolean acceptableTaskForAction(String taskName,String actionName) {
String[] allowedActions;
switch (taskName){
case "Payment" :
allowedActions = { "full-payment", "bill-payment"};
case "Transfer" :
allowedActions = { "transfer-to-other", "tarnsfer-to-own"};
}
for (String action : allowedActions){
if (actionName.equals(action)){
return true;
}
}
return false;
}
ご存知のように、上記は次のようにコンパイルされませんArray constants can only be used in initializers
さまざまなパラメーターを定義することを考えたので、
public boolean acceptableTaskForAction(String taskName,String actionName) {
String[] allowedActionsForPayment= { "full-payment", "payment"};
String[] allowedActionsForTransfer= { "transfer-to-other", "tarnsfer-to-own"};
String[] allowedActions={};
switch (taskName){
case "Payment" :
allowedActions = allowedActionsForPayment;
case "Transfer" :
allowedActions = allowedActionsForTransfer;
}
for (String action : allowedActions){
if (actionName.equals(action)){
return true;
}
}
return false;
}
他の解決策を考えますか!? 最善の解決策は何だと思いますか?