From 2484cd63fe99f3166a693ddb115e162f2e9b8d15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C5=82a=C5=BCej=20Rybarkiewicz?= Date: Sun, 24 May 2020 16:32:11 +0200 Subject: [PATCH] Initial commit --- .gitignore | 3 ++ pom.xml | 67 +++++++++++++++++++++++++++++++++++++++++ src/main/java/Main.java | 37 +++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/Main.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9a8e7f4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +target +java-xml-json-projekt-2.iml \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..901c8d8 --- /dev/null +++ b/pom.xml @@ -0,0 +1,67 @@ + + + 4.0.0 + + org.example + java-xml-json-projekt-2 + 1.0-SNAPSHOT + + 1.6 + 1.6 + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + package + + single + + + ${project.artifactId}-${project.version} + false + false + + + + Main + + + + + jar-with-dependencies + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + junit + junit + 4.12 + test + + + org.json + json + 20200518 + + + + \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..cf61ce8 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,37 @@ +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Scanner; +import org.json.JSONObject; + +public class Main { + public static void main(String[] args) { + try { + InputStream response = new URL("https://api.exchangeratesapi.io/latest").openStream(); + Scanner s = new Scanner(response).useDelimiter("\\A"); + String result = s.hasNext() ? s.next() : ""; + JSONObject rates = new JSONObject(result).getJSONObject("rates"); + Scanner inScanner = new Scanner(System.in); // Create a Scanner object + + while (true) { + System.out.println("Wpisz 'quit' aby zakończyć działania programu."); + System.out.println("Podaj kod waluty:"); + String code = inScanner.nextLine().trim().toUpperCase(); + if (code.equals("QUIT")) { + System.out.println("Papa."); + System.exit(0); + } else if (rates.has(code)) { + System.out.printf("Dzisiejszy kurs EUR/%s to %.6f\n", code, rates.getBigDecimal(code)); + } else { + System.out.printf("Nie znaleziono pary EUR/%s. Podaj inną walutę\n", code); + } + } + + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } +}