Merge branch 'develop' into feature/datasource
This commit is contained in:
@@ -1,10 +0,0 @@
|
||||
package ru.project;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*/
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package ru.project.collection;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class MyLinkedList<T> implements MyList<T> {
|
||||
private Node<T> head;
|
||||
private int size;
|
||||
|
||||
public void add(T value) {
|
||||
if (head == null) {
|
||||
this.head = new Node<>(value);
|
||||
} else {
|
||||
Node<T> temp = head;
|
||||
while (temp.getNext() != null) {
|
||||
temp = temp.getNext();
|
||||
}
|
||||
temp.setNext(new Node<>(value));
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public T get(int index) {
|
||||
int currentIndex = 0;
|
||||
Node<T> temp = head;
|
||||
while (temp != null) {
|
||||
if (currentIndex == index) {
|
||||
return temp.getValue();
|
||||
|
||||
} else {
|
||||
temp = temp.getNext();
|
||||
currentIndex++;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
public void remove(int index) {
|
||||
if (index < 0 || index >= size) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
if (index == 0) {
|
||||
head = head.getNext();
|
||||
size--;
|
||||
return;
|
||||
}
|
||||
int currentIndex = 0;
|
||||
Node<T> temp = head;
|
||||
while (temp != null) {
|
||||
if (currentIndex == index - 1) {
|
||||
temp.setNext(temp.getNext().getNext());
|
||||
size--;
|
||||
return;
|
||||
} else {
|
||||
temp = temp.getNext();
|
||||
currentIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public T set(int index, T value) {
|
||||
if (index < 0 || index >= size) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
Node<T> temp = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
temp = temp.getNext();
|
||||
}
|
||||
|
||||
T oldValue = temp.getValue();
|
||||
temp.setValue(value);
|
||||
return oldValue;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
public void addAll(MyList<? extends T> collection) {
|
||||
if (collection == null || collection.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < collection.size(); i++) {
|
||||
add(collection.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
Object[] res = new Object[size];
|
||||
int index = 0;
|
||||
Node<T> temp = head;
|
||||
while (temp != null) {
|
||||
res[index] = temp.getValue();
|
||||
index++;
|
||||
temp = temp.getNext();
|
||||
}
|
||||
return Arrays.toString(res);
|
||||
}
|
||||
|
||||
private static class Node<T> {
|
||||
private T value;
|
||||
private Node<T> next;
|
||||
|
||||
public Node(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public void setValue(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public void setNext(Node<T> next) {
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public T getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public Node<T> getNext() {
|
||||
return next;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package ru.project.collection;
|
||||
|
||||
public interface MyList<T> {
|
||||
|
||||
void add(T element);
|
||||
|
||||
T get(int index);
|
||||
|
||||
T set(int index, T element);
|
||||
|
||||
void remove(int index);
|
||||
|
||||
int size();
|
||||
|
||||
boolean isEmpty();
|
||||
void addAll(MyList<? extends T> collection);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package ru.project.collectors;
|
||||
import java.util.stream.Collector;
|
||||
|
||||
public class MyCollectors
|
||||
{
|
||||
private MyCollectors() {
|
||||
|
||||
}
|
||||
|
||||
public static <T> Collector<T, MyList<T>, MyList<T>> toMyList()
|
||||
{
|
||||
return new MyListCollector<>();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package ru.project.collectors;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BinaryOperator;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collector;
|
||||
public class MyListCollector <T> implements Collector<T, MyList<T>, MyList<T>>
|
||||
{
|
||||
@Override
|
||||
public Supplier<MyList<T>> supplier()
|
||||
{
|
||||
return () -> new MyLinkedList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public BiConsumer<MyList<T>, T> accumulator() {
|
||||
|
||||
return (list, element) -> list.add(element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BinaryOperator<MyList<T>> combiner() {
|
||||
return (list1, list2) -> {
|
||||
list1.addAll(list2);
|
||||
return list1;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<MyList<T>, MyList<T>> finisher()
|
||||
{
|
||||
return list -> list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Collector.Characteristics> characteristics() {
|
||||
return java.util.Set.of(
|
||||
Collector.Characteristics.IDENTITY_FINISH
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package ru.project.finder;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
public class StudentOccurrenceIndexFinder
|
||||
{
|
||||
public int[] findOccurrences(MyList<Student> students, Student target) {
|
||||
if (students == null) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
if (target == null) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
if (students.isEmpty()) {
|
||||
return new int[0];
|
||||
}
|
||||
int size = students.size();
|
||||
int threadCount = calculateThreadCount(size);
|
||||
int chunkSize = countChunkSize(size, threadCount);
|
||||
|
||||
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
|
||||
MyList<Future<MyList<Integer>>> futures = new MyLinkedList<>();
|
||||
|
||||
for (int i = 0; i < threadCount; i++) {
|
||||
int fromIndex = i * chunkSize;
|
||||
int toIndex = Math.min(fromIndex + chunkSize, size);
|
||||
|
||||
if (fromIndex >= toIndex) {
|
||||
break;
|
||||
}
|
||||
|
||||
final int start = fromIndex;
|
||||
final int end = toIndex;
|
||||
|
||||
Future<MyList<Integer>> future = executor.submit(() -> {
|
||||
return findInRange(students, target, start, end);
|
||||
});
|
||||
futures.add(future);
|
||||
}
|
||||
MyList<Integer> allIndices = new MyLinkedList<>();
|
||||
try {
|
||||
for (int i = 0; i < futures.size(); i++) {
|
||||
Future<MyList<Integer>> future = futures.get(i);
|
||||
MyList<Integer> partialResult = future.get();
|
||||
|
||||
if (partialResult != null) {
|
||||
for (int j = 0; j < partialResult.size(); j++) {
|
||||
allIndices.add(partialResult.get(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Ошибка при поиске", e);
|
||||
} finally {
|
||||
executor.shutdown();
|
||||
}
|
||||
|
||||
sortList(allIndices);
|
||||
|
||||
int[] resultArray = new int[allIndices.size()];
|
||||
for (int i = 0; i < allIndices.size(); i++) {
|
||||
resultArray[i] = allIndices.get(i);
|
||||
}
|
||||
|
||||
return resultArray;
|
||||
}
|
||||
|
||||
private void sortList(MyList<Integer> list) {
|
||||
if (list.size() <= 1) return;
|
||||
|
||||
MyList<Integer> sorted = new MyLinkedList<>();
|
||||
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
int value = list.get(i);
|
||||
int insertIndex = 0;
|
||||
while (insertIndex < sorted.size()) {
|
||||
if (sorted.get(insertIndex) > value) {
|
||||
break;
|
||||
}
|
||||
insertIndex++;
|
||||
}
|
||||
|
||||
MyList<Integer> temp = new MyLinkedList<>();
|
||||
for (int k = 0; k < insertIndex; k++) {
|
||||
temp.add(sorted.get(k));
|
||||
}
|
||||
temp.add(value);
|
||||
for (int k = insertIndex; k < sorted.size(); k++) {
|
||||
temp.add(sorted.get(k));
|
||||
}
|
||||
sorted = temp;
|
||||
}
|
||||
|
||||
while (!list.isEmpty()) {
|
||||
list.remove(0);
|
||||
}
|
||||
|
||||
for (int i = 0; i < sorted.size(); i++) {
|
||||
list.add(sorted.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
private int calculateThreadCount(int size) {
|
||||
int processors = Runtime.getRuntime().availableProcessors();
|
||||
return Math.min(processors, size);
|
||||
}
|
||||
|
||||
private int countChunkSize(int size, int threadCount) {
|
||||
if (threadCount == 0) return 0;
|
||||
return (size + threadCount - 1) / threadCount;
|
||||
}
|
||||
|
||||
private MyList<Integer> findInRange(MyList<Student> students, Student target, int fromIndex, int toIndex) {
|
||||
MyList<Integer> foundIndices = new MyLinkedList<>();
|
||||
|
||||
for (int i = fromIndex; i < toIndex; i++) {
|
||||
Student current = students.get(i);
|
||||
if (current != null && current.equals(target)) {
|
||||
foundIndices.add(i);
|
||||
}
|
||||
}
|
||||
|
||||
return foundIndices;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package ru.project.model;
|
||||
|
||||
import ru.project.collection.MyLinkedList;
|
||||
import ru.project.collection.MyList;
|
||||
import ru.project.student.Student;
|
||||
|
||||
public class AppState {
|
||||
|
||||
private MyList<Student> mainCollection;
|
||||
private MyList<Student> customDataCollection;
|
||||
private String currentFilePath;
|
||||
|
||||
public AppState() {
|
||||
this.mainCollection = new MyLinkedList<>();
|
||||
this.customDataCollection = new MyLinkedList<>();
|
||||
this.currentFilePath = null;
|
||||
}
|
||||
|
||||
public MyList<Student> getMainCollection() {
|
||||
return mainCollection;
|
||||
}
|
||||
|
||||
public void setMainCollection(MyList<Student> currentCollection) {
|
||||
this.mainCollection = currentCollection;
|
||||
}
|
||||
|
||||
public MyList<Student> getCustomDataCollection() {
|
||||
return customDataCollection;
|
||||
}
|
||||
|
||||
public void setCustomDataCollection(MyList<Student> customDataCollection) {
|
||||
|
||||
this.customDataCollection = customDataCollection;
|
||||
}
|
||||
|
||||
public String getCurrentFilePath() {
|
||||
return currentFilePath;
|
||||
}
|
||||
|
||||
public void setCurrentFilePath(String currentFilePath) {
|
||||
this.currentFilePath = currentFilePath;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package ru.project.search;
|
||||
|
||||
import ru.project.student.Student;
|
||||
|
||||
public class AverageGradeRangeCriteria implements StudentSearchCriteria {
|
||||
private final double min;
|
||||
private final double max;
|
||||
|
||||
public AverageGradeRangeCriteria(double min, double max) {
|
||||
if (min > max) {
|
||||
throw new IllegalArgumentException("Min grade must be <= max grade");
|
||||
}
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Student student) {
|
||||
double grade = student.getAverageGrade();
|
||||
return grade >= min && grade <= max;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package ru.project.search;
|
||||
|
||||
import ru.project.student.Student;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CompositeStudentSearchCriteria implements StudentSearchCriteria {
|
||||
private final SearchOperation operation;
|
||||
private final List<StudentSearchCriteria> criteriaList = new ArrayList<>();
|
||||
|
||||
public CompositeStudentSearchCriteria(SearchOperation operation) {
|
||||
if (operation == null) {
|
||||
throw new IllegalArgumentException("Operation must not be null");
|
||||
}
|
||||
this.operation = operation;
|
||||
}
|
||||
|
||||
public void add(StudentSearchCriteria criteria) {
|
||||
if (criteria == null) {
|
||||
throw new IllegalArgumentException("Criteria must not be null");
|
||||
}
|
||||
criteriaList.add(criteria);
|
||||
}
|
||||
|
||||
public void remove(StudentSearchCriteria criteria) {
|
||||
criteriaList.remove(criteria);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Student student) {
|
||||
if (criteriaList.isEmpty()) {
|
||||
return operation == SearchOperation.INTERSECTION;
|
||||
}
|
||||
|
||||
if (operation == SearchOperation.UNION) {
|
||||
for (StudentSearchCriteria c : criteriaList) {
|
||||
if (c.matches(student)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
for (StudentSearchCriteria c : criteriaList) {
|
||||
if (!c.matches(student)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package ru.project.search;
|
||||
|
||||
import ru.project.student.Student;
|
||||
|
||||
public class GroupNumberRangeCriteria implements StudentSearchCriteria {
|
||||
private final String min;
|
||||
private final String max;
|
||||
|
||||
public GroupNumberRangeCriteria(String min, String max) {
|
||||
if (min == null || max == null) {
|
||||
throw new IllegalArgumentException("Min and max must not be null");
|
||||
}
|
||||
if (min.compareTo(max) > 0) {
|
||||
throw new IllegalArgumentException("Min group must be <= max group");
|
||||
}
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Student student) {
|
||||
String group = student.getGroupNumber();
|
||||
return group.compareTo(min) >= 0 && group.compareTo(max) <= 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package ru.project.search;
|
||||
|
||||
import ru.project.student.Student;
|
||||
|
||||
public class RecordBookNumberRangeCriteria implements StudentSearchCriteria {
|
||||
private final int min;
|
||||
private final int max;
|
||||
|
||||
public RecordBookNumberRangeCriteria(int min, int max) {
|
||||
if (min > max) {
|
||||
throw new IllegalArgumentException("Min record number must be <= max");
|
||||
}
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Student student) {
|
||||
int number = student.getRecordBookNumber();
|
||||
return number >= min && number <= max;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package ru.project.search;
|
||||
|
||||
public enum SearchOperation {
|
||||
UNION,
|
||||
INTERSECTION
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package ru.project.search;
|
||||
|
||||
import ru.project.student.Student;
|
||||
|
||||
public interface StudentSearchCriteria {
|
||||
boolean matches(Student student);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package ru.project.search;
|
||||
|
||||
import ru.project.list.MyList;
|
||||
import ru.project.list.MyLinkedList;
|
||||
import ru.project.student.Student;
|
||||
|
||||
public class StudentSearchService {
|
||||
private final StudentSearchCriteria criteria;
|
||||
|
||||
public StudentSearchService(StudentSearchCriteria criteria) {
|
||||
if (criteria == null) {
|
||||
throw new IllegalArgumentException("Criteria must not be null");
|
||||
}
|
||||
this.criteria = criteria;
|
||||
}
|
||||
|
||||
public MyList<Student> find(MyList<Student> students) {
|
||||
if (students == null) {
|
||||
return new MyLinkedList<>();
|
||||
}
|
||||
MyList<Student> result = new MyLinkedList<>();
|
||||
for (int i = 0; i < students.size(); i++) {
|
||||
Student s = students.get(i);
|
||||
if (criteria.matches(s)) {
|
||||
result.add(s);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package ru.project.sorting;
|
||||
|
||||
import java.util.Comparator;
|
||||
import ru.project.collection.MyList;
|
||||
|
||||
public class BubbleSortStrategy<T> implements SortStrategy<T> {
|
||||
|
||||
@Override
|
||||
public void sort(MyList<T> list, Comparator<T> comparator) {
|
||||
if (list == null || list.size() <= 1) {
|
||||
return;
|
||||
}
|
||||
int n = list.size();
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
for (int j = 0; j < n - i - 1; j++) {
|
||||
if (comparator.compare(list.get(j), list.get(j + 1)) > 0) {
|
||||
T temp = list.get(j);
|
||||
list.set(j, list.get(j + 1));
|
||||
list.set(j + 1, temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package ru.project.sorting;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.function.ToIntFunction;
|
||||
import ru.project.collection.MyList;
|
||||
|
||||
public class EvenOnlyMergeSortStrategy<T> implements SortStrategy<T> {
|
||||
|
||||
private final ToIntFunction<T> valueExtractor;
|
||||
|
||||
public EvenOnlyMergeSortStrategy(ToIntFunction<T> valueExtractor) {
|
||||
this.valueExtractor = valueExtractor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sort(MyList<T> list, Comparator<T> comparator) {
|
||||
if (list == null || list.size() <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<Integer> evenIndices = new ArrayList<>();
|
||||
List<T> evenElements = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
T element = list.get(i);
|
||||
int value = valueExtractor.applyAsInt(element);
|
||||
if (value % 2 == 0) {
|
||||
evenIndices.add(i);
|
||||
evenElements.add(element);
|
||||
}
|
||||
}
|
||||
|
||||
if (evenElements.size() <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<T> sortedEven = mergeSort(evenElements, comparator);
|
||||
|
||||
for (int j = 0; j < evenIndices.size(); j++) {
|
||||
list.set(evenIndices.get(j), sortedEven.get(j));
|
||||
}
|
||||
}
|
||||
|
||||
private List<T> mergeSort(List<T> list, Comparator<T> comparator) {
|
||||
if (list.size() <= 1) {
|
||||
return list;
|
||||
}
|
||||
|
||||
int mid = list.size() / 2;
|
||||
List<T> left = mergeSort(list.subList(0, mid), comparator);
|
||||
List<T> right = mergeSort(list.subList(mid, list.size()), comparator);
|
||||
|
||||
return merge(left, right, comparator);
|
||||
}
|
||||
|
||||
private List<T> merge(List<T> left, List<T> right, Comparator<T> comparator) {
|
||||
List<T> result = new ArrayList<>();
|
||||
int i = 0, j = 0;
|
||||
|
||||
while (i < left.size() && j < right.size()) {
|
||||
if (comparator.compare(left.get(i), right.get(j)) <= 0) {
|
||||
result.add(left.get(i));
|
||||
i++;
|
||||
} else {
|
||||
result.add(right.get(j));
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
while (i < left.size()) {
|
||||
result.add(left.get(i));
|
||||
i++;
|
||||
}
|
||||
|
||||
while (j < right.size()) {
|
||||
result.add(right.get(j));
|
||||
j++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package ru.project.sorting;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.function.ToIntFunction;
|
||||
import ru.project.collection.MyLinkedList;
|
||||
import ru.project.collection.MyList;
|
||||
|
||||
public class EvenOnlySortStrategy<T> implements SortStrategy<T> {
|
||||
|
||||
private SortStrategy<T> sortStrategy;
|
||||
private ToIntFunction<T> valueExtractor;
|
||||
|
||||
public EvenOnlySortStrategy() {}
|
||||
|
||||
public EvenOnlySortStrategy(SortStrategy<T> sortStrategy) {
|
||||
setSortStrategy(sortStrategy);
|
||||
}
|
||||
|
||||
public EvenOnlySortStrategy(SortStrategy<T> sortStrategy, ToIntFunction<T> valueExtractor) {
|
||||
setSortStrategy(sortStrategy);
|
||||
setValueExtractor(valueExtractor);
|
||||
}
|
||||
|
||||
public void setSortStrategy(SortStrategy<T> sortStrategy) {
|
||||
if (sortStrategy == null) {
|
||||
throw new IllegalArgumentException("Sort strategy must not be null");
|
||||
}
|
||||
|
||||
this.sortStrategy = sortStrategy;
|
||||
}
|
||||
|
||||
public void setValueExtractor(ToIntFunction<T> valueExtractor) {
|
||||
if (valueExtractor == null) {
|
||||
throw new IllegalArgumentException("Value extractor must not be null");
|
||||
}
|
||||
|
||||
this.valueExtractor = valueExtractor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sort(MyList<T> list, Comparator<T> comparator) {
|
||||
if (sortStrategy == null) {
|
||||
throw new IllegalStateException("Sort strategy is not set");
|
||||
}
|
||||
|
||||
if (valueExtractor == null) {
|
||||
throw new IllegalStateException("Value extractor is not set");
|
||||
}
|
||||
|
||||
if (list == null || list.size() <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
int evenCount = 0;
|
||||
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
T element = list.get(i);
|
||||
|
||||
if (valueExtractor.applyAsInt(element) % 2 == 0) {
|
||||
evenCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if (evenCount <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
MyList<T> evenElements = new MyLinkedList<>();
|
||||
int[] evenIndexes = new int[evenCount];
|
||||
|
||||
int evenIndex = 0;
|
||||
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
T element = list.get(i);
|
||||
|
||||
if (valueExtractor.applyAsInt(element) % 2 == 0) {
|
||||
evenElements.add(element);
|
||||
evenIndexes[evenIndex] = i;
|
||||
evenIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
sortStrategy.sort(evenElements, comparator);
|
||||
|
||||
for (int i = 0; i < evenIndexes.length; i++) {
|
||||
list.set(evenIndexes[i], evenElements.get(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package ru.project.sorting;
|
||||
|
||||
import java.util.Comparator;
|
||||
import ru.project.collection.MyList;
|
||||
|
||||
public class MergeSortStrategy<T> implements SortStrategy<T> {
|
||||
|
||||
@Override
|
||||
public void sort(MyList<T> list, Comparator<T> comparator) {
|
||||
if (list == null || list.size() <= 1) {
|
||||
return;
|
||||
}
|
||||
mergeSort(list, 0, list.size() - 1, comparator);
|
||||
}
|
||||
|
||||
private void mergeSort(MyList<T> list, int left, int right, Comparator<T> comparator) {
|
||||
if (left >= right) {
|
||||
return;
|
||||
}
|
||||
int mid = (left + right) / 2;
|
||||
mergeSort(list, left, mid, comparator);
|
||||
mergeSort(list, mid + 1, right, comparator);
|
||||
merge(list, left, mid, right, comparator);
|
||||
}
|
||||
|
||||
private void merge(MyList<T> list, int left, int mid, int right, Comparator<T> comparator) {
|
||||
int leftSize = mid - left + 1;
|
||||
int rightSize = right - mid;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
T[] leftArray = (T[]) new Object[leftSize];
|
||||
@SuppressWarnings("unchecked")
|
||||
T[] rightArray = (T[]) new Object[rightSize];
|
||||
|
||||
for (int i = 0; i < leftSize; i++) {
|
||||
leftArray[i] = list.get(left + i);
|
||||
}
|
||||
for (int j = 0; j < rightSize; j++) {
|
||||
rightArray[j] = list.get(mid + 1 + j);
|
||||
}
|
||||
|
||||
int i = 0, j = 0, k = left;
|
||||
|
||||
while (i < leftSize && j < rightSize) {
|
||||
if (comparator.compare(leftArray[i], rightArray[j]) <= 0) {
|
||||
list.set(k, leftArray[i]);
|
||||
i++;
|
||||
} else {
|
||||
list.set(k, rightArray[j]);
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
while (i < leftSize) {
|
||||
list.set(k, leftArray[i]);
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
while (j < rightSize) {
|
||||
list.set(k, rightArray[j]);
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package ru.project.sorting;
|
||||
|
||||
import java.util.Comparator;
|
||||
import ru.project.collection.MyList;
|
||||
|
||||
public interface SortStrategy<T> {
|
||||
void sort(MyList<T> list, Comparator<T> comparator);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package ru.project.sorting;
|
||||
|
||||
import java.util.Comparator;
|
||||
import ru.project.student.Student;
|
||||
|
||||
public class StudentAverageGradeComparator implements Comparator<Student> {
|
||||
|
||||
@Override
|
||||
public int compare(Student s1, Student s2) {
|
||||
return Double.compare(s1.getAverageGrade(), s2.getAverageGrade());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package ru.project.sorting;
|
||||
|
||||
import java.util.Comparator;
|
||||
import ru.project.student.Student;
|
||||
|
||||
public class StudentGroupComparator implements Comparator<Student> {
|
||||
|
||||
@Override
|
||||
public int compare(Student s1, Student s2) {
|
||||
return s1.getGroupNumber().compareTo(s2.getGroupNumber());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package ru.project.sorting;
|
||||
|
||||
import java.util.Comparator;
|
||||
import ru.project.student.Student;
|
||||
|
||||
public class StudentRecordBookComparator implements Comparator<Student> {
|
||||
|
||||
@Override
|
||||
public int compare(Student s1, Student s2) {
|
||||
return Integer.compare(s1.getRecordBookNumber(), s2.getRecordBookNumber());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package ru.project.student;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Student {
|
||||
|
||||
public static final double MIN_AVERAGE_GRADE = 0.0;
|
||||
public static final double MAX_AVERAGE_GRADE = 5.0;
|
||||
public static final int MIN_RECORD_BOOK_NUMBER = 1;
|
||||
public static final String GROUP_NUMBER_PATTERN = "^[A-Z][0-9]{2}$";
|
||||
|
||||
private final String groupNumber;
|
||||
private final double averageGrade;
|
||||
private final int recordBookNumber;
|
||||
|
||||
public Student(String groupNumber, double averageGrade, int recordBookNumber) {
|
||||
if (groupNumber == null || !groupNumber.matches(GROUP_NUMBER_PATTERN)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Group number must match pattern: " + GROUP_NUMBER_PATTERN);
|
||||
}
|
||||
if (averageGrade < MIN_AVERAGE_GRADE || averageGrade > MAX_AVERAGE_GRADE) {
|
||||
throw new IllegalArgumentException(
|
||||
"Average grade must be between " + MIN_AVERAGE_GRADE + " and " + MAX_AVERAGE_GRADE);
|
||||
}
|
||||
if (recordBookNumber <= 0) {
|
||||
throw new IllegalArgumentException("Record book number must be positive");
|
||||
}
|
||||
if (recordBookNumber < MIN_RECORD_BOOK_NUMBER) {
|
||||
throw new IllegalArgumentException(
|
||||
"Record book number must be at least " + MIN_RECORD_BOOK_NUMBER);
|
||||
}
|
||||
this.groupNumber = groupNumber;
|
||||
this.averageGrade = averageGrade;
|
||||
this.recordBookNumber = recordBookNumber;
|
||||
}
|
||||
|
||||
public String getGroupNumber() {
|
||||
return groupNumber;
|
||||
}
|
||||
|
||||
public double getAverageGrade() {
|
||||
return averageGrade;
|
||||
}
|
||||
|
||||
public int getRecordBookNumber() {
|
||||
return recordBookNumber;
|
||||
}
|
||||
|
||||
public Student withGroupNumber(String groupNumber) {
|
||||
return new Student(groupNumber, this.averageGrade, this.recordBookNumber);
|
||||
}
|
||||
|
||||
public Student withAverageGrade(double averageGrade) {
|
||||
return new Student(this.groupNumber, averageGrade, this.recordBookNumber);
|
||||
}
|
||||
|
||||
public Student withRecordBookNumber(int recordBookNumber) {
|
||||
return new Student(this.groupNumber, this.averageGrade, recordBookNumber);
|
||||
}
|
||||
|
||||
public static double getMinAverageGrade() {
|
||||
return MIN_AVERAGE_GRADE;
|
||||
}
|
||||
|
||||
public static double getMaxAverageGrade() {
|
||||
return MAX_AVERAGE_GRADE;
|
||||
}
|
||||
|
||||
public static int getMinRecordBookNumber() {
|
||||
return MIN_RECORD_BOOK_NUMBER;
|
||||
}
|
||||
|
||||
public static String getGroupNumberPattern() {
|
||||
return GROUP_NUMBER_PATTERN;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "Student{"
|
||||
+ "groupNumber='"
|
||||
+ groupNumber
|
||||
+ '\''
|
||||
+ ", averageGrade="
|
||||
+ averageGrade
|
||||
+ ", recordBookNumber="
|
||||
+ recordBookNumber
|
||||
+ '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(groupNumber, averageGrade, recordBookNumber);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null || getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
if (this.hashCode() != obj.hashCode()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Student student = (Student) obj;
|
||||
return Double.compare(averageGrade, student.averageGrade) == 0
|
||||
&& recordBookNumber == student.recordBookNumber
|
||||
&& Objects.equals(groupNumber, student.groupNumber);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package ru.project.student;
|
||||
|
||||
public class StudentBuilder {
|
||||
private String groupNumber;
|
||||
private double averageGrade;
|
||||
private int recordBookNumber;
|
||||
|
||||
public StudentBuilder setGroupNumber(String groupNumber) {
|
||||
this.groupNumber = groupNumber;
|
||||
return this;
|
||||
}
|
||||
|
||||
public StudentBuilder setAverageGrade(double averageGrade) {
|
||||
this.averageGrade = averageGrade;
|
||||
return this;
|
||||
}
|
||||
|
||||
public StudentBuilder setRecordBookNumber(int recordBookNumber) {
|
||||
this.recordBookNumber = recordBookNumber;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Student build() {
|
||||
return new Student(groupNumber, averageGrade, recordBookNumber);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package ru.project.ui.base;
|
||||
|
||||
import com.googlecode.lanterna.gui2.BasicWindow;
|
||||
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
|
||||
|
||||
public abstract class BaseModalWindow extends BasicWindow {
|
||||
|
||||
protected final WindowBasedTextGUI gui;
|
||||
|
||||
protected BaseModalWindow(String title, WindowBasedTextGUI gui) {
|
||||
super(title);
|
||||
|
||||
this.gui = gui;
|
||||
}
|
||||
|
||||
public void showModal() {
|
||||
gui.addWindowAndWait(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package ru.project.ui.common;
|
||||
|
||||
import com.googlecode.lanterna.TerminalSize;
|
||||
import com.googlecode.lanterna.gui2.Button;
|
||||
import com.googlecode.lanterna.gui2.Direction;
|
||||
import com.googlecode.lanterna.gui2.EmptySpace;
|
||||
import com.googlecode.lanterna.gui2.LinearLayout;
|
||||
import com.googlecode.lanterna.gui2.Panel;
|
||||
import com.googlecode.lanterna.gui2.TextBox;
|
||||
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
|
||||
import com.googlecode.lanterna.input.KeyStroke;
|
||||
import com.googlecode.lanterna.input.KeyType;
|
||||
import ru.project.collection.MyList;
|
||||
import ru.project.student.Student;
|
||||
import ru.project.ui.base.BaseModalWindow;
|
||||
|
||||
public class CollectionDisplayWindow extends BaseModalWindow {
|
||||
|
||||
private final MyList<Student> collection;
|
||||
|
||||
private TextBox collectionTextBox;
|
||||
private Button exitButton;
|
||||
|
||||
public CollectionDisplayWindow(WindowBasedTextGUI gui, MyList<Student> collection) {
|
||||
|
||||
super("Просмотр коллекции", gui);
|
||||
|
||||
this.collection = collection;
|
||||
|
||||
Panel panel = createPanel();
|
||||
|
||||
setComponent(panel);
|
||||
|
||||
exitButton.takeFocus();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleInput(KeyStroke key) {
|
||||
if (key.getKeyType() == KeyType.Escape && collectionTextBox.isFocused()) {
|
||||
|
||||
exitButton.takeFocus();
|
||||
return true;
|
||||
}
|
||||
|
||||
return super.handleInput(key);
|
||||
}
|
||||
|
||||
private Panel createPanel() {
|
||||
Panel panel = new Panel();
|
||||
|
||||
panel.setLayoutManager(new LinearLayout(Direction.VERTICAL));
|
||||
|
||||
addCollectionDisplay(panel);
|
||||
addExitButton(panel);
|
||||
|
||||
return panel;
|
||||
}
|
||||
|
||||
private void addCollectionDisplay(Panel panel) {
|
||||
collectionTextBox = createCollectionScrollBox();
|
||||
|
||||
panel.addComponent(collectionTextBox);
|
||||
}
|
||||
|
||||
private TextBox createCollectionScrollBox() {
|
||||
TextBox textBox = new TextBox(new TerminalSize(80, 20), TextBox.Style.MULTI_LINE);
|
||||
|
||||
textBox.setReadOnly(true);
|
||||
|
||||
if (collection == null || collection.isEmpty()) {
|
||||
textBox.setText("Коллекция пуста.");
|
||||
} else {
|
||||
textBox.setText(buildCollectionText());
|
||||
}
|
||||
|
||||
return textBox;
|
||||
}
|
||||
|
||||
private void addExitButton(Panel panel) {
|
||||
panel.addComponent(new EmptySpace(new TerminalSize(0, 1)));
|
||||
|
||||
exitButton = new Button("Выйти", this::close);
|
||||
|
||||
panel.addComponent(exitButton);
|
||||
}
|
||||
|
||||
private String buildCollectionText() {
|
||||
StringBuilder text = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < collection.size(); i++) {
|
||||
Student student = collection.get(i);
|
||||
|
||||
text.append("[")
|
||||
.append(i)
|
||||
.append("] ")
|
||||
.append(student == null ? "null" : student)
|
||||
.append("\n");
|
||||
}
|
||||
|
||||
return text.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package ru.project.ui.common;
|
||||
|
||||
import com.googlecode.lanterna.gui2.Button;
|
||||
import com.googlecode.lanterna.gui2.Label;
|
||||
import com.googlecode.lanterna.gui2.LinearLayout;
|
||||
import com.googlecode.lanterna.gui2.Panel;
|
||||
import com.googlecode.lanterna.gui2.TextBox;
|
||||
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
|
||||
import java.util.function.Predicate;
|
||||
import ru.project.ui.base.BaseModalWindow;
|
||||
|
||||
public class InputValueWindow extends BaseModalWindow {
|
||||
|
||||
private final TextBox textBox;
|
||||
private final Predicate<String> onSave;
|
||||
|
||||
public InputValueWindow(
|
||||
WindowBasedTextGUI gui,
|
||||
String title,
|
||||
String prompt,
|
||||
String initialValue,
|
||||
Predicate<String> onSave) {
|
||||
|
||||
super(title, gui);
|
||||
|
||||
this.onSave = onSave;
|
||||
|
||||
Panel panel = new Panel();
|
||||
panel.setLayoutManager(new LinearLayout());
|
||||
|
||||
panel.addComponent(new Label(prompt));
|
||||
|
||||
textBox = new TextBox(initialValue == null ? "" : initialValue);
|
||||
|
||||
panel.addComponent(textBox);
|
||||
|
||||
panel.addComponent(new Button("Сохранить", this::save));
|
||||
|
||||
panel.addComponent(new Button("Отмена", this::close));
|
||||
|
||||
setComponent(panel);
|
||||
}
|
||||
|
||||
private void save() {
|
||||
String value = textBox.getText().trim();
|
||||
|
||||
if (value.isEmpty()) {
|
||||
MessageWindow.showModal(gui, "Значение не может быть пустым.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (onSave.test(value)) {
|
||||
close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package ru.project.ui.common;
|
||||
|
||||
import com.googlecode.lanterna.gui2.Button;
|
||||
import com.googlecode.lanterna.gui2.Label;
|
||||
import com.googlecode.lanterna.gui2.LinearLayout;
|
||||
import com.googlecode.lanterna.gui2.Panel;
|
||||
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
|
||||
import ru.project.ui.base.BaseModalWindow;
|
||||
|
||||
public class MessageWindow extends BaseModalWindow {
|
||||
|
||||
public MessageWindow(WindowBasedTextGUI gui, String message) {
|
||||
super("Информация", gui);
|
||||
|
||||
Panel panel = new Panel();
|
||||
panel.setLayoutManager(new LinearLayout());
|
||||
|
||||
panel.addComponent(new Label(message));
|
||||
|
||||
panel.addComponent(new Button("OK", this::close));
|
||||
|
||||
setComponent(panel);
|
||||
}
|
||||
|
||||
public static void showModal(WindowBasedTextGUI gui, String message) {
|
||||
new MessageWindow(gui, message).showModal();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
package ru.project.ui.creation;
|
||||
|
||||
import com.googlecode.lanterna.gui2.Button;
|
||||
import com.googlecode.lanterna.gui2.Label;
|
||||
import com.googlecode.lanterna.gui2.LinearLayout;
|
||||
import com.googlecode.lanterna.gui2.Panel;
|
||||
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
|
||||
import ru.project.collection.MyLinkedList;
|
||||
import ru.project.collection.MyList;
|
||||
import ru.project.model.AppState;
|
||||
import ru.project.student.Student;
|
||||
import ru.project.ui.base.BaseModalWindow;
|
||||
import ru.project.ui.common.InputValueWindow;
|
||||
import ru.project.ui.common.MessageWindow;
|
||||
|
||||
public class CreateCollectionMenu extends BaseModalWindow {
|
||||
|
||||
private int collectionSize;
|
||||
private FillMethod selectedFillMethod;
|
||||
|
||||
private final Label sizeLabel;
|
||||
private final Label fillMethodLabel;
|
||||
|
||||
private final CustomDataSettingsWindow customDataSettingsWindow;
|
||||
|
||||
public CreateCollectionMenu(WindowBasedTextGUI gui, AppState state) {
|
||||
super("Создание новой коллекции", gui);
|
||||
|
||||
collectionSize = 0;
|
||||
selectedFillMethod = FillMethod.CUSTOM;
|
||||
|
||||
customDataSettingsWindow = new CustomDataSettingsWindow(gui, state, () -> collectionSize);
|
||||
|
||||
Panel panel = new Panel();
|
||||
panel.setLayoutManager(new LinearLayout());
|
||||
|
||||
sizeLabel = new Label("Размер: не задан");
|
||||
|
||||
fillMethodLabel = new Label("Способ заполнения: " + getFillMethodName(selectedFillMethod));
|
||||
|
||||
panel.addComponent(sizeLabel);
|
||||
panel.addComponent(fillMethodLabel);
|
||||
|
||||
panel.addComponent(new Button("Задать размер", this::showCollectionSizeInputValueWindow));
|
||||
|
||||
panel.addComponent(new Button("Выбрать способ заполнения", this::selectFillMethod));
|
||||
|
||||
panel.addComponent(
|
||||
new Button("Настроить текущий способ заполнения", this::setupCurrentFillMethod));
|
||||
|
||||
panel.addComponent(new Button("Создать новую коллекцию", () -> updateMainCollection(state)));
|
||||
|
||||
panel.addComponent(new Button("Выйти", this::close));
|
||||
|
||||
setComponent(panel);
|
||||
}
|
||||
|
||||
private void showCollectionSizeInputValueWindow() {
|
||||
InputValueWindow window =
|
||||
new InputValueWindow(
|
||||
gui,
|
||||
"Размер коллекции",
|
||||
"Введите размер коллекции:",
|
||||
collectionSize > 0 ? String.valueOf(collectionSize) : null,
|
||||
this::handleSizeEntered);
|
||||
|
||||
window.showModal();
|
||||
}
|
||||
|
||||
private boolean handleSizeEntered(String value) {
|
||||
try {
|
||||
int enteredSize = Integer.parseInt(value);
|
||||
if (enteredSize <= 0) {
|
||||
MessageWindow.showModal(gui, "Размер должен быть больше 0.");
|
||||
return false;
|
||||
}
|
||||
collectionSize = enteredSize;
|
||||
sizeLabel.setText("Размер: " + collectionSize);
|
||||
return true;
|
||||
} catch (NumberFormatException e) {
|
||||
MessageWindow.showModal(gui, "Введите целое число.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void setupCurrentFillMethod() {
|
||||
switch (selectedFillMethod) {
|
||||
case CUSTOM -> {
|
||||
customDataSettingsWindow.showModal();
|
||||
}
|
||||
|
||||
case FILE ->
|
||||
// TODO
|
||||
MessageWindow.showModal(gui, "Не реализовано.");
|
||||
|
||||
case RANDOM ->
|
||||
MessageWindow.showModal(
|
||||
gui, "Для случайных данных дополнительные настройки не требуются.");
|
||||
}
|
||||
}
|
||||
|
||||
private void selectFillMethod() {
|
||||
BaseModalWindow menu =
|
||||
new FillMethodSelectionWindow(
|
||||
gui,
|
||||
fillMethod -> {
|
||||
selectedFillMethod = fillMethod;
|
||||
fillMethodLabel.setText(
|
||||
"Способ заполнения: " + getFillMethodName(selectedFillMethod));
|
||||
});
|
||||
|
||||
menu.showModal();
|
||||
}
|
||||
|
||||
private void updateMainCollection(AppState state) {
|
||||
if (collectionSize <= 0) {
|
||||
MessageWindow.showModal(gui, "Сначала задайте размер коллекции.");
|
||||
return;
|
||||
}
|
||||
|
||||
switch (selectedFillMethod) {
|
||||
case CUSTOM:
|
||||
createCollectionFromCustomData(state);
|
||||
break;
|
||||
|
||||
case RANDOM:
|
||||
createCollectionFromRandomData(state);
|
||||
break;
|
||||
|
||||
case FILE:
|
||||
createCollectionFromFileData(state);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void createCollectionFromCustomData(AppState state) {
|
||||
MyList<Student> customDataCollection = state.getCustomDataCollection();
|
||||
|
||||
MyList<Student> mainCollection = new MyLinkedList<>();
|
||||
|
||||
for (int i = 0; i < customDataCollection.size(); i++) {
|
||||
mainCollection.add(customDataCollection.get(i));
|
||||
}
|
||||
|
||||
state.setMainCollection(mainCollection);
|
||||
|
||||
MessageWindow.showModal(gui, "Коллекция успешно создана.");
|
||||
}
|
||||
|
||||
private void createCollectionFromRandomData(AppState state) {
|
||||
// TODO
|
||||
MessageWindow.showModal(gui, "Не реализовано.");
|
||||
}
|
||||
|
||||
private void createCollectionFromFileData(AppState state) {
|
||||
// TODO
|
||||
MessageWindow.showModal(gui, "Не реализовано.");
|
||||
}
|
||||
|
||||
private String getFillMethodName(FillMethod fillMethod) {
|
||||
return switch (fillMethod) {
|
||||
case CUSTOM -> "Пользовательские данные";
|
||||
|
||||
case RANDOM -> "Случайные данные";
|
||||
|
||||
case FILE -> "Данные из файла";
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,289 @@
|
||||
package ru.project.ui.creation;
|
||||
|
||||
import com.googlecode.lanterna.gui2.Button;
|
||||
import com.googlecode.lanterna.gui2.Label;
|
||||
import com.googlecode.lanterna.gui2.LinearLayout;
|
||||
import com.googlecode.lanterna.gui2.Panel;
|
||||
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
|
||||
import java.util.function.Supplier;
|
||||
import ru.project.collection.MyLinkedList;
|
||||
import ru.project.collection.MyList;
|
||||
import ru.project.model.AppState;
|
||||
import ru.project.student.Student;
|
||||
import ru.project.student.StudentBuilder;
|
||||
import ru.project.ui.base.BaseModalWindow;
|
||||
import ru.project.ui.common.CollectionDisplayWindow;
|
||||
import ru.project.ui.common.InputValueWindow;
|
||||
import ru.project.ui.common.MessageWindow;
|
||||
|
||||
public class CustomDataSettingsWindow extends BaseModalWindow {
|
||||
|
||||
private final AppState state;
|
||||
private final Supplier<Integer> requestedCollectionSize;
|
||||
private final StudentBuilder studentBuilder;
|
||||
|
||||
private String groupNumber;
|
||||
private Double averageGrade;
|
||||
private Integer recordBookNumber;
|
||||
private Integer selectedObjectIndex;
|
||||
|
||||
private final Label groupNumberLabel;
|
||||
private final Label averageGradeLabel;
|
||||
private final Label recordBookNumberLabel;
|
||||
private final Label indexLabel;
|
||||
|
||||
private MyList<Student> preparedCustomDataCollection;
|
||||
|
||||
public CustomDataSettingsWindow(
|
||||
WindowBasedTextGUI gui, AppState state, Supplier<Integer> collectionSize) {
|
||||
|
||||
super("Настройка пользовательских данных", gui);
|
||||
|
||||
this.state = state;
|
||||
this.requestedCollectionSize = collectionSize;
|
||||
|
||||
this.studentBuilder = new StudentBuilder();
|
||||
|
||||
Panel panel = new Panel();
|
||||
panel.setLayoutManager(new LinearLayout());
|
||||
|
||||
groupNumberLabel = new Label("Номер группы: не задан");
|
||||
|
||||
averageGradeLabel = new Label("Средний балл: не задан");
|
||||
|
||||
recordBookNumberLabel = new Label("Номер зачётной книжки: не задан");
|
||||
|
||||
indexLabel = new Label("Индекс объекта: не задан");
|
||||
|
||||
panel.addComponent(groupNumberLabel);
|
||||
panel.addComponent(averageGradeLabel);
|
||||
panel.addComponent(recordBookNumberLabel);
|
||||
panel.addComponent(indexLabel);
|
||||
|
||||
panel.addComponent(new Button("Задать номер группы", this::setGroupNumber));
|
||||
|
||||
panel.addComponent(new Button("Задать средний балл", this::setAverageGrade));
|
||||
|
||||
panel.addComponent(new Button("Задать номер зачётной книжки", this::setRecordBookNumber));
|
||||
|
||||
panel.addComponent(new Button("Задать индекс объекта", this::setObjectIndex));
|
||||
|
||||
panel.addComponent(new Button("Задать данные объекта", this::editObject));
|
||||
|
||||
panel.addComponent(new Button("Предпросмотр", this::preview));
|
||||
|
||||
panel.addComponent(new Button("Применить", this::apply));
|
||||
|
||||
panel.addComponent(new Button("Назад", this::close));
|
||||
|
||||
setComponent(panel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showModal() {
|
||||
prepareCustomDataCollection();
|
||||
super.showModal();
|
||||
}
|
||||
|
||||
private void prepareCustomDataCollection() {
|
||||
preparedCustomDataCollection = new MyLinkedList<>();
|
||||
|
||||
int targetCollectionSize = requestedCollectionSize.get();
|
||||
int currentCustomDataCollectionSize = state.getCustomDataCollection().size();
|
||||
|
||||
int elementsToCopy = Math.min(currentCustomDataCollectionSize, targetCollectionSize);
|
||||
|
||||
for (int i = 0; i < elementsToCopy; i++) {
|
||||
preparedCustomDataCollection.add(state.getCustomDataCollection().get(i));
|
||||
}
|
||||
|
||||
for (int i = elementsToCopy; i < targetCollectionSize; i++) {
|
||||
preparedCustomDataCollection.add(null);
|
||||
}
|
||||
}
|
||||
|
||||
private void setGroupNumber() {
|
||||
InputValueWindow window =
|
||||
new InputValueWindow(
|
||||
gui,
|
||||
"Номер группы",
|
||||
"Введите номер группы (например, A12):",
|
||||
groupNumber,
|
||||
value -> {
|
||||
if (!value.matches(Student.getGroupNumberPattern())) {
|
||||
MessageWindow.showModal(gui, "Номер группы должен соответствовать формату A12.");
|
||||
return false;
|
||||
}
|
||||
|
||||
groupNumber = value;
|
||||
studentBuilder.setGroupNumber(value);
|
||||
|
||||
groupNumberLabel.setText("Номер группы: " + groupNumber);
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
window.showModal();
|
||||
}
|
||||
|
||||
private void setAverageGrade() {
|
||||
InputValueWindow window =
|
||||
new InputValueWindow(
|
||||
gui,
|
||||
"Средний балл",
|
||||
"Введите средний балл от 0.0 до 5.0:",
|
||||
averageGrade == null ? null : String.valueOf(averageGrade),
|
||||
value -> {
|
||||
try {
|
||||
double parsedValue = Double.parseDouble(value);
|
||||
|
||||
if (parsedValue < Student.getMinAverageGrade()
|
||||
|| parsedValue > Student.getMaxAverageGrade()) {
|
||||
|
||||
MessageWindow.showModal(gui, "Средний балл должен быть от 0.0 до 5.0.");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
averageGrade = parsedValue;
|
||||
|
||||
studentBuilder.setAverageGrade(parsedValue);
|
||||
|
||||
averageGradeLabel.setText("Средний балл: " + averageGrade);
|
||||
|
||||
return true;
|
||||
|
||||
} catch (NumberFormatException e) {
|
||||
MessageWindow.showModal(gui, "Введите корректное число.");
|
||||
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
window.showModal();
|
||||
}
|
||||
|
||||
private void setRecordBookNumber() {
|
||||
InputValueWindow window =
|
||||
new InputValueWindow(
|
||||
gui,
|
||||
"Номер зачётной книжки",
|
||||
"Введите номер зачётной книжки:",
|
||||
recordBookNumber == null ? null : String.valueOf(recordBookNumber),
|
||||
value -> {
|
||||
try {
|
||||
int parsedValue = Integer.parseInt(value);
|
||||
|
||||
if (parsedValue < Student.getMinRecordBookNumber()) {
|
||||
|
||||
MessageWindow.showModal(gui, "Номер зачётной книжки должен быть положительным.");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
recordBookNumber = parsedValue;
|
||||
|
||||
studentBuilder.setRecordBookNumber(parsedValue);
|
||||
|
||||
recordBookNumberLabel.setText("Номер зачётной книжки: " + recordBookNumber);
|
||||
|
||||
return true;
|
||||
|
||||
} catch (NumberFormatException e) {
|
||||
MessageWindow.showModal(gui, "Введите целое число.");
|
||||
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
window.showModal();
|
||||
}
|
||||
|
||||
private void setObjectIndex() {
|
||||
InputValueWindow window =
|
||||
new InputValueWindow(
|
||||
gui,
|
||||
"Индекс объекта",
|
||||
"Введите индекс объекта:",
|
||||
selectedObjectIndex == null ? null : String.valueOf(selectedObjectIndex),
|
||||
value -> {
|
||||
try {
|
||||
int index = Integer.parseInt(value);
|
||||
|
||||
if (index < 0) {
|
||||
MessageWindow.showModal(gui, "Индекс объекта не может быть отрицательным.");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int size = requestedCollectionSize.get();
|
||||
|
||||
if (index >= size) {
|
||||
MessageWindow.showModal(gui, "Индекс должен быть меньше размера коллекции.");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
selectedObjectIndex = index;
|
||||
|
||||
indexLabel.setText("Индекс объекта: " + selectedObjectIndex);
|
||||
|
||||
return true;
|
||||
|
||||
} catch (NumberFormatException e) {
|
||||
MessageWindow.showModal(gui, "Введите целое число.");
|
||||
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
window.showModal();
|
||||
}
|
||||
|
||||
private void editObject() {
|
||||
if (!hasStudentData()) {
|
||||
MessageWindow.showModal(gui, "Сначала задайте все поля студента.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (selectedObjectIndex == null) {
|
||||
MessageWindow.showModal(gui, "Сначала задайте индекс объекта.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (selectedObjectIndex >= preparedCustomDataCollection.size()) {
|
||||
MessageWindow.showModal(gui, "Индекс объекта выходит за границы подготовленной коллекции.");
|
||||
return;
|
||||
}
|
||||
|
||||
Student student = studentBuilder.build();
|
||||
|
||||
preparedCustomDataCollection.set(selectedObjectIndex, student);
|
||||
|
||||
MessageWindow.showModal(gui, "Данные объекта успешно заданы.");
|
||||
}
|
||||
|
||||
private void apply() {
|
||||
state.setCustomDataCollection(preparedCustomDataCollection);
|
||||
prepareCustomDataCollection();
|
||||
|
||||
MessageWindow.showModal(gui, "Пользовательские данные успешно применены.");
|
||||
}
|
||||
|
||||
private void preview() {
|
||||
if (preparedCustomDataCollection == null || preparedCustomDataCollection.isEmpty()) {
|
||||
|
||||
MessageWindow.showModal(gui, "Подготовленная коллекция пуста.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
CollectionDisplayWindow window = new CollectionDisplayWindow(gui, preparedCustomDataCollection);
|
||||
|
||||
window.showModal();
|
||||
}
|
||||
|
||||
private boolean hasStudentData() {
|
||||
return groupNumber != null && averageGrade != null && recordBookNumber != null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package ru.project.ui.creation;
|
||||
|
||||
public enum FillMethod {
|
||||
CUSTOM,
|
||||
RANDOM,
|
||||
FILE
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package ru.project.ui.creation;
|
||||
|
||||
import com.googlecode.lanterna.gui2.ActionListBox;
|
||||
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
|
||||
import java.util.function.Consumer;
|
||||
import ru.project.ui.base.BaseModalWindow;
|
||||
|
||||
public class FillMethodSelectionWindow extends BaseModalWindow {
|
||||
|
||||
private final Consumer<FillMethod> onFillMethodSelected;
|
||||
|
||||
public FillMethodSelectionWindow(
|
||||
WindowBasedTextGUI gui, Consumer<FillMethod> onFillMethodSelected) {
|
||||
|
||||
super("Выбор способа заполнения", gui);
|
||||
|
||||
this.onFillMethodSelected = onFillMethodSelected;
|
||||
|
||||
ActionListBox menu = new ActionListBox();
|
||||
|
||||
menu.addItem("Пользовательские данные", () -> selectFillMethod(FillMethod.CUSTOM));
|
||||
|
||||
menu.addItem("Случайные данные", () -> selectFillMethod(FillMethod.RANDOM));
|
||||
|
||||
menu.addItem("Данные из файла", () -> selectFillMethod(FillMethod.FILE));
|
||||
|
||||
menu.addItem("Выйти", this::close);
|
||||
|
||||
setComponent(menu);
|
||||
}
|
||||
|
||||
private void selectFillMethod(FillMethod fillMethod) {
|
||||
onFillMethodSelected.accept(fillMethod);
|
||||
close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package ru.project.ui.main;
|
||||
|
||||
import com.googlecode.lanterna.gui2.ActionListBox;
|
||||
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
|
||||
import ru.project.collection.MyList;
|
||||
import ru.project.model.AppState;
|
||||
import ru.project.student.Student;
|
||||
import ru.project.ui.base.BaseModalWindow;
|
||||
import ru.project.ui.common.CollectionDisplayWindow;
|
||||
import ru.project.ui.common.MessageWindow;
|
||||
import ru.project.ui.creation.CreateCollectionMenu;
|
||||
import ru.project.ui.sorting.SortCollectionMenu;
|
||||
|
||||
public class MainMenu extends BaseModalWindow {
|
||||
|
||||
private final ActionListBox menu;
|
||||
|
||||
private final BaseModalWindow createCollectionMenu;
|
||||
private final BaseModalWindow sortCollectionMenu;
|
||||
|
||||
public MainMenu(WindowBasedTextGUI gui, AppState state) {
|
||||
super("Главное меню", gui);
|
||||
|
||||
this.createCollectionMenu = new CreateCollectionMenu(gui, state);
|
||||
this.sortCollectionMenu = new SortCollectionMenu(gui, state);
|
||||
|
||||
menu = new ActionListBox();
|
||||
|
||||
menu.addItem("Показать текущую коллекцию", () -> showMainCollection(state));
|
||||
|
||||
menu.addItem("Меню создания новой коллекции", this::showCreateCollectionMenu);
|
||||
|
||||
menu.addItem("Меню сохранения текущей коллекции в файл", this::saveCollection);
|
||||
|
||||
menu.addItem("Меню сортировки текущей коллекции", this::sortCollection);
|
||||
|
||||
menu.addItem("Меню поиска элемента в текущей коллекции", this::searchCollection);
|
||||
|
||||
menu.addItem("Меню подсчёта количества вхождений элемента", this::countOccurrences);
|
||||
|
||||
menu.addItem("Завершить выполнение программы", this::exit);
|
||||
|
||||
setComponent(menu);
|
||||
}
|
||||
|
||||
private void showMainCollection(AppState state) {
|
||||
MyList<Student> mainCollection = state.getMainCollection();
|
||||
|
||||
BaseModalWindow window = new CollectionDisplayWindow(gui, mainCollection);
|
||||
|
||||
window.showModal();
|
||||
}
|
||||
|
||||
private void showCreateCollectionMenu() {
|
||||
createCollectionMenu.showModal();
|
||||
}
|
||||
|
||||
private void saveCollection() {
|
||||
// TODO
|
||||
MessageWindow.showModal(gui, "Не реализовано.");
|
||||
}
|
||||
|
||||
private void sortCollection() {
|
||||
sortCollectionMenu.showModal();
|
||||
}
|
||||
|
||||
private void searchCollection() {
|
||||
// TODO
|
||||
MessageWindow.showModal(gui, "Не реализовано.");
|
||||
}
|
||||
|
||||
private void countOccurrences() {
|
||||
// TODO
|
||||
MessageWindow.showModal(gui, "Не реализовано.");
|
||||
}
|
||||
|
||||
private void exit() {
|
||||
close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package ru.project.ui.sorting;
|
||||
|
||||
import com.googlecode.lanterna.gui2.ActionListBox;
|
||||
import com.googlecode.lanterna.gui2.Button;
|
||||
import com.googlecode.lanterna.gui2.LinearLayout;
|
||||
import com.googlecode.lanterna.gui2.Panel;
|
||||
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
|
||||
import java.util.function.Consumer;
|
||||
import ru.project.ui.base.BaseModalWindow;
|
||||
|
||||
public class NormalSortSettingsWindow extends BaseModalWindow {
|
||||
|
||||
private final Consumer<SortField> onSortFieldSelected;
|
||||
|
||||
public NormalSortSettingsWindow(WindowBasedTextGUI gui, Consumer<SortField> onSortFieldSelected) {
|
||||
|
||||
super("Обычная сортировка", gui);
|
||||
|
||||
this.onSortFieldSelected = onSortFieldSelected;
|
||||
|
||||
Panel panel = new Panel();
|
||||
panel.setLayoutManager(new LinearLayout());
|
||||
|
||||
ActionListBox menu = new ActionListBox();
|
||||
|
||||
menu.addItem("Сортировать по номеру группы", () -> selectSortField(SortField.GROUP_NUMBER));
|
||||
|
||||
menu.addItem("Сортировать по среднему баллу", () -> selectSortField(SortField.AVERAGE_GRADE));
|
||||
|
||||
menu.addItem(
|
||||
"Сортировать по номеру зачётной книжки",
|
||||
() -> selectSortField(SortField.RECORD_BOOK_NUMBER));
|
||||
|
||||
panel.addComponent(menu);
|
||||
|
||||
panel.addComponent(new Button("Выйти", this::close));
|
||||
|
||||
setComponent(panel);
|
||||
}
|
||||
|
||||
private void selectSortField(SortField sortField) {
|
||||
onSortFieldSelected.accept(sortField);
|
||||
close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package ru.project.ui.sorting;
|
||||
|
||||
import com.googlecode.lanterna.gui2.Button;
|
||||
import com.googlecode.lanterna.gui2.Label;
|
||||
import com.googlecode.lanterna.gui2.LinearLayout;
|
||||
import com.googlecode.lanterna.gui2.Panel;
|
||||
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
|
||||
import ru.project.ui.base.BaseModalWindow;
|
||||
|
||||
public class ParitySortSettingsWindow extends BaseModalWindow {
|
||||
|
||||
public ParitySortSettingsWindow(WindowBasedTextGUI gui) {
|
||||
super("Настройка сортировки по чётности", gui);
|
||||
|
||||
Panel panel = new Panel();
|
||||
panel.setLayoutManager(new LinearLayout());
|
||||
|
||||
panel.addComponent(new Label("Сортировка выполняется по номеру зачётной книжки."));
|
||||
|
||||
panel.addComponent(new Button("Назад", this::close));
|
||||
|
||||
setComponent(panel);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package ru.project.ui.sorting;
|
||||
|
||||
import com.googlecode.lanterna.gui2.Button;
|
||||
import com.googlecode.lanterna.gui2.Label;
|
||||
import com.googlecode.lanterna.gui2.LinearLayout;
|
||||
import com.googlecode.lanterna.gui2.Panel;
|
||||
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
|
||||
import ru.project.model.AppState;
|
||||
import ru.project.ui.base.BaseModalWindow;
|
||||
import ru.project.ui.common.MessageWindow;
|
||||
|
||||
public class SortCollectionMenu extends BaseModalWindow {
|
||||
|
||||
private final AppState state;
|
||||
|
||||
private final Label sortMethodLabel;
|
||||
private final Label sortFieldLabel;
|
||||
|
||||
private final SortMethodSelectionWindow sortMethodSelectionWindow;
|
||||
private final NormalSortSettingsWindow normalSortSettingsWindow;
|
||||
private final ParitySortSettingsWindow paritySortSettingsWindow;
|
||||
|
||||
private SortMethod selectedSortMethod;
|
||||
private SortField selectedSortField;
|
||||
|
||||
public SortCollectionMenu(WindowBasedTextGUI gui, AppState state) {
|
||||
|
||||
super("Меню сортировки текущей коллекции", gui);
|
||||
|
||||
this.state = state;
|
||||
this.selectedSortMethod = SortMethod.NORMAL;
|
||||
this.selectedSortField = SortField.GROUP_NUMBER;
|
||||
|
||||
Panel panel = new Panel();
|
||||
panel.setLayoutManager(new LinearLayout());
|
||||
|
||||
this.sortMethodLabel = new Label("Способ сортировки: " + getSortMethodName(selectedSortMethod));
|
||||
|
||||
this.sortFieldLabel = new Label("Сортировать по: " + getSortFieldName(selectedSortField));
|
||||
|
||||
this.sortMethodSelectionWindow =
|
||||
new SortMethodSelectionWindow(
|
||||
gui,
|
||||
sortMethod -> {
|
||||
selectedSortMethod = sortMethod;
|
||||
|
||||
sortMethodLabel.setText(
|
||||
"Способ сортировки: " + getSortMethodName(selectedSortMethod));
|
||||
});
|
||||
|
||||
this.normalSortSettingsWindow =
|
||||
new NormalSortSettingsWindow(
|
||||
gui,
|
||||
sortField -> {
|
||||
selectedSortField = sortField;
|
||||
});
|
||||
|
||||
this.paritySortSettingsWindow = new ParitySortSettingsWindow(gui);
|
||||
|
||||
panel.addComponent(sortMethodLabel);
|
||||
panel.addComponent(sortFieldLabel);
|
||||
|
||||
panel.addComponent(new Button("Выбрать способ сортировки", this::selectSortMethod));
|
||||
|
||||
panel.addComponent(
|
||||
new Button("Настроить текущий способ сортировки", this::setupCurrentSortMethod));
|
||||
|
||||
panel.addComponent(new Button("Сортировать текущую коллекцию", this::sortCurrentCollection));
|
||||
|
||||
panel.addComponent(new Button("Выйти", this::close));
|
||||
|
||||
setComponent(panel);
|
||||
}
|
||||
|
||||
private void setupCurrentSortMethod() {
|
||||
switch (selectedSortMethod) {
|
||||
case NORMAL -> normalSortSettingsWindow.showModal();
|
||||
|
||||
case PARITY -> paritySortSettingsWindow.showModal();
|
||||
}
|
||||
}
|
||||
|
||||
private void selectSortMethod() {
|
||||
sortMethodSelectionWindow.showModal();
|
||||
}
|
||||
|
||||
private void sortCurrentCollection() {
|
||||
if (state.getMainCollection() == null || state.getMainCollection().isEmpty()) {
|
||||
|
||||
MessageWindow.showModal(gui, "Текущая коллекция пуста.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
switch (selectedSortMethod) {
|
||||
case NORMAL -> sortNormally();
|
||||
|
||||
case PARITY -> sortByParity();
|
||||
}
|
||||
}
|
||||
|
||||
private void sortNormally() {
|
||||
// TODO
|
||||
MessageWindow.showModal(gui, "Обычная сортировка не реализована.");
|
||||
}
|
||||
|
||||
private void sortByParity() {
|
||||
// TODO
|
||||
MessageWindow.showModal(gui, "Сортировка по чётности не реализована.");
|
||||
}
|
||||
|
||||
private String getSortMethodName(SortMethod sortMethod) {
|
||||
return switch (sortMethod) {
|
||||
case NORMAL -> "Обычная сортировка";
|
||||
case PARITY -> "Сортировка по чётности";
|
||||
};
|
||||
}
|
||||
|
||||
private String getSortFieldName(SortField sortField) {
|
||||
return switch (sortField) {
|
||||
case GROUP_NUMBER -> "номеру группы";
|
||||
case AVERAGE_GRADE -> "среднему баллу";
|
||||
case RECORD_BOOK_NUMBER -> "номеру зачётной книжки";
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package ru.project.ui.sorting;
|
||||
|
||||
public enum SortField {
|
||||
GROUP_NUMBER,
|
||||
AVERAGE_GRADE,
|
||||
RECORD_BOOK_NUMBER
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package ru.project.ui.sorting;
|
||||
|
||||
public enum SortMethod {
|
||||
NORMAL,
|
||||
PARITY
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package ru.project.ui.sorting;
|
||||
|
||||
import com.googlecode.lanterna.gui2.ActionListBox;
|
||||
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
|
||||
import java.util.function.Consumer;
|
||||
import ru.project.ui.base.BaseModalWindow;
|
||||
|
||||
public class SortMethodSelectionWindow extends BaseModalWindow {
|
||||
|
||||
private final Consumer<SortMethod> onSortMethodSelected;
|
||||
|
||||
public SortMethodSelectionWindow(
|
||||
WindowBasedTextGUI gui, Consumer<SortMethod> onSortMethodSelected) {
|
||||
|
||||
super("Выбрать способ сортировки", gui);
|
||||
|
||||
this.onSortMethodSelected = onSortMethodSelected;
|
||||
|
||||
ActionListBox menu = new ActionListBox();
|
||||
|
||||
menu.addItem("Обычная сортировка", () -> selectSortMethod(SortMethod.NORMAL));
|
||||
|
||||
menu.addItem("Сортировка по чётности", () -> selectSortMethod(SortMethod.PARITY));
|
||||
|
||||
menu.addItem("Выйти", this::close);
|
||||
|
||||
setComponent(menu);
|
||||
}
|
||||
|
||||
private void selectSortMethod(SortMethod sortMethod) {
|
||||
onSortMethodSelected.accept(sortMethod);
|
||||
close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user