Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.util.*;
- import java.util.stream.Collectors;
- public class StudentGroups {
- public static class Group {
- String town;
- List<Student> students;
- Group(String town, List<Student> students) {
- this.town = town;
- this.students = students;
- }
- List<Student> getStudents() {
- return students;
- }
- String getTown() {
- return town;
- }
- }
- public static class Student {
- String name;
- String email;
- LocalDate date;
- Student(String name, String email, LocalDate date) {
- this.name = name;
- this.email = email;
- this.date = date;
- }
- String getName() {
- return name;
- }
- String getEmail() {
- return email;
- }
- LocalDate getDate() {
- return date;
- }
- @Override
- public String toString() {
- return String.join(", ", this.getEmail());
- }
- }
- public static class Town {
- String name;
- int seats;
- List<Student> students;
- Town(String name, int seats, List<Student> students) {
- this.name = name;
- this.seats = seats;
- this.students = students;
- }
- String getName() {
- return name;
- }
- int getSeats() {
- return seats;
- }
- List<Student> getStudents() {
- return students;
- }
- void setStudents(Student student) {
- this.students.add(student);
- }
- }
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Town> towns = new ArrayList<>();
- List<Group> groups = new ArrayList<>();
- String input = scanner.nextLine();
- while (!input.equals("End")) {
- if (input.contains("=>")) {
- String[] inputTowns = input.split(" => ");
- String townName = inputTowns[0];
- String[] inputSeats = inputTowns[1].split("\\s+");
- int seats = Integer.parseInt(inputSeats[0]);
- Town town = new Town(townName, seats, new ArrayList<Student>());
- towns.add(town);
- } else {
- String[] inputStudents = input.split("\\|");
- String studentName = inputStudents[0];
- String studentEmail = inputStudents[1].trim();
- String studentDate = inputStudents[2].trim();
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d-MMM-yyyy", Locale.getDefault());
- LocalDate date = LocalDate.parse(studentDate, formatter);
- Student student = new Student(studentName, studentEmail, date);
- towns.get(towns.size() - 1).setStudents(student);
- }
- input = scanner.nextLine();
- }
- for (Town town : towns/*.stream().sorted(Comparator.comparing(Town::getName)).collect(Collectors.toList())*/) {
- int groupCount = (int) Math.ceil(town.getStudents().size() / (double) town.getSeats());
- town.getStudents().sort(Comparator.comparing(Student::getDate).thenComparing(Student::getName).thenComparing(Student::getEmail));
- for (int i = 0; i < groupCount; i++) {
- groups.add(new Group(town.getName(), town.getStudents().stream().skip(i * town.getSeats()).limit(town.getSeats()).collect(Collectors.toList())));
- }
- }
- System.out.printf("Created %d groups in %d towns:%n", groups.size(), towns.stream().map(Town::getName).distinct().count());
- groups.stream().sorted(Comparator.comparing(Group::getTown)).forEach(group -> {
- System.out.printf("%s => ", group.getTown());
- System.out.println(group.getStudents().toString().replaceAll("[\\[\\]]", ""));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement