It seems correct to me.
"Be sure that the lines list has the ADDED label with the required lines in the correct places."
package com.codegym.task.task19.task1916;
import javax.sound.sampled.Line;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
Tracking changes
*/
public class Solution {
public static List<LineItem> lines = new ArrayList<>();
public static void main(String[] args) throws IOException {
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
String file1 = console.readLine(), file2 = console.readLine();
console.close();
FileReader file1Reader = new FileReader(file1), file2Reader = new FileReader(file2);
List<String> file1Lines = readLines(file1Reader), file2Lines = readLines(file2Reader);
mergeLines(file1Lines, file2Lines);
}
public static List<String> readLines(FileReader fileReader) throws IOException {
BufferedReader reader = new BufferedReader(fileReader);
List<String> fileLines = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
fileLines.add(line);
}
reader.close();
return fileLines;
}
public static void mergeLines(List<String> oldLines, List<String> newLines) {
int indexOld = 0, indexNew = 0;
while (indexOld < oldLines.size() && indexNew < newLines.size()) {
String oldLine = oldLines.get(indexOld);
String newLine = newLines.get(indexNew);
if (oldLine.equals(newLine)) {
lines.add(new LineItem(Type.SAME, oldLine));
indexOld ++;
indexNew ++;
} else if (indexOld + 1 < oldLines.size() && oldLines.get(indexOld + 1).equals(newLine)) {
lines.add(new LineItem(Type.REMOVED, oldLine));
indexOld ++;
} else if (indexNew + 1 < newLines.size() && newLines.get(indexNew + 1).equals(oldLine)) {
lines.add(new LineItem(Type.ADDED, newLine));
indexNew ++;
}
}
if (indexOld < oldLines.size()) { lines.add(new LineItem(Type.REMOVED, oldLines.get(indexOld))); }
if (indexNew < newLines.size()) { lines.add(new LineItem(Type.ADDED, oldLines.get(indexNew))); }
}
public static enum Type {
ADDED, // New line added
REMOVED, // Line deleted
SAME // No change
}
public static class LineItem {
public Type type;
public String line;
public LineItem(Type type, String line) {
this.type = type;
this.line = line;
}
}
}