refactor: integrate StudentCsvParser into FileDataSource

This commit is contained in:
Ершов Владислав
2026-08-30 22:38:01 +07:00
parent a65ed01c5c
commit a5ed3acfd8
@@ -12,32 +12,28 @@ import ru.project.collection.MyList;
import ru.project.collection.MyLinkedList;
public class FileDataSource implements DataSource{
private String fileName;
private final String fileName;
public FileDataSource(String fileName) {
this.fileName = fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
@Override
public MyList<Student> create() {
Path path = Paths.of(fileName);
try(var lines = Files.lines(path)){
return lines
//.skip(1)
.map(e -> StringParser.parseString(e))
.filter(e -> !(e == null))
.collect(Collector.of(MyLinkedList::new,
MyLinkedList::add,
(left, right) -> left;
}catch(FileNotFoundException e){
System.out.println("File not found.");
MyList<Student> resultList = new MyLinkedList<>();
try {
StudentCsvReader reader = new StudentCsvReader(fileName);
while reader.hasNext() {
result.append(reader.next());
}
}
catch (IllegalArgumentException e){
System.out.println(e.getMessage());
return null;
}
catch (NoSuchElementException e){
System.out.println("No such element");
return null;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}