mardi 12 février 2019

Alternative way of implementing nested if statement

I am trying to refactor this code I have below that uses several nested if statements to check if two list contains the same items.

                     List<Car> CarList = CarService.findCarByConfigtype(pageName);
        for (int i = 0; i < CarList.size(); i++) {
            System.out.println(CarRestApiController.data().getModel());
            if (CarList.get(i).getModel().equals(CarRestApiController.data().getModel())) {
                dataFound.add(CarList.get(i).getModel());
                if (CarList.get(i).getDerivative().equals(CarRestApiController.data().getDerivative())) {
                    dataFound.add(CarList.get(i).getDerivative());
                    if (CarList.get(i).getSvp().equals(CarRestApiController.data().getSvp())) {
                        dataFound.add(CarList.get(i).getSvp());
                        if (CarList.get(i).getEngine().equals(CarRestApiController.data().getEngine())) {
                            dataFound.add(CarList.get(i).getEngine());
                            if (CarList.get(i).getFueltype().equals(CarRestApiController.data().getFueltype())) {
                                dataFound.add(CarList.get(i).getFueltype());
                                if (CarList.get(i).getBodystyle().equals(CarRestApiController.data().getBodystyle())) {
                                    dataFound.add(CarList.get(i).getBodystyle());
                                    if (CarList.get(i).getTransmission().equals(CarRestApiController.data().getTransmission())) {
                                        dataFound.add(CarList.get(i).getTransmission());
                                        if (CarList.get(i).getSalescategory().equals(CarRestApiController.data().getSalescategory())) {
                                            dataFound.add(CarList.get(i).getSalescategory());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

Aucun commentaire:

Enregistrer un commentaire