I want to reduce the number of if statement in my code. I need some advice on how to do this. I have a simple form, when I click submit, it trigger a workflow for approval process.
public void initiateWorkflow(){
//some code here
//workflow is started
}
Next, when the approver login the application, he either approves or rejects the application. So if it approves it triggers the following method:
public void approveRequest(String workflowId){
//some code here
//update status
update (param1,param2,param3,......);
}
After the approver approves the request, I need to update status of the request. Let's say it was a Parking form. The user applies for parking request. After approval, I need to update status in the parking table to approved.
So I've implemented a method to update the status using JPA:
public <T> Boolean update(Class<T> entityClass,String fldName, String idFldName,String fldValue,String id) {
Query q = entityManager.createQuery("update "+entityClass.getSimpleName()+" set "+ fldName+" ='"+fldValue+"' WHERE "+idFldName+"=:id");
q.setParameter( "id", id);
q.executeUpdate();
}
So in my method approveRequest(..), I will call this method update with the parameters required to do the job. My question I have different modules that will use this workflow.. for e.g Parking, leave, loan and so on. So I will need to call this method update in my method approve request several times with the corresponding parameter. As such there will be lot of if statement.
public void approveRequest(String workflowId){
//some code here
//update status
if(application="parking")
update (param1,param2,param3,......);
elseif (application="loan")
update
elseif .....
}
So how can I avoid such things? It does not look good professionally writing codes with lots of if statement. I would be glad if someone can help on this.