simplify controller method
@@ -24,15 +24,23 @@ public class CustomResponseController {
@GetMapping("/age")
public ResponseEntity<String> age(@RequestParam("yearOfBirth") int yearOfBirth) {
- int currentYear = Year.now().getValue();
-
- if (currentYear < yearOfBirth) {
+ if (isInFuture(yearOfBirth)) {
return new ResponseEntity<>("Year of birth cannot be in the future", HttpStatus.BAD_REQUEST);
}
- int age = currentYear - yearOfBirth;
+ return new ResponseEntity<>("Your age is " + calculateAge(yearOfBirth), HttpStatus.OK);
+ }
+
+ private int calculateAge(int yearOfBirth) {
+ return currentYear() - yearOfBirth;
+ }
+
+ private boolean isInFuture(int year) {
+ return currentYear() < year;
+ }
- return new ResponseEntity<>("Your age is " + age, HttpStatus.OK);
+ private int currentYear() {
+ return Year.now().getValue();
}
@GetMapping("/customHeader")
@@ -47,7 +55,8 @@ public class CustomResponseController {
public void manual(HttpServletResponse response) throws IOException {
response.setHeader("Custom-Header", "foo");
response.setStatus(200);
- response.getWriter().println("Hello World!");
+ response.getWriter()
+ .println("Hello World!");
}
}
