diff --git a/src/main/java/com/gmgauthier/CalculatorApp.java b/src/main/java/com/gmgauthier/CalculatorApp.java new file mode 100644 index 0000000..7f97e39 --- /dev/null +++ b/src/main/java/com/gmgauthier/CalculatorApp.java @@ -0,0 +1,20 @@ +package com.gmgauthier; + +import com.gmgauthier.client.CalculatorClient; + +import java.io.IOException; +import java.net.URISyntaxException; + +public class CalculatorApp { + + public static void main(String[] args) + throws URISyntaxException, IOException, InterruptedException { + CalculatorClient cc = new CalculatorClient(); + + System.out.println(cc.getSum(10,20)); + System.out.println(cc.getProduct(123, 444)); + System.out.println(cc.getDifference(846, 233)); + System.out.println(cc.getQuotient(8, 2)); + } + +} diff --git a/src/main/java/com/gmgauthier/client/CalculatorApp.java b/src/main/java/com/gmgauthier/client/CalculatorApp.java deleted file mode 100644 index 5910bd9..0000000 --- a/src/main/java/com/gmgauthier/client/CalculatorApp.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.gmgauthier.client; - -import org.json.JSONObject; - -import java.io.IOException; -import java.math.BigDecimal; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; - -public class CalculatorApp { - - private static final String ROOT_URL = "http://localhost:8000/"; - static HttpClient httpClient; - - public static void main(String[] args) - throws URISyntaxException, IOException, InterruptedException { - httpClient = HttpClient.newHttpClient(); - - System.out.println(getSum(10,20)); - System.out.println(getProduct(123, 444)); - System.out.println(getDifference(846, 233)); - System.out.println(getQuotient(999, 4)); - - } - - public static Integer getSum(Integer operanda, Integer operandb) - throws URISyntaxException, IOException, InterruptedException { - URI addUrl = new URI(ROOT_URL + "sum"); - JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb}); - return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("sum"); - } - - public static Integer getDifference(Integer operanda, Integer operandb) - throws URISyntaxException, IOException, InterruptedException { - URI addUrl = new URI(ROOT_URL + "difference"); - JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb}); - return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("difference"); - } - - public static Integer getProduct(Integer operanda, Integer operandb) - throws URISyntaxException, IOException, InterruptedException { - URI addUrl = new URI(ROOT_URL + "product"); - JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb}); - return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("product"); - } - - public static BigDecimal getQuotient(Integer operanda, Integer operandb) - throws URISyntaxException, IOException, InterruptedException { - URI addUrl = new URI(ROOT_URL + "quotient"); - JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb}); - return (BigDecimal) new JSONObject(makeRequest(addUrl, postJson).body()).get("quotient"); - } - - private static HttpResponse makeRequest(URI uri, JSONObject jsonBody) - throws IOException, InterruptedException { - HttpRequest httpRequest = HttpRequest.newBuilder() - .header("Content-Type","application/json") - .uri(uri) - .POST(HttpRequest.BodyPublishers.ofString(String.valueOf(jsonBody))) - .build(); - return httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); - } - -}