As far as I can tell, my code functions exactly as it should, but I'm still not getting the validation. Help me Guadalupe, you're my only hope!
package com.codegym.task.task19.task1916;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/*
Tracking changes
*/
public class Solution {
public static List<LineItem> lines = new ArrayList<>();
public static List<String> addedLines = new ArrayList<>();
public static void main(String[] args) throws IOException {
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> original = getFileLines(console.readLine());
ArrayList<String> updated = getFileLines(console.readLine());
console.close();
populateList(original, updated);
if (args.length > 0 && args[0].equals("debug")){
for (LineItem lineItem : lines){
System.out.println(lineItem.line);
}
}
}
//Reads lines from a file and returns them in list form
private static ArrayList<String> getFileLines(String fileName) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
ArrayList<String> list = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null){
list.add(line);
}
reader.close();
return list;
}
//Goes through each list sequentially and adds them to the final list
private static void populateList(List<String> original, List<String> updated){
int sameCount = 0;
for (String line : original){
if (updated.contains(line)) sameCount++;
}
boolean sameLine = ((original.size() - sameCount) < sameCount);
//sameLine determines if there are more same lines or different lines, and sets the starting point accordingly
//As each line is added, the boolean is toggled, preventing repeating types
while (!allLinesProcessed(original, updated)) {
//loops indefinitely until all lines from the input files have been processed
if (sameLine) {
for (int i = 0; i < original.size() ; i++){
if (updated.contains(original.get(i)) && (!addedLines.contains(original.get(i)))){
addedLines.add(original.get(i));
lines.add(new LineItem(Type.SAME, "SAME "+original.get(i)));
break;
}
}
sameLine = false;
}
if (!sameLine) {
for (int i = 0; i < original.size() ; i++){
if (!original.contains(updated.get(i)) && (!addedLines.contains(updated.get(i)))) {
addedLines.add(updated.get(i));
lines.add(new LineItem(Type.ADDED, "ADDED " + updated.get(i)));
break;
}
if (!updated.contains(original.get(i)) && (!addedLines.contains(original.get(i)))){
addedLines.add(original.get(i));
lines.add(new LineItem(Type.REMOVED, "REMOVED "+original.get(i)));
break;
}
}
sameLine = true;
}
}
}
//Checks the addedLines list against the original input lists to see if all lines have been processed
private static boolean allLinesProcessed(List<String> original, List<String> updated){
for (String line : original){
if (!addedLines.contains(line)) return false;
}
for (String line : updated){
if (!addedLines.contains(line)) return false;
}
return true;
}
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;
}
}
}