Last active
December 8, 2020 22:48
-
-
Save manzaloros/abead71fe32136b2ee19f1488f01938d to your computer and use it in GitHub Desktop.
Spring Calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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