saveToJson, saveToXml prepared and PdfConverting moved to weatherForecast class so all of the methods stay under one class
This commit is contained in:
parent
f49ad0ae3b
commit
5a7cf62d18
7
pom.xml
7
pom.xml
@ -27,7 +27,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
<artifactId>jackson-databind</artifactId>
|
<artifactId>jackson-databind</artifactId>
|
||||||
<version>2.9.8</version>
|
<version>2.13.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.itextpdf</groupId>
|
<groupId>com.itextpdf</groupId>
|
||||||
@ -57,5 +57,10 @@
|
|||||||
<artifactId>commons-io</artifactId>
|
<artifactId>commons-io</artifactId>
|
||||||
<version>2.11.0</version>
|
<version>2.11.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||||
|
<artifactId>jackson-dataformat-xml</artifactId>
|
||||||
|
<version>2.13.1</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
1
results.json
Normal file
1
results.json
Normal 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}]
|
BIN
results.pdf
BIN
results.pdf
Binary file not shown.
5
results.xml
Normal file
5
results.xml
Normal 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>
|
@ -1,4 +1,5 @@
|
|||||||
package model;
|
package model;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.itextpdf.text.Document;
|
import com.itextpdf.text.Document;
|
||||||
import com.itextpdf.text.Paragraph;
|
import com.itextpdf.text.Paragraph;
|
||||||
import com.itextpdf.text.pdf.PdfWriter;
|
import com.itextpdf.text.pdf.PdfWriter;
|
||||||
@ -7,11 +8,13 @@ import java.io.BufferedReader;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class ConvertResultPdf {
|
public class ConvertResultPdf {
|
||||||
private String inputFile;
|
private String inputFile;
|
||||||
private String outputFile;
|
private String outputFile;
|
||||||
|
|
||||||
|
|
||||||
public ConvertResultPdf(String inputFile, String outputFile) {
|
public ConvertResultPdf(String inputFile, String outputFile) {
|
||||||
this.inputFile = inputFile;
|
this.inputFile = inputFile;
|
||||||
this.outputFile = outputFile;
|
this.outputFile = outputFile;
|
||||||
@ -35,34 +38,4 @@ public class ConvertResultPdf {
|
|||||||
e.printStackTrace();
|
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();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
|
@ -1,9 +1,18 @@
|
|||||||
package model;
|
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.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.FileReader;
|
||||||
|
|
||||||
public class weatherForecast {
|
public class weatherForecast {
|
||||||
public static final List<weatherForecast> allEntries = new ArrayList<>();
|
public static final List<weatherForecast> allEntries = new ArrayList<>();
|
||||||
@ -36,18 +45,33 @@ public class weatherForecast {
|
|||||||
|
|
||||||
return weatherForecastString;
|
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 {
|
public static void convertToPdf(String inputFile, String outputFile) {
|
||||||
// File jsonFile = new File(generateFileName(".json"));
|
try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) {
|
||||||
// ObjectMapper mapper = new ObjectMapper();
|
Document document = new Document();
|
||||||
// mapper.writeValue(jsonFile, allEntries);
|
PdfWriter.getInstance(document, new FileOutputStream(outputFile));
|
||||||
// }
|
document.open();
|
||||||
|
|
||||||
// public static void saveToXml() throws IOException {
|
String line;
|
||||||
// File xmlFile = new File(generateFileName(".xml"));
|
while ((line = br.readLine()) != null) {
|
||||||
// XmlMapper mapper = new XmlMapper();
|
document.add(new Paragraph(line));
|
||||||
// mapper.writeValue(xmlFile, allEntries);
|
}
|
||||||
// }
|
|
||||||
|
document.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static List<weatherForecast> getAllEntries() {
|
public static List<weatherForecast> getAllEntries() {
|
||||||
return new ArrayList<>(allEntries);
|
return new ArrayList<>(allEntries);
|
||||||
|
@ -74,33 +74,34 @@ public class Main {
|
|||||||
System.out.println(forecastString);
|
System.out.println(forecastString);
|
||||||
// String forecastString = weatherString.prepareForecast(inputCity, response);
|
// String forecastString = weatherString.prepareForecast(inputCity, response);
|
||||||
// System.out.println(forecastString);
|
// System.out.println(forecastString);
|
||||||
// temporaryFile.addToFile(forecastString);
|
temporaryFile.addToFile(forecastString);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Podanego miasta nie ma na liscie.");
|
System.out.println("Podanego miasta nie ma na liscie.");
|
||||||
}
|
}
|
||||||
// } else if (Objects.equals(input, "z")) {
|
} else if (Objects.equals(input, "z")) {
|
||||||
// System.out.println("W jakim formacie chcesz zapisac wyniki?\n pdf/json/xml");
|
System.out.println("W jakim formacie chcesz zapisac wyniki?\n pdf/json/xml");
|
||||||
// String whichFormat = scanner.nextLine().toLowerCase();
|
String whichFormat = scanner.nextLine().toLowerCase();
|
||||||
// if (Objects.equals(whichFormat, "pdf")) {
|
if (Objects.equals(whichFormat, "pdf")) {
|
||||||
// String outputFile = "results.pdf";
|
String outputFile = "results.pdf";
|
||||||
// ConvertResultPdf convertResult = new ConvertResultPdf(temporaryFile.FILE_NAME,
|
// ConvertResultPdf convertResult = new ConvertResultPdf(temporaryFile.FILE_NAME,
|
||||||
// "results.pdf");
|
// "results.pdf");
|
||||||
// convertResult.convertToPdf();
|
// convertResult.convertToPdf();
|
||||||
// } else if (Objects.equals(whichFormat, "json")) {
|
|
||||||
// String outputFile = "results.json";
|
weatherForecast.convertToPdf(temporaryFile.FILE_NAME, outputFile);
|
||||||
// ConvertResultPdf convertResult = new ConvertResultPdf(temporaryFile.FILE_NAME,
|
|
||||||
// "results.json");
|
} else if (Objects.equals(whichFormat, "json")) {
|
||||||
//// convertResult.convertToJson();
|
String outputFile = "results.json";
|
||||||
// } else if (Objects.equals(whichFormat, "xml")) {
|
weatherForecast.saveToJson(outputFile);
|
||||||
// String outputFile = "results.xml";
|
} else if (Objects.equals(whichFormat, "xml")) {
|
||||||
// ConvertResultPdf convertResult = new ConvertResultPdf(temporaryFile.FILE_NAME,
|
String outputFile = "results.xml";
|
||||||
// "results.xml");
|
weatherForecast.saveToXml(outputFile);
|
||||||
//// convertResult.convertToXml();
|
} else {
|
||||||
// } else {
|
System.out.println("Wprowadz poprawna opcje.");
|
||||||
// System.out.println("Wprowadz poprawna opcje.");
|
continue;
|
||||||
// }
|
}
|
||||||
// System.out.println("Program zakonczyl dzialanie.");
|
System.out.println("Program zakonczyl dzialanie.");
|
||||||
// break;
|
break;
|
||||||
} else if (Objects.equals(input,"x")) {
|
} else if (Objects.equals(input,"x")) {
|
||||||
System.out.println("Program zakonczyl dzialanie.");
|
System.out.println("Program zakonczyl dzialanie.");
|
||||||
break;
|
break;
|
||||||
|
@ -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
|
||||||
|
--------------------
|
Loading…
Reference in New Issue
Block a user