0

I want to be able to retrieve the value of the body mass index and use it to render a comment as per the range obtained after the calculation.

The code I have below does not achieve it. I tried to put a switch statement in there, but it claims, modelmap is null, I got that. But, I still need to get this task done. I believe I should be using something different from modelmap.

@RequestMapping(value = "/")
        public String index(@RequestParam(required = false) Integer weight,
                @RequestParam(required = false) Double height,
                @RequestParam(required = false) String unitweight,
                @RequestParam(required = false) String unitheight,
                @RequestParam(required = false) String gender,
                @RequestParam(required = false) String comment,
                ModelMap modelMap) {

            if(weight!=null&&height!=null) {

                ImcCalculadora bmIcalc = new ImcCalculadora();

                modelMap.put("bmi", "O Seu Indice de massa corporal é: " + bmIcalc.getBmiKgCm(weight, height, unitweight, unitheight, comment));
                
                                
                modelMap.put("weight", weight);
                modelMap.put("height", height);
                modelMap.put("unitweight", unitweight);
                modelMap.put("unitheight", unitheight);
                modelMap.put("comment", comment);
                modelMap.put("gender", gender );
                modelMap.put("genderimg", gender+".png");
    
    
            }

            return "index";
        }

Below is my model code:

public double getBmiKgCm(int weight, double height, String unitweight,String unitheight, String comment) {   
                        
            if (height == 0 || weight == 0) {
                System.out.println("Valor Incorrecto");
            }

            double factor1 = unitheight.equals("cm") ? 1 : 2.54 ;
            double factor2 = unitweight.equals("kg") ? 1 : 2.20462262 ;

            double bmi = (weight/factor2) / (height * height);
            double roundOff = Math.round(bmi * 100.0) / 100.0;   
            
            if (roundOff < 18.5) {
                System.out.println("less than 18.5");
                comment = "Underweight";
            } else if ((roundOff >= 18.5 || (bmi) <= 24.9)) {
                System.out.println("between 18.5 and 24.9");
                comment = "Normal";
            } else if (roundOff >= 25 || bmi <= 29.9) {
                System.out.println("between 25 and 29.9");
                comment = "Overweight";
            } else {
                System.out.println("greater than 30");
                comment = "Obese";
            }              

            return roundOff;
        }
Amir Schnell
  • 611
  • 4
  • 11
Carlos Pimentel
  • 381
  • 5
  • 11
  • I do not quite get what exactly you are trying to do with your `modelMap`. How do you plan on using it? Your ModelMap is null because it isn't filled automatically by spring. You either make it a local variable, or annotatte it if you want to pass it within the request – Amir Schnell Dec 07 '20 at 10:40
  • Hi @Amir. I just built a simple body mass index. It works fine. I am new to spring though. So I want to instead of having the code I show above. When a user calculates their bmi, to also include a comment message. I do not know if I can do that with modelmap. Let me put my model code. You can see in the model I have actually if else statements for now, which I will transform in switch statement later on. The result of the bmi calculation is displayed but not the messages on the if else. I.E( Is the user normal, overweight, or underwait, etc). – Carlos Pimentel Dec 07 '20 at 10:45
  • I guess your comment is not picked up in the current code, because you pass the comment as String into `getBmiKgCm` and then you assign it a value. This is valid in Java, but does not do anything for you, because this is not propagated to the calling method. Read up on: https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Amir Schnell Dec 07 '20 at 11:06

0 Answers0