clean up app, and change quotient values to hide bug

This commit is contained in:
Greg Gauthier 2023-05-26 19:44:46 +01:00
parent 65dfd81d83
commit 137b1ecfc9
2 changed files with 20 additions and 67 deletions

View File

@ -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));
}
}

View File

@ -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<String> 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());
}
}