feat: add StringParser and reworking project structure
This commit is contained in:
@@ -1,3 +0,0 @@
|
|||||||
public interface DataSource {
|
|
||||||
MyList<Student> fill(int size);
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
public class FileDataSource implements DataSource{
|
|
||||||
private String path;
|
|
||||||
|
|
||||||
public FileDataSource(String path) {
|
|
||||||
this.path = path;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPath(String path) {
|
|
||||||
this.path = path;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public MyList<Student> fill(int size) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
|
|
||||||
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
|
|
||||||
public class Main {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
|
|
||||||
// to see how IntelliJ IDEA suggests fixing it.
|
|
||||||
System.out.printf("Hello and welcome!");
|
|
||||||
|
|
||||||
for (int i = 1; i <= 5; i++) {
|
|
||||||
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
|
|
||||||
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
|
|
||||||
System.out.println("i = " + i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class ManualDataSource implements DataSource{
|
|
||||||
@Override
|
|
||||||
public MyList<Student> fill(int size) {
|
|
||||||
String strToParse;
|
|
||||||
Scanner scanner = new Scanner(System.in);
|
|
||||||
while(true){
|
|
||||||
strToParse = scanner.next();
|
|
||||||
if (strToParse.equals("0"))
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
scanner.close();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
public class MyList <T>{
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
public class Student {
|
|
||||||
private final String groupNumber;
|
|
||||||
private final double averageGrade;
|
|
||||||
private final String recordBookNumber;
|
|
||||||
|
|
||||||
public Student(String groupNumber, double averageGrade, String recordBookNumber) {
|
|
||||||
this.groupNumber = groupNumber;
|
|
||||||
this.averageGrade = averageGrade;
|
|
||||||
this.recordBookNumber = recordBookNumber;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
public class StudentBuilder {
|
|
||||||
private String groupNumber;
|
|
||||||
private double averageGrade;
|
|
||||||
private String recordBookNumber;
|
|
||||||
|
|
||||||
public void setGroupNumber(String groupNumber) {
|
|
||||||
this.groupNumber = groupNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAverageGrade(double averageGrade) {
|
|
||||||
this.averageGrade = averageGrade;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRecordBookNumber(String recordBookNumber) {
|
|
||||||
this.recordBookNumber = recordBookNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Student build(){
|
|
||||||
//TODO: Реализовать проверку на корректность введенных значений
|
|
||||||
|
|
||||||
return new Student(groupNumber,averageGrade, recordBookNumber);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package ru.project.datasource;
|
||||||
|
|
||||||
|
import ru.project.student.Student;
|
||||||
|
import ru.project.collection.MyList;
|
||||||
|
|
||||||
|
|
||||||
|
public interface DataSource {
|
||||||
|
MyList<Student> fill(int size);
|
||||||
|
}
|
||||||
@@ -7,34 +7,36 @@ import java.nio.file.Files;
|
|||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.stream.Collector;
|
import java.util.stream.Collector;
|
||||||
import java.util.NoSuchElementException;
|
|
||||||
import ru.project.student.Student;
|
import ru.project.student.Student;
|
||||||
import ru.project.collection.MyList;
|
import ru.project.collection.MyList;
|
||||||
import ru.project.collection.MyLinkedList;
|
|
||||||
|
|
||||||
public class FileDataSource implements DataSource{
|
public class FileDataSource implements DataSource{
|
||||||
private final String fileName;
|
private 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> fill(int size) {
|
||||||
MyList<Student> resultList = new MyLinkedList<>();
|
Path path = Paths.get(fileName);
|
||||||
try {
|
try(var lines = Files.lines(path)){
|
||||||
StudentCsvReader reader = new StudentCsvReader(fileName);
|
return lines.skip(1)
|
||||||
while reader.hasNext() {
|
.limit(size)
|
||||||
result.append(reader.next());
|
.map(e -> StringParser.parseString(e))
|
||||||
}
|
.filter(e -> !(e == null))
|
||||||
}
|
.collect(Collector.of(MyList::new,
|
||||||
catch (IllegalArgumentException e){
|
MyList::add,
|
||||||
System.out.println(e.getMessage());
|
(left, right) -> left.addAll(right)));
|
||||||
return null;
|
}catch(FileNotFoundException e){
|
||||||
}
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package ru.project.datasource;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
import ru.project.student.Student;
|
||||||
|
import ru.project.collection.MyList;
|
||||||
|
|
||||||
|
public class ManualDataSource implements DataSource{
|
||||||
|
@Override
|
||||||
|
public MyList<Student> fill(int size) {
|
||||||
|
MyList<Student> result = new MyList<>();
|
||||||
|
String strToParse;
|
||||||
|
Student student;
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
for (short i = 0; i < size; i++){
|
||||||
|
strToParse = scanner.next();
|
||||||
|
if (strToParse.equals("0"))
|
||||||
|
break;
|
||||||
|
student = StringParser.parseString(strToParse);
|
||||||
|
if (student == null){
|
||||||
|
System.out.println("Oops! Invalid input");
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
result.add(student);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
scanner.close();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -1,3 +1,8 @@
|
|||||||
|
package ru.project.datasource;
|
||||||
|
|
||||||
|
import ru.project.student.Student;
|
||||||
|
import ru.project.collection.MyList;
|
||||||
|
|
||||||
public class RandomDataSource implements DataSource{
|
public class RandomDataSource implements DataSource{
|
||||||
@Override
|
@Override
|
||||||
public MyList<Student> fill(int size) {
|
public MyList<Student> fill(int size) {
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package ru.project.datasource;
|
||||||
|
|
||||||
|
import ru.project.student.Student;
|
||||||
|
import ru.project.student.StudentBuilder;
|
||||||
|
import ru.project.collection.MyList;
|
||||||
|
|
||||||
|
public class StringParser {
|
||||||
|
public static Student parseString(String str){
|
||||||
|
String [] parts = str.split(";");
|
||||||
|
try{
|
||||||
|
return new StudentBuilder()
|
||||||
|
.setGroupNumber(parts[0])
|
||||||
|
.setAverageGrade(Double.valueOf(parts[1]))
|
||||||
|
.setRecordBookNumber(parts[2])
|
||||||
|
.build();
|
||||||
|
}catch(NumberFormatException e){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user