Untitled

 avatar
unknown
plain_text
3 years ago
2.1 kB
6
Indexable
public class StudentManagement {
    /**
     * declare array.
     * @param int index;
     */
    private Student[] students = new Student[100];
    private int index = 0;

    /**
     * compare two student group.
     */
    public static boolean sameGroup(Student s1, Student s2) {
        return s1.getGroup().equals(s2.getGroup());
    }

    /**
     * addStudent.
     */
    public void addStudent(Student newStudent) {
        students[index] = newStudent;
        index++;
    }

    /**
     * studentsByGroup.
     */
    public String studentsByGroup() {
        if (index == 0) {
            return "";
        }
        String[] filteredGroup = new String[100];
        int tmp = 0;
        String ans = "";
        for (int i = 0; i < index; i++) {
            boolean check = false;
            for (int j = 0; j < tmp; j++) {
                if ((students[i].getGroup()).equals(filteredGroup[j])) {
                    check = true;
                    break;
                }
            }
            if (!check) {
                filteredGroup[tmp] = students[i].getGroup();
                tmp++;
            }
        }
        for (int i = 0; i < tmp; i++) {
            ans = ans + filteredGroup[i] + "\n";
            for (int j = 0; j < index; j++) {
                if ((students[j].getGroup()).equals(filteredGroup[i])) {
                    ans = ans + students[j].getInfo() + "\n";
                }
            }
        }
        return ans;
    }

    /**
     * removeStudent.
     */
    public void removeStudent(String id) {
        int removeIndex = -1;
        for (int i = 0; i < index; i++) {
            if (students[i].getId().equals(id)) {
                removeIndex = i;
                break;
            }
        }
        if (removeIndex == -1) {
            return;
        }
        for (int i = removeIndex; i < index; i++) {
            students[i] = students[i + 1];
        }
        students[index - 1] = null;
        --index;
    }

    public static void main(String[] args) {

    }
}
Editor is loading...