Condition is not satisfying..
package com.codegym.task.task17.task1721;
import java.util.ArrayList;
import java.util.List;
import java.io.*;
import java.util.*;
/*
Transactionality
Requirements:
1. The Solution class must contain a public static List<String> field called allLines.
2. The Solution class must contain a public static List<String> field called linesForRemoval.
3. The Solution class must have a public void joinData() method that can throw
a CorruptedDataException.
4. The program should read the names of two files from the console.
5. The program must read line by line from the first file into the allLines list.
6. The program must read line by line from the second file into the linesForRemoval list.
7. The joinData method must remove from the allLines list all lines in the linesForRemoval
list if allLines contains all the lines in the linesForRemoval list.
8. The joinData method must clear the allLines list and throw a CorruptedDataException,
if allLines does not contain all the lines in the linesForRemoval list.
9. The joinData method should be called in main.
*/
public class Solution {
public static List<String> allLines = new ArrayList<>();
public static List<String> linesForRemoval = new ArrayList<>();
public static void main(String[] args) throws IOException {
String allLineFileName;
String linesForRemovalFileName;
BufferedReader rl = new BufferedReader(new InputStreamReader(System.in));
//read two file names
allLineFileName = rl.readLine();
linesForRemovalFileName = rl.readLine();
rl.close();
//read files into ArrayList
readInto(allLines, allLineFileName);
readInto(linesForRemoval, linesForRemovalFileName);
//run join data
Solution data = new Solution();
data.joinData();
}
public void joinData() throws CorruptedDataException {
/*
for (Iterator<String> stringIterator = allLines.iterator();stringIterator.hasNext();){
String line1 = stringIterator.next();
for ( String line2: linesForRemoval){
if (line1.equals(line2)){
stringIterator.remove();
}
}
}*/
if(allLines.containsAll(linesForRemoval)){
allLines.removeAll(linesForRemoval);
}
if (allLines.size() == 0) {
allLines.clear(); }
else {
allLines.clear();
throw new CorruptedDataException();
}
}
public static void readInto(List<String> target, String source){
try {
BufferedReader read = new BufferedReader(new FileReader(source));
String line;
while ((line = read.readLine()) != null){
target.add(line);
}
read.close();
}
catch (FileNotFoundException e) {
System.out.println("File not found!");
}
catch (IOException e) {
System.out.println("IO Error");
}
}
}