feat: add StringParser and reworking project structure

This commit is contained in:
Ершов Владислав
2026-08-24 22:54:19 +07:00
parent 808e1eadbb
commit 8fa66b10d2
12 changed files with 82 additions and 103 deletions
@@ -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;
}
}
-15
View File
@@ -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;
}
}
-2
View File
@@ -1,2 +0,0 @@
public class MyList <T>{
}
-11
View File
@@ -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.Paths;
import java.util.stream.Collector;
import java.util.NoSuchElementException;
import ru.project.student.Student;
import ru.project.collection.MyList;
import ru.project.collection.MyLinkedList;
public class FileDataSource implements DataSource{
private final String fileName;
private String fileName;
public FileDataSource(String fileName) {
this.fileName = fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
@Override
public MyList<Student> create() {
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");
public MyList<Student> fill(int size) {
Path path = Paths.get(fileName);
try(var lines = Files.lines(path)){
return lines.skip(1)
.limit(size)
.map(e -> StringParser.parseString(e))
.filter(e -> !(e == null))
.collect(Collector.of(MyList::new,
MyList::add,
(left, right) -> left.addAll(right)));
}catch(FileNotFoundException e){
System.out.println("File not found.");
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;
}
}
@@ -1,3 +1,8 @@
package ru.project.datasource;
import ru.project.student.Student;
import ru.project.collection.MyList;
public class RandomDataSource implements DataSource{
@Override
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;
}
}
}