SystemManagement

 avatar
unknown
java
3 years ago
2.5 kB
3
Indexable
import java.util.Objects;

public class StudentManagement {
    private Student[] students = new Student[100];

    public int total = 0;

    public static boolean sameGroup(Student s1, Student s2) {
        return (Objects.equals(s1.getGroup(), s2.getGroup()));
    }

    public void addStudent(Student newStudent) {
        students[total] = new Student(newStudent);
        total++;
    }

    /**
     * Get students info by group.
     */
    public String studentsByGroup() {
        Boolean[] appear = new Boolean[total];
        for (int i = 0; i < total; i++) {
            appear[i] = false;
        }
        String result = "";

        for (int i = 0; i < total; i++) {
            if (appear[i]) {
                continue;
            }
            if (i != 0) {
                result += '\n';
            }
            result += students[i].getGroup();
            for (int j = i; j < total; j++) {
                if (Objects.equals(students[i].getGroup(), students[j].getGroup())) {
                    appear[j] = true;
                    result += '\n';
                    result += students[j].getInfo();
                }
            }
        }
        return result;
    }

    /**
     * Remove a student by id.
     * @param id represents id string.
     */
    public void removeStudent(String id) {
        for (int i = 0; i < total; i++) {
            if (students[i].getId().equals(id)) {
                for (int j = i; j < total - 1; j++) {
                    students[j] = students[j + 1];
                }
                students[total - 1] = null;
                total--;
            }
        }
    }

    public static void main(String[] args) {
        StudentManagement sm = new StudentManagement();
        Student s1 = new Student("A", "111", "email");
        s1.setGroup("K62CA");
        Student s2 = new Student("B", "112", "email");
        s2.setGroup("K62CA");

        Student s3 = new Student("C", "113", "email");
        s3.setGroup("K62CC");

        Student s4 = new Student("D", "113", "email");
        s4.setGroup("K62CA");
        sm.addStudent(s1);
        sm.addStudent(s2);
        sm.addStudent(s3);
        sm.addStudent(s4);
        //System.out.println(sm.studentsByGroup());
        //sm.removeStudent("111");
        sm.removeStudent("111");
        System.out.println("---");
        System.out.println(sm.total);
        System.out.println(sm.studentsByGroup());
    }
}
Editor is loading...