174 lines
6.8 KiB
Java
174 lines
6.8 KiB
Java
package server;
|
|
|
|
import common.enums.EventType;
|
|
import common.models.Coordinates;
|
|
import common.models.Event;
|
|
import common.models.Ticket;
|
|
import common.enums.TicketType;
|
|
|
|
import java.util.ArrayDeque;
|
|
import java.util.Comparator;
|
|
import java.util.Scanner;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class CollectionManager {
|
|
private ArrayDeque<Ticket> tickets;
|
|
private FileManager fileManager;
|
|
private Scanner scanner = new Scanner(System.in);
|
|
|
|
public CollectionManager(FileManager fileManager) {
|
|
this.fileManager = fileManager;
|
|
this.tickets = fileManager.loadCollection();
|
|
}
|
|
|
|
public String addTicket(Ticket ticket) {
|
|
tickets.add(ticket);
|
|
return "Ticket added successfully.";
|
|
}
|
|
|
|
public String updateTicketById(long id, Ticket newTicket) {
|
|
for (Ticket ticket : tickets) {
|
|
if (ticket.getId() == id) {
|
|
tickets.remove(ticket);
|
|
tickets.add(newTicket);
|
|
return "Ticket updated successfully.";
|
|
}
|
|
}
|
|
return "Ticket with ID " + id + " not found.";
|
|
}
|
|
|
|
public String removeTicketById(long id) {
|
|
if (tickets.removeIf(ticket -> ticket.getId() == id)) {
|
|
return "Ticket removed successfully.";
|
|
}
|
|
return "Ticket with ID " + id + " not found.";
|
|
}
|
|
|
|
public String clear() {
|
|
tickets.clear();
|
|
return "Collection cleared.";
|
|
}
|
|
|
|
public String show() {
|
|
return tickets.stream()
|
|
.map(Ticket::toString)
|
|
.collect(Collectors.joining("\n"));
|
|
}
|
|
|
|
public String getInfo() {
|
|
return "Collection type: " + tickets.getClass() + "\n" +
|
|
"Initialization date: " + /* initialization date here */ "\n" +
|
|
"Number of elements: " + tickets.size();
|
|
}
|
|
|
|
public String saveCollection() {
|
|
fileManager.saveCollection(tickets);
|
|
return "Collection saved.";
|
|
}
|
|
|
|
public String removeHead() {
|
|
if (tickets.isEmpty()) return "Collection is empty.";
|
|
Ticket first = tickets.poll();
|
|
return (first != null) ? first.toString() : "Collection is empty.";
|
|
}
|
|
|
|
public String addIfMin(Ticket newTicket) {
|
|
Ticket min = tickets.stream().min(Comparator.naturalOrder()).orElse(null);
|
|
if (min == null || newTicket.compareTo(min) < 0) {
|
|
tickets.add(newTicket);
|
|
return "Ticket added successfully.";
|
|
}
|
|
return "New ticket is not less than the smallest ticket in the collection.";
|
|
}
|
|
|
|
public String removeGreater(Ticket ticket) {
|
|
int initialSize = tickets.size();
|
|
tickets.removeIf(t -> t.compareTo(ticket) > 0);
|
|
return "Removed " + (initialSize - tickets.size()) + " tickets.";
|
|
}
|
|
|
|
public String groupCountingByType() {
|
|
return tickets.stream()
|
|
.collect(Collectors.groupingBy(Ticket::getType, Collectors.counting()))
|
|
.entrySet().stream()
|
|
.map(entry -> entry.getKey() + ": " + entry.getValue())
|
|
.collect(Collectors.joining("\n"));
|
|
}
|
|
|
|
public String filterByEvent(String eventName) {
|
|
return tickets.stream()
|
|
.filter(ticket -> ticket.getEvent() != null && ticket.getEvent().getName().equals(eventName))
|
|
.map(Ticket::toString)
|
|
.collect(Collectors.joining("\n"));
|
|
}
|
|
|
|
public String filterLessThanType(String typeStr) {
|
|
TicketType type;
|
|
try {
|
|
type = TicketType.valueOf(typeStr.toUpperCase());
|
|
} catch (IllegalArgumentException e) {
|
|
return "Invalid ticket type: " + typeStr;
|
|
}
|
|
return tickets.stream()
|
|
.filter(ticket -> ticket.getType() != null && ticket.getType().compareTo(type) < 0)
|
|
.map(Ticket::toString)
|
|
.collect(Collectors.joining("\n"));
|
|
}
|
|
|
|
public Ticket createTicket() {
|
|
System.out.println("Введите название билета:");
|
|
String name = scanner.nextLine().trim();
|
|
while (name.isEmpty()) {
|
|
System.out.println("Название не может быть пустым. Пожалуйста, введите название билета:");
|
|
name = scanner.nextLine().trim();
|
|
}
|
|
|
|
System.out.println("Введите координату x (целое число <= 794):");
|
|
int x = scanner.nextInt();
|
|
while (x > 794) {
|
|
System.out.println("Координата x должна быть <= 794. Пожалуйста, введите координату x:");
|
|
x = scanner.nextInt();
|
|
}
|
|
|
|
System.out.println("Введите координату y (целое число):");
|
|
int y = scanner.nextInt();
|
|
|
|
scanner.nextLine(); // очистка буфера
|
|
|
|
System.out.println("Введите цену билета (положительное число или оставьте пустым):");
|
|
String priceInput = scanner.nextLine().trim();
|
|
Long price = priceInput.isEmpty() ? null : Long.parseLong(priceInput);
|
|
|
|
System.out.println("Введите скидку на билет (от 0 до 100 или оставьте пустым):");
|
|
String discountInput = scanner.nextLine().trim();
|
|
Long discount = discountInput.isEmpty() ? null : Long.parseLong(discountInput);
|
|
|
|
System.out.println("Выберите тип билета (VIP, USUAL, BUDGETARY, CHEAP или оставьте пустым):");
|
|
TicketType type = null;
|
|
String typeStr = scanner.nextLine().trim().toUpperCase();
|
|
if (!typeStr.isEmpty()) {
|
|
type = TicketType.valueOf(typeStr);
|
|
}
|
|
|
|
// Для Event и EventType можно добавить аналогичный процесс ввода
|
|
// Предполагаем, что для Event нужно ввести дополнительные данные, например, название события
|
|
System.out.println("Введите название события (или оставьте пустым):");
|
|
String eventName = scanner.nextLine().trim();
|
|
Event event = null;
|
|
if (!eventName.isEmpty()) {
|
|
// Здесь должна быть логика создания объекта Event на основе введенных данных
|
|
event = new Event(
|
|
1, // TODO: сгенерировать ID автоматически
|
|
eventName,
|
|
1000,
|
|
"Some event",
|
|
EventType.BASEBALL
|
|
);
|
|
}
|
|
|
|
// Предполагается, что ID генерируется автоматически
|
|
// Дата создания устанавливается текущей датой
|
|
return new Ticket(name, new Coordinates(x, y), price, discount, type, event);
|
|
}
|
|
}
|