Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mail.lenka949;
- import java.util.TreeSet;
- public class Group {
- private TreeSet<Student> students = new TreeSet<Student>(new StudentComporator());
- private int num = 0;
- public Group() {
- super();
- }
- public void addStudent(Student student) {
- students.add(student);
- }
- public void removeStudent(Student student) {
- if (students.contains(student)) {
- students.remove(student);
- } else {
- System.out.println("The student is not found in this group");
- return;
- }
- }
- public Student getStudentByName(String lastName) {
- Student st = students.stream().filter(lastName::equals).findFirst().orElse(null);
- return st;
- }
- public String genListByChar(char c) {
- StringBuffer sb = new StringBuffer();
- sb.append("Group:").append(c).append(System.lineSeparator());
- students.stream().filter(n -> n.getLastName().charAt(0) == c)
- .forEachOrdered(n -> sb.append(getNextNum()).append(n).append(System.lineSeparator()));
- this.num = 0;
- return sb.toString();
- }
- public Group genGroupByChar(char c) {
- Group newGroup = new Group();
- students.stream().filter(n -> n.getLastName().charAt(0) == c)
- .forEachOrdered(n -> newGroup.addStudent(n));
- return newGroup;
- }
- private String getNextNum(){
- this.num = num + 1;
- return num + ") ";
- }
- @Override
- public String toString() {
- StringBuffer sb = new StringBuffer();
- sb.append("Group:").append(System.lineSeparator());
- students.stream().forEachOrdered(n -> sb.append(getNextNum()).append(n).append(System.lineSeparator()));
- this.num = 0;
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement