commit 3a7971336377cca5c8afb274b911df82153a2dc6 Author: Wiktor Gołąb Date: Sat Dec 16 08:47:55 2023 +0100 first commit diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..d34d995 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/itextpdf_5_5_4.xml b/.idea/libraries/itextpdf_5_5_4.xml new file mode 100644 index 0000000..405fcbe --- /dev/null +++ b/.idea/libraries/itextpdf_5_5_4.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/jdom2_2_0_5.xml b/.idea/libraries/jdom2_2_0_5.xml new file mode 100644 index 0000000..2455d47 --- /dev/null +++ b/.idea/libraries/jdom2_2_0_5.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/json_20231013.xml b/.idea/libraries/json_20231013.xml new file mode 100644 index 0000000..c826c8f --- /dev/null +++ b/.idea/libraries/json_20231013.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e42c9ea --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Pogoda_Poznań_JSON.json b/Pogoda_Poznań_JSON.json new file mode 100644 index 0000000..894ccd6 --- /dev/null +++ b/Pogoda_Poznań_JSON.json @@ -0,0 +1,5 @@ +{"Poznań": { + "ciśnienie": 1013, + "wilgotność": 95, + "temperatura": 2.7800000000000296 +}} \ No newline at end of file diff --git a/Pogoda_Poznań_PDF.pdf b/Pogoda_Poznań_PDF.pdf new file mode 100644 index 0000000..8178b0b Binary files /dev/null and b/Pogoda_Poznań_PDF.pdf differ diff --git a/Pogoda_Poznań_XML.xml b/Pogoda_Poznań_XML.xml new file mode 100644 index 0000000..6808e5b --- /dev/null +++ b/Pogoda_Poznań_XML.xml @@ -0,0 +1,6 @@ + + + 2.7800000000000296 + 95.0 + 1013.0 + diff --git a/README.md b/README.md new file mode 100644 index 0000000..7b32ec4 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +Program polega na sprawdzaniu temperatury, ciśnienia i wilgotności dla pięciu wybranych miast: +- Wrocław +- Poznań +- Warszawa +- Kraków +- Gdańsk +Ponadto, program pozwala zapisać wyniki pomiarów w trzech formatach: PDF, XML oraz JSON. +W projekcie skorzystałem z trzech zewnętrznych bibliotek (itextpdf, jdom2, json) \ No newline at end of file diff --git a/lib/itextpdf-5.5.4.jar b/lib/itextpdf-5.5.4.jar new file mode 100644 index 0000000..1508639 Binary files /dev/null and b/lib/itextpdf-5.5.4.jar differ diff --git a/lib/jdom2-2.0.5.jar b/lib/jdom2-2.0.5.jar new file mode 100644 index 0000000..b6996c7 Binary files /dev/null and b/lib/jdom2-2.0.5.jar differ diff --git a/lib/json-20231013.jar b/lib/json-20231013.jar new file mode 100644 index 0000000..465ad35 Binary files /dev/null and b/lib/json-20231013.jar differ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b20a8cd --- /dev/null +++ b/pom.xml @@ -0,0 +1,13 @@ + + 4.0.0 + org.example + Pogodynkaaa + 1.0-SNAPSHOT + + 17 + 17 + + Archetype - Pogodynkaaa + http://maven.apache.org + diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..ecaa6dd --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,243 @@ +import com.itextpdf.text.Paragraph; +import com.itextpdf.text.pdf.PdfWriter; +import org.json.JSONObject; + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; +import org.jdom2.output.Format; +import org.jdom2.output.XMLOutputter; + +public class Main { + + private static final String API_KEY = "23351061d324359a096983ef1fffc506"; + private static final String API_URL = "http://api.openweathermap.org/data/2.5/weather"; + + public static void main(String[] args) { + Map cities = initializeCities(); + + try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { + while (true) { + System.out.println("P-Podaj miasto, Z-Zakończ"); + System.out.print(">>"); + String input = reader.readLine(); + + if ("Z".equalsIgnoreCase(input)) { + break; + } + + if ("P".equalsIgnoreCase(input)) { + System.out.print(">>"); + String cityName = reader.readLine(); + + if (cities.containsKey(cityName)) { + Coordinates coordinates = cities.get(cityName); + WeatherData weatherData = getWeatherData(coordinates); + + if (weatherData != null) { + double temperature = weatherData.getTemperature() - 273.15; + System.out.println("Temp: " + temperature + + " Wilgotność: " + weatherData.getHumidity() + + " Ciśnienie: " + weatherData.getPressure()); + + System.out.println("P-PDF J-JSON X-XML W-Do wszystkich trzech"); + System.out.print(">>"); + String format = reader.readLine().toUpperCase(); + + switch (format) { + case "P": + saveToPDF(cityName, weatherData); + break; + case "J": + saveToJSON(cityName, weatherData); + break; + case "X": + saveToXML(cityName, weatherData); + break; + case "W": + saveToPDF(cityName, weatherData); + saveToJSON(cityName, weatherData); + saveToXML(cityName, weatherData); + break; + default: + System.out.println("Nieznany format."); + } + } else { + System.out.println("Błąd podczas pobierania danych."); + } + } else { + System.out.println("Nieznane miasto."); + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static Map initializeCities() { + Map cities = new HashMap<>(); + cities.put("Poznań", new Coordinates(52.4084, 16.9342)); + cities.put("Warszawa", new Coordinates(52.2298, 21.0118)); + cities.put("Kraków", new Coordinates(50.0647, 19.9450)); + cities.put("Wrocław", new Coordinates(51.1079, 17.0385)); + cities.put("Gdańsk", new Coordinates(54.3520, 18.6466)); + return cities; + } + + private static WeatherData getWeatherData(Coordinates coordinates) throws IOException { + String url = API_URL + "?lat=" + coordinates.getLatitude() + "&lon=" + coordinates.getLongitude() + "&appid=" + API_KEY; + HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + connection.setRequestMethod("GET"); + + int responseCode = connection.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { + StringBuilder response = new StringBuilder(); + String inputLine; + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + + return parseWeatherData(response.toString()); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } else { + System.out.println("Błąd podczas pobierania danych: " + responseCode); + return null; + } + } + + private static WeatherData parseWeatherData(String json) { + try { + JSONObject jsonObject = new JSONObject(json); + JSONObject mainObject = jsonObject.getJSONObject("main"); + + double temperature = mainObject.getDouble("temp"); + double humidity = mainObject.getDouble("humidity"); + double pressure = mainObject.getDouble("pressure"); + + return new WeatherData(temperature, humidity, pressure); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + private static void saveToPDF(String cityName, WeatherData weatherData) { + Path filePath = Paths.get("Pogoda_" + cityName + "_PDF.pdf"); + generatePDF(String.valueOf(filePath), cityName, weatherData.getTemperature() - 273.15, weatherData.getHumidity(), weatherData.getPressure()); + } + + public static void generatePDF(String filePath, String cityName, double temperature, double humidity, double pressure) { + try { + com.itextpdf.text.Document pdfDocument = new com.itextpdf.text.Document(); + PdfWriter.getInstance(pdfDocument, new FileOutputStream(filePath)); + pdfDocument.open(); + + pdfDocument.add(new Paragraph("Pogoda w " + cityName + ":")); + pdfDocument.add(new Paragraph("> Temperatura: " + temperature + "°C,")); + pdfDocument.add(new Paragraph("> Wilgotność: " + humidity + "%,")); + pdfDocument.add(new Paragraph("> Ciśnienie: " + pressure + " hPa")); + + pdfDocument.close(); + System.out.println("Zapisano plik PDF w: " + filePath); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static void saveToJSON(String cityName, WeatherData weatherData) { + try { + JSONObject jsonWeatherData = new JSONObject(); + jsonWeatherData.put("temperatura", weatherData.getTemperature() - 273.15); + jsonWeatherData.put("wilgotność", weatherData.getHumidity()); + jsonWeatherData.put("ciśnienie", weatherData.getPressure()); + + JSONObject finalObject = new JSONObject(); + finalObject.put(cityName, jsonWeatherData); + + Path filePath = Paths.get("Pogoda_" + cityName + "_JSON.json"); + Files.write(filePath, finalObject.toString(2).getBytes()); + + System.out.println("Zapisano plik JSON w: " + filePath); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void saveToXML(String cityName, WeatherData weatherData) { + try { + org.jdom2.Element root = new org.jdom2.Element("Pogoda_w_" + cityName); // Use fully qualified name for Element + + org.jdom2.Document document = new org.jdom2.Document(root); // Use fully qualified name for Document + + org.jdom2.Element temperatureElement = new org.jdom2.Element("Temperatura").setText(String.valueOf(weatherData.getTemperature() - 273.15)); + org.jdom2.Element humidityElement = new org.jdom2.Element("Wilgotność").setText(String.valueOf(weatherData.getHumidity())); + org.jdom2.Element pressureElement = new org.jdom2.Element("Ciśnienie").setText(String.valueOf(weatherData.getPressure())); + + root.addContent(temperatureElement); + root.addContent(humidityElement); + root.addContent(pressureElement); + + XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat()); + + String xmlString = xmlOutputter.outputString(document); + + Path filePath = Paths.get("Pogoda_" + cityName + "_XML.xml"); + Files.write(filePath, xmlString.getBytes()); + System.out.println("Zapisano plik XML w: " + filePath); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static class Coordinates { + private final double latitude; + private final double longitude; + + public Coordinates(double latitude, double longitude) { + this.latitude = latitude; + this.longitude = longitude; + } + + public double getLatitude() { + return latitude; + } + + public double getLongitude() { + return longitude; + } + } + + private static class WeatherData { + private final double temperature; + private final double humidity; + private final double pressure; + + public WeatherData(double temperature, double humidity, double pressure) { + this.temperature = temperature; + this.humidity = humidity; + this.pressure = pressure; + } + + public double getTemperature() { + return temperature; + } + + public double getHumidity() { + return humidity; + } + + public double getPressure() { + return pressure; + } + } +} \ No newline at end of file diff --git a/src/main/resources/META-INF/maven/archetype.xml b/src/main/resources/META-INF/maven/archetype.xml new file mode 100644 index 0000000..8c06000 --- /dev/null +++ b/src/main/resources/META-INF/maven/archetype.xml @@ -0,0 +1,9 @@ + + Pogodynkaaa + + src/main/java/App.java + + + src/test/java/AppTest.java + + diff --git a/src/main/resources/archetype-resources/pom.xml b/src/main/resources/archetype-resources/pom.xml new file mode 100644 index 0000000..44a2c9c --- /dev/null +++ b/src/main/resources/archetype-resources/pom.xml @@ -0,0 +1,15 @@ + + 4.0.0 + $org.example + $Pogodynkaaa + $1.0-SNAPSHOT + + + junit + junit + 3.8.1 + test + + + diff --git a/src/main/resources/archetype-resources/src/main/java/App.java b/src/main/resources/archetype-resources/src/main/java/App.java new file mode 100644 index 0000000..1fa6a95 --- /dev/null +++ b/src/main/resources/archetype-resources/src/main/java/App.java @@ -0,0 +1,13 @@ +package $org.example; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/src/main/resources/archetype-resources/src/test/java/AppTest.java b/src/main/resources/archetype-resources/src/test/java/AppTest.java new file mode 100644 index 0000000..65be417 --- /dev/null +++ b/src/main/resources/archetype-resources/src/test/java/AppTest.java @@ -0,0 +1,38 @@ +package $org.example; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} diff --git a/target/classes/META-INF/maven/archetype.xml b/target/classes/META-INF/maven/archetype.xml new file mode 100644 index 0000000..8c06000 --- /dev/null +++ b/target/classes/META-INF/maven/archetype.xml @@ -0,0 +1,9 @@ + + Pogodynkaaa + + src/main/java/App.java + + + src/test/java/AppTest.java + + diff --git a/target/classes/Main$Coordinates.class b/target/classes/Main$Coordinates.class new file mode 100644 index 0000000..0eddd9b Binary files /dev/null and b/target/classes/Main$Coordinates.class differ diff --git a/target/classes/Main$WeatherData.class b/target/classes/Main$WeatherData.class new file mode 100644 index 0000000..0a12b75 Binary files /dev/null and b/target/classes/Main$WeatherData.class differ diff --git a/target/classes/Main.class b/target/classes/Main.class new file mode 100644 index 0000000..b0c1140 Binary files /dev/null and b/target/classes/Main.class differ diff --git a/target/classes/archetype-resources/pom.xml b/target/classes/archetype-resources/pom.xml new file mode 100644 index 0000000..44a2c9c --- /dev/null +++ b/target/classes/archetype-resources/pom.xml @@ -0,0 +1,15 @@ + + 4.0.0 + $org.example + $Pogodynkaaa + $1.0-SNAPSHOT + + + junit + junit + 3.8.1 + test + + + diff --git a/target/classes/archetype-resources/src/main/java/App.java b/target/classes/archetype-resources/src/main/java/App.java new file mode 100644 index 0000000..1fa6a95 --- /dev/null +++ b/target/classes/archetype-resources/src/main/java/App.java @@ -0,0 +1,13 @@ +package $org.example; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/target/classes/archetype-resources/src/test/java/AppTest.java b/target/classes/archetype-resources/src/test/java/AppTest.java new file mode 100644 index 0000000..65be417 --- /dev/null +++ b/target/classes/archetype-resources/src/test/java/AppTest.java @@ -0,0 +1,38 @@ +package $org.example; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +}