saveToJson, saveToXml prepared and PdfConverting moved to weatherForecast class so all of the methods stay under one class

This commit is contained in:
Marcin Hutek 2023-12-09 13:26:32 +01:00
parent f49ad0ae3b
commit 5a7cf62d18
8 changed files with 91 additions and 64 deletions

View File

@ -27,7 +27,7 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
<version>2.13.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
@ -57,5 +57,10 @@
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.13.1</version>
</dependency>
</dependencies>
</project>

1
results.json Normal file
View File

@ -0,0 +1 @@
[{"cityName":"berlin","description":"overcast clouds","temperature":277.98,"pressure":999,"humidity":91},{"cityName":"tokyo","description":"clear sky","temperature":287.08,"pressure":1018,"humidity":60},{"cityName":"warsaw","description":"light snow","temperature":272.62,"pressure":1013,"humidity":89}]

Binary file not shown.

5
results.xml Normal file
View File

@ -0,0 +1,5 @@
<ArrayList>
<item>
<cityName>vancouver</cityName><description>overcast clouds</description><temperature>275.68</temperature><pressure>1026</pressure><humidity>92</humidity></item>
<item><cityName>chicago</cityName><description>mist</description><temperature>285.39</temperature><pressure>1001</pressure><humidity>92</humidity></item>
</ArrayList>

View File

@ -1,4 +1,5 @@
package model;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
@ -7,11 +8,13 @@ import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
public class ConvertResultPdf {
private String inputFile;
private String outputFile;
public ConvertResultPdf(String inputFile, String outputFile) {
this.inputFile = inputFile;
this.outputFile = outputFile;
@ -35,34 +38,4 @@ public class ConvertResultPdf {
e.printStackTrace();
}
}
// public void convertToJson() {
// try {
// String txtData = new String(Files.readAllBytes(Paths.get(inputFile)));
// String[] lines = txtData.split("\n");
//
// String cityName = lines[0].substring("city: ".length()).trim();
// String description = lines[1].substring("description: ".length()).trim();
//// double temperature = Double.parseDouble(lines[2].substring("temperature: ".length()).trim()
//// .replace(",", "."));
// double temperature = Double.parseDouble(lines[2].replaceAll("[^\\d.,]", "").replace(",", "."));
// int pressure = Integer.parseInt(lines[3].substring("pressure: ".length()).trim().split(" ")[0]);
// int humidity = Integer.parseInt(lines[4].substring("humidity: ".length()).trim().split(" ")[0]);
//
// ObjectMapper objectMapper = new ObjectMapper();
// ArrayNode jsonArray = objectMapper.createArrayNode();
// ObjectNode cityNode = JsonNodeFactory.instance.objectNode();
//
// cityNode.put("name", cityName);
// cityNode.put("description", description);
// cityNode.put("temperature", temperature);
// cityNode.put("pressure", pressure);
// cityNode.put("humidity", humidity);
//
// jsonArray.add(cityNode);
//
// objectMapper.writerWithDefaultPrettyPrinter().writeValue(new File(outputFile), jsonArray);
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
}
}

View File

@ -1,9 +1,18 @@
package model;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
public class weatherForecast {
public static final List<weatherForecast> allEntries = new ArrayList<>();
@ -36,18 +45,33 @@ public class weatherForecast {
return weatherForecastString;
}
public static void saveToJson(String outputFile) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(new File(outputFile), allEntries);
}
public static void saveToXml(String outputFile) throws IOException {
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.writeValue(new File(outputFile), allEntries);
}
// public static void saveToJson() throws IOException {
// File jsonFile = new File(generateFileName(".json"));
// ObjectMapper mapper = new ObjectMapper();
// mapper.writeValue(jsonFile, allEntries);
// }
public static void convertToPdf(String inputFile, String outputFile) {
try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(outputFile));
document.open();
// public static void saveToXml() throws IOException {
// File xmlFile = new File(generateFileName(".xml"));
// XmlMapper mapper = new XmlMapper();
// mapper.writeValue(xmlFile, allEntries);
// }
String line;
while ((line = br.readLine()) != null) {
document.add(new Paragraph(line));
}
document.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static List<weatherForecast> getAllEntries() {
return new ArrayList<>(allEntries);

View File

@ -74,33 +74,34 @@ public class Main {
System.out.println(forecastString);
// String forecastString = weatherString.prepareForecast(inputCity, response);
// System.out.println(forecastString);
// temporaryFile.addToFile(forecastString);
temporaryFile.addToFile(forecastString);
} else {
System.out.println("Podanego miasta nie ma na liscie.");
}
// } else if (Objects.equals(input, "z")) {
// System.out.println("W jakim formacie chcesz zapisac wyniki?\n pdf/json/xml");
// String whichFormat = scanner.nextLine().toLowerCase();
// if (Objects.equals(whichFormat, "pdf")) {
// String outputFile = "results.pdf";
} else if (Objects.equals(input, "z")) {
System.out.println("W jakim formacie chcesz zapisac wyniki?\n pdf/json/xml");
String whichFormat = scanner.nextLine().toLowerCase();
if (Objects.equals(whichFormat, "pdf")) {
String outputFile = "results.pdf";
// ConvertResultPdf convertResult = new ConvertResultPdf(temporaryFile.FILE_NAME,
// "results.pdf");
// convertResult.convertToPdf();
// } else if (Objects.equals(whichFormat, "json")) {
// String outputFile = "results.json";
// ConvertResultPdf convertResult = new ConvertResultPdf(temporaryFile.FILE_NAME,
// "results.json");
//// convertResult.convertToJson();
// } else if (Objects.equals(whichFormat, "xml")) {
// String outputFile = "results.xml";
// ConvertResultPdf convertResult = new ConvertResultPdf(temporaryFile.FILE_NAME,
// "results.xml");
//// convertResult.convertToXml();
// } else {
// System.out.println("Wprowadz poprawna opcje.");
// }
// System.out.println("Program zakonczyl dzialanie.");
// break;
weatherForecast.convertToPdf(temporaryFile.FILE_NAME, outputFile);
} else if (Objects.equals(whichFormat, "json")) {
String outputFile = "results.json";
weatherForecast.saveToJson(outputFile);
} else if (Objects.equals(whichFormat, "xml")) {
String outputFile = "results.xml";
weatherForecast.saveToXml(outputFile);
} else {
System.out.println("Wprowadz poprawna opcje.");
continue;
}
System.out.println("Program zakonczyl dzialanie.");
break;
} else if (Objects.equals(input,"x")) {
System.out.println("Program zakonczyl dzialanie.");
break;

View File

@ -0,0 +1,18 @@
city: chicago
description: light rain
temperature[K]: 285,44
pressure[hPa]: 1001
humidity[%]: 92
--------------------
city: chicago
description: light rain
temperature[K]: 285,44
pressure[hPa]: 1001
humidity[%]: 92
--------------------
city: chicago
description: light rain
temperature[K]: 285,44
pressure[hPa]: 1001
humidity[%]: 92
--------------------