Skip to content

Instantly share code, notes, and snippets.

@simonharrer
Created October 31, 2012 09:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save simonharrer/3986086 to your computer and use it in GitHub Desktop.
Save simonharrer/3986086 to your computer and use it in GitHub Desktop.
Simple Calculator as a Java Webservice
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class Calculator {
public static void main(String[] args) {
Calculator.calculate(1, 1, "ADD");
Calculator.calculate(2, 3, "MULT");
Calculator.calculate(5, 2, "asdf");
Endpoint.publish("http://localhost:12345/calc", new Calculator());
}
public int compute(int x, int y, String operation) {
return Calculator.calculate(x, y, operation);
}
public static int calculate(int x, int y, String operation) {
int result;
String op;
if ("ADD".equals(operation)) {
result = x + y;
op = "+";
} else if ("SUB".equals(operation)) {
result = x - y;
op = "-";
} else if ("MULT".equals(operation)) {
result = x * y;
op = "*";
} else if ("DIV".equals(operation)) {
result = x / y;
op = "/";
} else {
// defaults to SUB
result = x - y;
op = "-";
}
log(x, y, result, op);
return result;
}
private static void log(int x, int y, int result, String op) {
System.out.format("%d %s %d = %d%n", x, op, y, result);
}
}
@GreatLegolas
Copy link

Hello,
Does it work on Java EE?

@simonharrer
Copy link
Author

Should do. But I don't know for sure as I've written this code 7 years ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment