Skip to content

Instantly share code, notes, and snippets.

@manzaloros
Last active December 8, 2020 22:48
Show Gist options
  • Save manzaloros/abead71fe32136b2ee19f1488f01938d to your computer and use it in GitHub Desktop.
Save manzaloros/abead71fe32136b2ee19f1488f01938d to your computer and use it in GitHub Desktop.
Spring Calculator
package com.galvanize.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
import static java.lang.Integer.parseInt;
@RestController
@RequestMapping("/math")
public class PiController {
@GetMapping("/pi")
public String returnPi() {
return "3.141592653589793";
}
@GetMapping("/calculate")
public String math(@RequestParam Map map) {
int x = parseInt((String) map.get("x"));
int y = parseInt((String) map.get("y"));
int result;
if (map.containsKey("operation")) {
String operation = (String) map.get("operation");
switch (operation) {
case "add":
result = x + y;
return x + " + " + y + " = " + result;
case "multiply":
result = x * y;
return x + " * " + y + " = " + result;
case "subtract":
result = x - y;
return x + " - " + y + " = " + result;
case "divide":
result = x / y;
return x + " / " + y + " = " + result;
}
}
result = x + y;
return x + " + " + y + " = " + result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment