vendredi 28 septembre 2018

How to introduce a new if-condition for a specified case without any business logic given to implement that case?

Essentially, I've been asked to solve a programming quiz where the task is to introduce a new status with id 10 (dtl.getOpStatId() is 10) in the code down below. I've been asked to make any necessary changes to this code to add the required functionality( to add the additional operation stat id). Is there something obvious I'm missing????

I was given the following hint: if the only thing you are doing is adding dtl.getOpStatId() == 10 to the code below, you are not doing it correctly.

public class ChangeMe {

public void doOp(Operation op, Map<Long, String> reliefOperationMap, OperationSummary sum) {
    for (int d = 0; d < op.getOperationDetails().size(); ) {
        OperationDetail dtl = op.getOperationDetails().get(d);
        if (dtl.getOpStatId() == 1 || dtl.getOpStatId() == 3 || dtl.getOpStatId() == 4) {
            sum.setOpCond(true);
            if (dtl.getRelblkId() != null && dtl.getOperationsumRefId() != null && dtl.getOperationsumParentId() != null) {
                sum.setIsReliefOperation("Y");
                reliefOperationMap.put(dtl.getOperationsumId(), "Y");
            }
        } else {
            if (dtl.getRelblkId() != null && dtl.getOperationsumRefId() != null && dtl.getOperationsumParentId() != null) {
                sum.setOpCond(true);
                sum.setIsReliefOperation("Y");
                reliefOperationMap.put(dtl.getOperationsumId(), "Y");
            } else {
                sum.setOpCond(true);
            }
        }
        break;
      }
   }
}

This what I have so far. I can't think of anything else considering there are no implementations given for the objects that are being passed to this method (doOp()).

public static void doOp(Operation op, Map<Long, String> reliefOperationMap, OperationSummary sum) {

/*Missing increment condition from for loop, will be endlessly looping on 
the first element of the ArrayList (op.getOperationDetails())*/

    for (int d = 0; d < op.getOperationDetails().size(); d++ ) {
        OperationDetail dtl = op.getOperationDetails().get(d);
        if (dtl.getOpStatId() == 1 || dtl.getOpStatId() == 3 || dtl.getOpStatId() == 4) {
            sum.setOpCond(true);
            if (dtl.getRelblkId() != null && dtl.getOperationsumRefId() != null && dtl.getOperationsumParentId() != null) {
                sum.setIsReliefOperation("Y");
                reliefOperationMap.put(dtl.getOperationsumId(), "Y");
                System.out.println("Reached 1 or 3 or 4");
            }
        }else if(dtl.getOpStatId() == 10){

            //business logic for OpStatId here

        }else {
            System.out.println("Reached anything but 1,3 or 4");
            if (dtl.getRelblkId() != null && dtl.getOperationsumRefId() != null && dtl.getOperationsumParentId() != null) {
                sum.setOpCond(true);
                sum.setIsReliefOperation("Y");
                reliefOperationMap.put(dtl.getOperationsumId(), "Y");
            } else {
                sum.setOpCond(true);
            }
        }
/*removed break from here, no point of having a for loop if you're only 
 ever checking the first element of the Arraylist*/
    }
}
}

To Recap my changes : *Added a 'd++' in the for loop, otherwise it would be looping forever on the first element of the List.

*Added an else-if(dtl.getOpStatId() == 10) condition with empty business logic...I know they said this is wrong but I can't think of anything else

*removed break condition otherwise would only check the first element of the loop and exit the for loop

Aucun commentaire:

Enregistrer un commentaire