Merge pull request into develop from random-generator
This commit is contained in:
@@ -1,30 +1,37 @@
|
|||||||
package ru.project.random;
|
package ru.project.random;
|
||||||
|
|
||||||
import java.util.Random;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
import ru.project.collection.MyLinkedList;
|
import ru.project.collection.MyLinkedList;
|
||||||
import ru.project.collection.MyList;
|
import ru.project.collection.MyList;
|
||||||
import ru.project.student.Student;
|
import ru.project.student.Student;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
import ru.project.student.StudentBuilder;
|
import ru.project.student.StudentBuilder;
|
||||||
|
public class RandomStudentGenerator
|
||||||
public class RandomStudentGenerator {
|
{
|
||||||
private static final Random RANDOM = new Random();
|
private static final Random RANDOM = new Random();
|
||||||
private String minGroupNumber;
|
private String minGroupNumber;
|
||||||
private String maxGroupNumber;
|
private String maxGroupNumber;
|
||||||
private Double minAverageGrade;
|
private Double minAverageGrade;
|
||||||
private Double maxAverageGrade;
|
private Double maxAverageGrade;
|
||||||
private Integer minRecordBookNumber;
|
private Integer minRecordBookNumber;
|
||||||
private Integer maxRecordBookNumber;
|
private Integer maxRecordBookNumber;
|
||||||
private static final double DEFAULT_MIN_GRADE = 0.0;
|
private static final double DEFAULT_MIN_GRADE = 0.0;
|
||||||
private static final double DEFAULT_MAX_GRADE = 5.0;
|
private static final double DEFAULT_MAX_GRADE = 5.0;
|
||||||
private static final int DEFAULT_MIN_RECORD_BOOK = 1;
|
private static final int DEFAULT_MIN_RECORD_BOOK = 1;
|
||||||
private static final String GROUP_PATTERN_REGEX = "^[A-Z][0-9]{2}$";
|
private static final String GROUP_PATTERN_REGEX = "^[A-Z][0-9]{2}$";
|
||||||
private static final Pattern GROUP_PATTERN = Pattern.compile(GROUP_PATTERN_REGEX);
|
private static final Pattern GROUP_PATTERN = Pattern.compile(GROUP_PATTERN_REGEX);
|
||||||
|
public RandomStudentGenerator setMinGroupNumber(String minGroupNumber) {
|
||||||
public RandomStudentGenerator setMinGroupNumber(String minGroupNumber) {
|
if (minGroupNumber != null && !GROUP_PATTERN.matcher(minGroupNumber).matches()) {
|
||||||
if (minGroupNumber != null && !GROUP_PATTERN.matcher(minGroupNumber).matches()) {
|
throw new IllegalArgumentException("minGroupNumber does not match Student group pattern: " + GROUP_PATTERN_REGEX);
|
||||||
throw new IllegalArgumentException(
|
}
|
||||||
"minGroupNumber does not match Student group pattern: " + GROUP_PATTERN_REGEX);
|
this.minGroupNumber = minGroupNumber;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
public RandomStudentGenerator setMaxGroupNumber(String maxGroupNumber) {
|
||||||
|
if (maxGroupNumber != null && !GROUP_PATTERN.matcher(maxGroupNumber).matches()) {
|
||||||
|
throw new IllegalArgumentException("maxGroupNumber does not match Student group pattern: " + GROUP_PATTERN_REGEX);
|
||||||
|
}
|
||||||
|
this.maxGroupNumber = maxGroupNumber;
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
this.minGroupNumber = minGroupNumber;
|
this.minGroupNumber = minGroupNumber;
|
||||||
return this;
|
return this;
|
||||||
@@ -106,23 +113,12 @@ public class RandomStudentGenerator {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validateRanges() {
|
private double generateAverageGrade() {
|
||||||
|
double min = (minAverageGrade != null) ? minAverageGrade : DEFAULT_MIN_GRADE;
|
||||||
double effectiveMinGrade = (minAverageGrade != null) ? minAverageGrade : DEFAULT_MIN_GRADE;
|
double max = (maxAverageGrade != null) ? maxAverageGrade : DEFAULT_MAX_GRADE;
|
||||||
double effectiveMaxGrade = (maxAverageGrade != null) ? maxAverageGrade : DEFAULT_MAX_GRADE;
|
double range = max - min;
|
||||||
if (effectiveMinGrade > effectiveMaxGrade) {
|
double grade = min + (range * RANDOM.nextDouble());
|
||||||
throw new IllegalArgumentException("minAverageGrade cannot be greater than maxAverageGrade");
|
return Math.round(grade * 100.0) / 100.0;
|
||||||
}
|
|
||||||
int effectiveMinRecord =
|
|
||||||
(minRecordBookNumber != null) ? minRecordBookNumber : DEFAULT_MIN_RECORD_BOOK;
|
|
||||||
int effectiveMaxRecord =
|
|
||||||
(maxRecordBookNumber != null) ? maxRecordBookNumber : Integer.MAX_VALUE;
|
|
||||||
if (effectiveMaxRecord == Integer.MAX_VALUE) {
|
|
||||||
effectiveMaxRecord = effectiveMinRecord + 999_999;
|
|
||||||
}
|
|
||||||
if (effectiveMinRecord > effectiveMaxRecord) {
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"minRecordBookNumber cannot be greater than maxRecordBookNumber");
|
|
||||||
}
|
}
|
||||||
if (minGroupNumber != null && maxGroupNumber != null) {
|
if (minGroupNumber != null && maxGroupNumber != null) {
|
||||||
if (minGroupNumber.compareTo(maxGroupNumber) > 0) {
|
if (minGroupNumber.compareTo(maxGroupNumber) > 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user