Skip to content

Commit 091fd32

Browse files
committed
added differences between file1.json and file2.json
1 parent 219b26a commit 091fd32

File tree

2 files changed

+61
-18
lines changed

2 files changed

+61
-18
lines changed

app/src/main/java/hexlet/code/App.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
class App implements Runnable {
1717

18-
@Option(names = { "-h", "--help" }, usageHelp = true, description = "Show this help message and exit.")
19-
boolean usageHelpRequested;
20-
21-
@Option(names = { "-V", "--version" }, versionHelp = true, description = "Print version information and exit.")
22-
boolean versionInfoRequested;
18+
// @Option(names = { "-h", "--help" }, usageHelp = true, description = "Show this help message and exit.")
19+
// boolean usageHelpRequested;
20+
//
21+
// @Option(names = { "-V", "--version" }, versionHelp = true, description = "Print version information and exit.")
22+
// boolean versionInfoRequested;
2323

2424
@Option(names = { "-f", "--format " }, paramLabel = "format",
2525
description = "output format [default: stylish]", defaultValue = "stylish")
@@ -35,11 +35,8 @@ class App implements Runnable {
3535
@Override
3636
public void run() {
3737
try {
38-
Map<String, Object> data1 = JsonParser.readJsonFile(filepath1);
39-
Map<String, Object> data2 = JsonParser.readJsonFile(filepath2);
40-
41-
System.out.println("Файл 1: " + data1);
42-
System.out.println("Файл 2: " + data2);
38+
String diff = Differ.generate(filepath1, filepath2);
39+
System.out.println(diff);
4340
} catch (IOException e) {
4441
System.err.println("Ошибка при чтении файла: " + e.getMessage());
4542
}
@@ -48,12 +45,4 @@ public void run() {
4845
public static void main(String[] args) {
4946
new CommandLine(new App()).execute(args);
5047
}
51-
52-
public class JsonParser {
53-
public static Map<String, Object> readJsonFile(String filePath) throws IOException {
54-
ObjectMapper objectMapper = new ObjectMapper();
55-
return objectMapper.readValue(new File("src/main/resources/" + filePath), Map.class);
56-
}
57-
}
5848
}
59-
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package hexlet.code;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.util.Map;
8+
import java.util.Set;
9+
import java.util.TreeSet;
10+
11+
public class Differ {
12+
13+
public static String generate(String filePath1, String filePath2) throws IOException {
14+
ObjectMapper objectMapper = new ObjectMapper();
15+
16+
// Читаю JSON и складываю в Map
17+
Map<String, Object> fileInfo1 = objectMapper.readValue(new File("src/main/resources/" + filePath1), Map.class);
18+
Map<String, Object> fileInfo2 = objectMapper.readValue(new File("src/main/resources/" + filePath2), Map.class);
19+
20+
return generateDiff(fileInfo1, fileInfo2);
21+
}
22+
23+
public static String generateDiff(Map<String, Object> fileInfo1, Map<String, Object> fileInfo2) {
24+
25+
//разбиваю на значения
26+
Set<String> allKeys = new TreeSet<>();
27+
allKeys.addAll(fileInfo1.keySet());
28+
allKeys.addAll(fileInfo2.keySet());
29+
30+
// тут буду хранить результат сравнения
31+
StringBuilder differences = new StringBuilder("\n" +
32+
"{\n");
33+
34+
//сравниваю по значениям и вывожу обратно ключ-значение
35+
for (String key : allKeys) {
36+
37+
Object value1 = fileInfo1.get(key);
38+
Object value2 = fileInfo2.get(key);
39+
40+
if (value1 != null && value2 != null && value1.equals(value2)) {
41+
differences.append(" ").append(key).append(": ").append(value1).append("\n");
42+
} else {
43+
if (value1 != null) {
44+
differences.append(" - ").append(key).append(": ").append(value1).append("\n");
45+
} if (value2 != null) {
46+
differences.append(" + ").append(key).append(": ").append(value2).append("\n");
47+
}
48+
}
49+
}
50+
51+
differences.append("}\n");
52+
return differences.toString();
53+
}
54+
}

0 commit comments

Comments
 (0)