Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. @Controller
  2. public class CalculatorController {
  3. @GetMapping("/calculator")
  4. public String index(Model model) {
  5. model.addAttribute("Gender", "1");
  6. model.addAttribute("Activity", "1");
  7. model.addAttribute("view", "calculator/calculator");
  8. return "base-layout";
  9. }
  10.  
  11. @PostMapping("/calculator")
  12. public String index(
  13. @RequestParam String Age,
  14. @RequestParam String Gender,
  15. @RequestParam String Weight,
  16. @RequestParam String Height,
  17. @RequestParam String Activity,
  18. Model model
  19. )
  20. {
  21. int years;
  22. double kg;
  23. double cm;
  24.  
  25. try {
  26.  
  27. years = Integer.parseInt(Age);
  28. }
  29. catch (NumberFormatException ex){
  30. years = 0;
  31. }
  32.  
  33. try {
  34.  
  35. kg = Double.parseDouble(Weight);
  36. }
  37. catch (NumberFormatException ex){
  38. kg = 0;
  39.  
  40. }
  41.  
  42. try {
  43.  
  44. cm = Double.parseDouble(Height);
  45. }
  46. catch (NumberFormatException ex){
  47.  
  48. cm = 0;
  49. }
  50.  
  51. Calculator calculator = new Calculator(
  52.  
  53. years,
  54. Gender,
  55. kg,
  56. cm,
  57. Activity
  58. );
  59.  
  60. double calories = calculator.calculateCal();
  61.  
  62.  
  63. model.addAttribute("Age", years);
  64. model.addAttribute("Gender",Gender);
  65. model.addAttribute("Activity",Activity);
  66. model.addAttribute("Weight", kg);
  67. model.addAttribute("Height", cm);
  68. model.addAttribute("calories", calories);
  69.  
  70.  
  71. model.addAttribute("view", "calculator/calculator");
  72. return "base-layout";
  73.  
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement