lundi 30 juillet 2018

how to avoid multile if statements

I have next code:

  private static final List<String> VALUES =
  ImmutableList.of("15", "545", "856", "5", "4558"); 

  public int getAmountOfUnits(String value, int duration) {


if (VALUES.contains(value)) {


  if (duration >= 1 && duration <= 7) return 0;


  if (duration >= 8 && duration <= 82) {
    return getUnitAmount(duration, 22, 15, 1);
  }


  if (duration >= 83 && duration <= 90) return 7;


  if (duration >= 91) {
    return getUnitAmount(duration, 114, 24, 7);
  }
}



if (value.equalsIgnoreCase("3") && duration >= 0) {
  return getUnitAmount(duration, 15, 16, 1);
}


if (value.equalsIgnoreCase("4") && duration >= 0) {
  return getUnitAmount(duration, 59, 60, 0);
}

return 1;
  }

List item

private int getUnitAmount(int input, int startRange, int increment, int startUnit) {
  while (startRange < input) {
    startUnit++;
    startRange += increment;
  }
  return startUnit;
}

How I can avoid this multile line if statments?

Aucun commentaire:

Enregistrer un commentaire