fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class Ideone {
  8. public static void main (String[] args) throws java.lang.Exception {
  9. for(int bmiIndex : Arrays.asList(5,6,21,25,29,32,90)){
  10. BMI bmi = BMI.getState(bmiIndex);
  11. System.out.println("Status for BMI " + bmiIndex + " is: " + bmi.getDescription());
  12. }
  13. }
  14. }
  15.  
  16. enum BMI {
  17. UNKNOWN(Integer.MAX_VALUE, -1, "Unknown"),
  18. UNDERWEIGHT(0, 19, "Underweight"),
  19. NORMAL(20, 26, "Normal"),
  20. OVERWEIGHT(26, 31, "Overweight"),
  21. OBESE(31, 44, "Obese"),
  22. DEAD(45, Integer.MAX_VALUE, "Probably already dead");
  23.  
  24. private final int lower;
  25. private final int upper;
  26. private final String description;
  27.  
  28. private BMI(int lower, int upper, String description){
  29. this.lower = lower;
  30. this.upper = upper;
  31. this.description = description;
  32. }
  33.  
  34. int getLowerBound() {return lower;}
  35. int getUperBound() {return upper;}
  36. String getDescription() {return description;}
  37.  
  38. private boolean isInRange(int bmiIndex){
  39. return lower <= bmiIndex && bmiIndex < upper;
  40. }
  41.  
  42. public static BMI getState(int bmiIndex){
  43. for(BMI bmi : BMI.values()){
  44. if(bmi.isInRange(bmiIndex)){
  45. return bmi;
  46. }
  47. }
  48. return UNKNOWN;
  49. }
  50. }
Success #stdin #stdout 0.08s 380160KB
stdin
Standard input is empty
stdout
Status for BMI 5 is: Underweight
Status for BMI 6 is: Underweight
Status for BMI 21 is: Normal
Status for BMI 25 is: Normal
Status for BMI 29 is: Overweight
Status for BMI 32 is: Obese
Status for BMI 90 is: Probably already dead