refactor: integrate StudentCsvParser into FileDataSource
This commit is contained in:
@@ -12,32 +12,28 @@ import ru.project.collection.MyList;
|
|||||||
import ru.project.collection.MyLinkedList;
|
import ru.project.collection.MyLinkedList;
|
||||||
|
|
||||||
public class FileDataSource implements DataSource{
|
public class FileDataSource implements DataSource{
|
||||||
private String fileName;
|
private final String fileName;
|
||||||
|
|
||||||
public FileDataSource(String fileName) {
|
public FileDataSource(String fileName) {
|
||||||
this.fileName = fileName;
|
this.fileName = fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFileName(String fileName) {
|
|
||||||
this.fileName = fileName;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MyList<Student> create() {
|
public MyList<Student> create() {
|
||||||
Path path = Paths.of(fileName);
|
MyList<Student> resultList = new MyLinkedList<>();
|
||||||
try(var lines = Files.lines(path)){
|
try {
|
||||||
return lines
|
StudentCsvReader reader = new StudentCsvReader(fileName);
|
||||||
//.skip(1)
|
while reader.hasNext() {
|
||||||
.map(e -> StringParser.parseString(e))
|
result.append(reader.next());
|
||||||
.filter(e -> !(e == null))
|
}
|
||||||
.collect(Collector.of(MyLinkedList::new,
|
}
|
||||||
MyLinkedList::add,
|
catch (IllegalArgumentException e){
|
||||||
(left, right) -> left;
|
System.out.println(e.getMessage());
|
||||||
}catch(FileNotFoundException e){
|
return null;
|
||||||
System.out.println("File not found.");
|
}
|
||||||
|
catch (NoSuchElementException e){
|
||||||
|
System.out.println("No such element");
|
||||||
return null;
|
return null;
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user