Скажите что думаете - на адекватность кода ? у меня подозрение что это Пи*дец а не код.
Подскажите что не нравится Валидатору?
Может есть идеи как упростить код Буду рад всем идеям и замечаниям !
package com.javarush.task.task19.task1916;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
Отслеживаем изменения
*/
public class Solution {
public static List<LineItem> lines = new ArrayList<LineItem>();
public static void main(String[] args)throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String firstPathFile = input.readLine();
String secondPathFile = input.readLine();
BufferedReader firstFile = new BufferedReader(new FileReader(firstPathFile));
BufferedReader secondFile = new BufferedReader(new FileReader(secondPathFile));
ArrayList<String> listFromFirstFile = new ArrayList<>();
ArrayList<String> listFromSecondFile = new ArrayList<>();
while (firstFile.ready()){
listFromFirstFile.add(firstFile.readLine());
}
while (secondFile.ready()){
listFromSecondFile.add(secondFile.readLine());
}
int maxLengthFromLists = Math.max(listFromFirstFile.size(),listFromSecondFile.size());
int countForSecondList = 0;
for (int i = 0; i < maxLengthFromLists; i++) {
// For small first txt File
if (listFromFirstFile.size() < listFromSecondFile.size()){
if (listFromFirstFile.size() > i && listFromFirstFile.get(i).equals(listFromSecondFile.get(countForSecondList)) ){
lines.add(new LineItem(Type.SAME,listFromFirstFile.get(countForSecondList)));
countForSecondList++;
} else if (listFromFirstFile.size() <= i || listFromSecondFile.get(countForSecondList+1).equals(listFromFirstFile.get(i+1))) {
lines.add(new LineItem(Type.ADDED,listFromSecondFile.get(countForSecondList)));
countForSecondList++;
}
// For Large first File
} else if (listFromFirstFile.size() > listFromSecondFile.size()) {
try {
listFromSecondFile.get(countForSecondList);
} catch (IndexOutOfBoundsException e){
lines.add(new LineItem(Type.REMOVED,listFromFirstFile.get(i)));
break;
}
if (!(listFromFirstFile.get(i).equals(listFromSecondFile.get(countForSecondList))) &&
listFromSecondFile.get(countForSecondList).equals(listFromFirstFile.get(i+1))) {
lines.add(new LineItem(Type.REMOVED,listFromFirstFile.get(i)));
countForSecondList--;
// System.out.println(listFromFirstFile.get(i) + " REMOVED i: "+ i +" count: "+ countForSecondList);
} else if (listFromFirstFile.get(i).equals(listFromSecondFile.get(countForSecondList))){
lines.add(new LineItem(Type.SAME,listFromFirstFile.get(i)));
// System.out.println(listFromFirstFile.get(i) + " SAME i: "+ i +" count: "+ countForSecondList);
} else if (!(listFromFirstFile.get(i).equals(listFromSecondFile.get(countForSecondList))) &&
!(listFromSecondFile.get(countForSecondList).equals(listFromFirstFile.get(i+1)))) {
lines.add(new LineItem(Type.ADDED,listFromSecondFile.get(countForSecondList)));
i--;
// System.out.println(listFromSecondFile.get(countForSecondList) + " ADDED i: "+ i +" count: "+ countForSecondList);
}
countForSecondList++;
}
}
input.close();
firstFile.close();
secondFile.close();
}
public static enum Type {
ADDED, //добавлена новая строка
REMOVED, //удалена строка
SAME //без изменений
}
public static class LineItem {
public Type type;
public String line;
public LineItem(Type type, String line) {
this.type = type;
this.line = line;
}
}
}