dorobić pętlę pytającą o waluty
This commit is contained in:
commit
b048d12e95
2
ZJJX-Projekt2.iml
Normal file
2
ZJJX-Projekt2.iml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4" />
|
36
pom.xml
Normal file
36
pom.xml
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>ZJJX-Projekt2</groupId>
|
||||||
|
<artifactId>ZJJX-Projekt2</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.json</groupId>
|
||||||
|
<artifactId>javax.json-api</artifactId>
|
||||||
|
<version>1.1.4</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.glassfish</groupId>
|
||||||
|
<artifactId>javax.json</artifactId>
|
||||||
|
<version>1.1</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>8</source>
|
||||||
|
<target>8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
9
src/main/java/ApiException.java
Normal file
9
src/main/java/ApiException.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
public class ApiException extends RuntimeException {
|
||||||
|
public ApiException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ApiException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
5
src/main/java/ConnectionFactory.java
Normal file
5
src/main/java/ConnectionFactory.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
public class ConnectionFactory {
|
||||||
|
public HttpConnection build(String url) {
|
||||||
|
return new HttpConnection(url);
|
||||||
|
}
|
||||||
|
}
|
29
src/main/java/EcbApi.java
Normal file
29
src/main/java/EcbApi.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import javax.json.Json;
|
||||||
|
import javax.json.JsonObject;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
public class EcbApi implements ExchangeApi {
|
||||||
|
|
||||||
|
private static final String API_URL_TEMPLATE = "https://api.exchangeratesapi.io/latest";
|
||||||
|
|
||||||
|
private final ConnectionFactory connectionFactory;
|
||||||
|
private final JsonObject json;
|
||||||
|
|
||||||
|
public EcbApi() {
|
||||||
|
this(new ConnectionFactory(), Json.createObjectBuilder().build());
|
||||||
|
}
|
||||||
|
|
||||||
|
public EcbApi(ConnectionFactory connectionFactory, JsonObject json) {
|
||||||
|
this.connectionFactory = connectionFactory;
|
||||||
|
this.json = json;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BigDecimal exchangeRate(String currencyCode) {
|
||||||
|
try (HttpConnection connection = connectionFactory.build(API_URL_TEMPLATE)) {
|
||||||
|
String response = connection.response();
|
||||||
|
RatesProcessing rates = new RatesProcessing(response, currencyCode);
|
||||||
|
return rates.getRate(currencyCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
5
src/main/java/ExchangeApi.java
Normal file
5
src/main/java/ExchangeApi.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
public interface ExchangeApi {
|
||||||
|
BigDecimal exchangeRate(String currencyCode);
|
||||||
|
}
|
51
src/main/java/HttpConnection.java
Normal file
51
src/main/java/HttpConnection.java
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
public class HttpConnection implements Closeable {
|
||||||
|
|
||||||
|
private final HttpURLConnection connection;
|
||||||
|
|
||||||
|
HttpConnection(String url) {
|
||||||
|
try {
|
||||||
|
connection = (HttpURLConnection) new URL(url).openConnection();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new ApiException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String response() {
|
||||||
|
validateResponse();
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
try (BufferedReader responseReader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
|
||||||
|
String line;
|
||||||
|
while ((line = responseReader.readLine()) != null) {
|
||||||
|
response.append(line);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new ApiException(e);
|
||||||
|
}
|
||||||
|
return response.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
connection.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
void validateResponse() {
|
||||||
|
try {
|
||||||
|
int responseCode = connection.getResponseCode();
|
||||||
|
if (responseCode != 200) {
|
||||||
|
String responseMessage = connection.getResponseMessage();
|
||||||
|
throw new ApiException(String.format("Something went wrong! [%d] %s", responseCode, responseMessage));
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new ApiException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
6
src/main/java/Main.java
Normal file
6
src/main/java/Main.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
EcbApi getRates = new EcbApi();
|
||||||
|
System.out.println(getRates.exchangeRate("PLN"));
|
||||||
|
}
|
||||||
|
}
|
35
src/main/java/RatesProcessing.java
Normal file
35
src/main/java/RatesProcessing.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import javax.json.Json;
|
||||||
|
import javax.json.stream.JsonParser;
|
||||||
|
import java.io.StringReader;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class RatesProcessing {
|
||||||
|
private Map<String, BigDecimal> rates;
|
||||||
|
|
||||||
|
public RatesProcessing(String result, String currencyCode) {
|
||||||
|
this.rates = new HashMap<>();
|
||||||
|
|
||||||
|
final JsonParser parser = Json.createParser(new StringReader(result));
|
||||||
|
String key = null;
|
||||||
|
BigDecimal value = null;
|
||||||
|
while (parser.hasNext()) {
|
||||||
|
final JsonParser.Event event = parser.next();
|
||||||
|
switch (event) {
|
||||||
|
case KEY_NAME:
|
||||||
|
key = parser.getString();
|
||||||
|
break;
|
||||||
|
case VALUE_NUMBER:
|
||||||
|
value = parser.getBigDecimal();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
rates.put(key, value);
|
||||||
|
}
|
||||||
|
parser.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigDecimal getRate(String currencyCode) {
|
||||||
|
return rates.get(currencyCode);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user