52 lines
1.6 KiB
Java
52 lines
1.6 KiB
Java
package model;
|
|
|
|
import java.time.LocalDate;
|
|
import java.util.*;
|
|
|
|
public class OperationJournal {
|
|
private final Map<Long, Operation> operations = new HashMap<>();
|
|
|
|
public void saveOperation(Operation operation) {
|
|
|
|
if (operation == null) {
|
|
throw new IllegalArgumentException("Не указана операция для сохранения в журнал");
|
|
}
|
|
|
|
operations.put(operation.getId(), operation);
|
|
}
|
|
|
|
public Operation getOperationById(Long id) {
|
|
if (id == null) {
|
|
throw new IllegalArgumentException("Не указан ID операции");
|
|
}
|
|
return operations.get(id);
|
|
}
|
|
|
|
public List<Operation> getOperationsBetweenDates(LocalDate dateFrom, LocalDate dateTo) {
|
|
|
|
if (dateFrom == null || dateTo == null) {
|
|
throw new IllegalArgumentException(
|
|
"Необходимо указать начало и конец периода"
|
|
);
|
|
}
|
|
|
|
if (dateFrom.isAfter(dateTo)) {
|
|
throw new IllegalArgumentException(
|
|
"Начало периода не может быть позже конца"
|
|
);
|
|
}
|
|
|
|
List<Operation> result = new ArrayList<>();
|
|
|
|
for (Operation operation : operations.values()) {
|
|
if (!operation.getDate().isBefore(dateFrom) && !operation.getDate().isAfter(dateTo)) {
|
|
result.add(operation);
|
|
}
|
|
}
|
|
|
|
result.sort(Comparator.comparing(Operation::getDate).thenComparing(Operation::getId));
|
|
|
|
return result;
|
|
}
|
|
}
|