Search code examples
javabooleanargumentsencapsulation

Passing boolean argument in method but method boolean variable is still unused


A bit of a Java noob here. So I am calling a method called ageRestrProcessor() that takes a boolean ageValidationStatus argument, into the Processing class. The Processing class has a boolean variable ageNotValidated already initialized to true. So now, I'm trying to pass the ageNotValidated variable into the ageRestrProcessor() method as an argument, and then within the method, trying to change the boolean value to false. But, in the method itself, it's saying the boolean variable ageValidationStatus is unused. So obviously that boolean value never gets changed. On the surface, it seems this should work but i can't figure out what the problem is. Thanks in advance for your responses.

//ItemProcessors class

public class ItemsProcessors {
  void ageRestrProcessor(int keyNum, String itemNameFromDB, double itemUnitPriceFromDB, boolean ageValidationStatus, boolean ageValidator, String itemUnit, Map<Integer, Object[]> map){
    double ageRstrItemPrice = bp.roundUpToTwoDecimals(itemUnitPriceFromDB);
    String dollarAgeRestPrice = bp.addDollarFormat(ageRstrItemPrice);

    if (ageValidator){
        System.out.println("\tAge Validation Successful");
        System.out.println("\tTotal Price: " + dollarAgeRestPrice);
        map.put(keyNum, new Object[] {itemNameFromDB, itemUnitPriceFromDB, itemUnit, ageRstrItemPrice, dollarAgeRestPrice});

        //Here, the compiler is telling me that ageNotValidated variable is never used.
        ageValidationStatus = false;
    }
    else {
        System.out.println("\tShopper is underage. Item not added");
    }
  }
}

//I'm calling the ageRestrProcessor method into this Processing class

public class Processing extends OrderData {
   private Map<Integer, Object[]> itemMap = new HashMap<Integer, Object[]>();
   BasePage bp = new BasePage();
   private Exceptions xcep = new Exceptions();
   private ItemsProcessors ip = new ItemsProcessors();
   public boolean ageNotValidated = true;

   public void itemsProcessor(XSSFSheet itemSheet){
      if (xcep.isAgeRestricted(itemSheet, rowNum) && ageNotValidated){
         String ageEntered = bp.getStringFromScanner("\tEnter DOB in 'mm-dd-yyyy' format: ");
         boolean isAgeValid = xcep.ageValidator(ageEntered);

         //I'm trying to pass the ageNotValidated variable into the method here.
         ip.ageRestrProcessor(keynum++, itemNameFromDB, itemUnitPriceFromDB, ageNotValidated, isAgeValid, itemUnit, itemMap);
      }
    }
}

Solution

  • ageValidationStatus is created inside ageRestrProcessor method and cannot be used outside this method( local variable) and it's not being used anywhere else in the current method as well

    so it's a warning by compiler to improve code quality , unnecessary assignment of value when it has no use elsewhere


    Make ageRestrProcessor return boolean flag and use value accordingly

      boolean ageRestrProcessor(int keyNum, String itemNameFromDB, double itemUnitPriceFromDB, boolean ageValidationStatus, boolean ageValidator, String itemUnit, Map<Integer, Object[]> map){
             //..code
             // you can remove  boolean ageValidationStatus from method signature
        if (ageValidator){
             //..code
             return false;
        }
        else {
            System.out.println("\tShopper is underage. Item not added");
            return true;
        }
      }
    

    and

    ageNotValidated = ip.ageRestrProcessor(keynum++,....;