Consider below sample code in java which wants to search which task are allowed in the action
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;
}
As you know the above will not compile as Array constants can only be used in initializers
I thought of defining different parameters so it will be
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;
}
Do you think of other solutions!? What do you think is the best solution?