SHARE
TWEET

Java Fundamentals - Student Groups

mark79 Aug 12th, 2019 161 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.time.LocalDate;
  2. import java.time.format.DateTimeFormatter;
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class StudentGroups {
  7.  
  8.     public static class Group {
  9.         String town;
  10.         List<Student> students;
  11.  
  12.         Group(String town, List<Student> students) {
  13.             this.town = town;
  14.             this.students = students;
  15.         }
  16.  
  17.         List<Student> getStudents() {
  18.             return students;
  19.         }
  20.  
  21.         String getTown() {
  22.             return town;
  23.         }
  24.     }
  25.  
  26.     public static class Student {
  27.         String name;
  28.         String email;
  29.         LocalDate date;
  30.  
  31.         Student(String name, String email, LocalDate date) {
  32.             this.name = name;
  33.             this.email = email;
  34.             this.date = date;
  35.         }
  36.  
  37.         String getName() {
  38.             return name;
  39.         }
  40.  
  41.         String getEmail() {
  42.             return email;
  43.         }
  44.  
  45.         LocalDate getDate() {
  46.             return date;
  47.         }
  48.  
  49.         @Override
  50.         public String toString() {
  51.             return String.join(", ", this.getEmail());
  52.         }
  53.     }
  54.  
  55.     public static class Town {
  56.         String name;
  57.         int seats;
  58.         List<Student> students;
  59.  
  60.         Town(String name, int seats, List<Student> students) {
  61.             this.name = name;
  62.             this.seats = seats;
  63.             this.students = students;
  64.         }
  65.  
  66.         String getName() {
  67.             return name;
  68.         }
  69.  
  70.         int getSeats() {
  71.             return seats;
  72.         }
  73.  
  74.         List<Student> getStudents() {
  75.             return students;
  76.         }
  77.  
  78.         void setStudents(Student student) {
  79.             this.students.add(student);
  80.         }
  81.     }
  82.  
  83.     public static void main(String[] args) {
  84.         Scanner scanner = new Scanner(System.in);
  85.  
  86.         List<Town> towns = new ArrayList<>();
  87.         List<Group> groups = new ArrayList<>();
  88.  
  89.         String input = scanner.nextLine();
  90.         while (!input.equals("End")) {
  91.  
  92.             if (input.contains("=>")) {
  93.                 String[] inputTowns = input.split(" => ");
  94.                 String townName = inputTowns[0];
  95.                 String[] inputSeats = inputTowns[1].split("\\s+");
  96.                 int seats = Integer.parseInt(inputSeats[0]);
  97.                 Town town = new Town(townName, seats, new ArrayList<Student>());
  98.                 towns.add(town);
  99.             } else {
  100.                 String[] inputStudents = input.split("\\|");
  101.                 String studentName = inputStudents[0];
  102.                 String studentEmail = inputStudents[1].trim();
  103.                 String studentDate = inputStudents[2].trim();
  104.  
  105.                 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d-MMM-yyyy", Locale.getDefault());
  106.                 LocalDate date = LocalDate.parse(studentDate, formatter);
  107.  
  108.                 Student student = new Student(studentName, studentEmail, date);
  109.                 towns.get(towns.size() - 1).setStudents(student);
  110.             }
  111.  
  112.             input = scanner.nextLine();
  113.         }
  114.  
  115.         for (Town town : towns/*.stream().sorted(Comparator.comparing(Town::getName)).collect(Collectors.toList())*/) {
  116.             int groupCount = (int) Math.ceil(town.getStudents().size() / (double) town.getSeats());
  117.             town.getStudents().sort(Comparator.comparing(Student::getDate).thenComparing(Student::getName).thenComparing(Student::getEmail));
  118.             for (int i = 0; i < groupCount; i++) {
  119.                 groups.add(new Group(town.getName(), town.getStudents().stream().skip(i * town.getSeats()).limit(town.getSeats()).collect(Collectors.toList())));
  120.             }
  121.         }
  122.  
  123.         System.out.printf("Created %d groups in %d towns:%n", groups.size(), towns.stream().map(Town::getName).distinct().count());
  124.         groups.stream().sorted(Comparator.comparing(Group::getTown)).forEach(group -> {
  125.             System.out.printf("%s => ", group.getTown());
  126.             System.out.println(group.getStudents().toString().replaceAll("[\\[\\]]", ""));
  127.         });
  128.     }
  129. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top