first commit

This commit is contained in:
Wiktor Gołąb 2023-12-16 08:47:55 +01:00
commit 3a79713363
29 changed files with 517 additions and 0 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

16
.idea/compiler.xml Normal file
View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<excludeFromCompile>
<directory url="file://$PROJECT_DIR$/src/main/resources/archetype-resources" includeSubdirectories="true" />
</excludeFromCompile>
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="Pogodynkaaa" />
</profile>
</annotationProcessing>
</component>
</project>

7
.idea/encodings.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

20
.idea/jarRepositories.xml Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

View File

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="itextpdf-5.5.4">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/itextpdf-5.5.4.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="jdom2-2.0.5">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/jdom2-2.0.5.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="json-20231013">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/json-20231013.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

13
.idea/misc.xml Normal file
View File

@ -0,0 +1,13 @@
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

5
Pogoda_Poznań_JSON.json Normal file
View File

@ -0,0 +1,5 @@
{"Poznań": {
"ciśnienie": 1013,
"wilgotność": 95,
"temperatura": 2.7800000000000296
}}

BIN
Pogoda_Poznań_PDF.pdf Normal file

Binary file not shown.

6
Pogoda_Poznań_XML.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Pogoda_w_Poznań>
<Temperatura>2.7800000000000296</Temperatura>
<Wilgotność>95.0</Wilgotność>
<Ciśnienie>1013.0</Ciśnienie>
</Pogoda_w_Poznań>

8
README.md Normal file
View File

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

BIN
lib/itextpdf-5.5.4.jar Normal file

Binary file not shown.

BIN
lib/jdom2-2.0.5.jar Normal file

Binary file not shown.

BIN
lib/json-20231013.jar Normal file

Binary file not shown.

13
pom.xml Normal file
View File

@ -0,0 +1,13 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Pogodynkaaa</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<name>Archetype - Pogodynkaaa</name>
<url>http://maven.apache.org</url>
</project>

243
src/main/java/Main.java Normal file
View File

@ -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<String, Coordinates> 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<String, Coordinates> initializeCities() {
Map<String, Coordinates> 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;
}
}
}

View File

@ -0,0 +1,9 @@
<archetype>
<id>Pogodynkaaa</id>
<sources>
<source>src/main/java/App.java</source>
</sources>
<testSources>
<source>src/test/java/AppTest.java</source>
</testSources>
</archetype>

View File

@ -0,0 +1,15 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>$org.example</groupId>
<artifactId>$Pogodynkaaa</artifactId>
<version>$1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,13 @@
package $org.example;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}

View File

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

View File

@ -0,0 +1,9 @@
<archetype>
<id>Pogodynkaaa</id>
<sources>
<source>src/main/java/App.java</source>
</sources>
<testSources>
<source>src/test/java/AppTest.java</source>
</testSources>
</archetype>

Binary file not shown.

Binary file not shown.

BIN
target/classes/Main.class Normal file

Binary file not shown.

View File

@ -0,0 +1,15 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>$org.example</groupId>
<artifactId>$Pogodynkaaa</artifactId>
<version>$1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,13 @@
package $org.example;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}

View File

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