Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.9 kB
4
Indexable
Never
public class Student {
    /**
     * declare properties private.
     * @param String name
     * @param String id
     * @param String group
     * @param String email
     */
    private String name;
    private String id;
    private String group;
    private String email;

    /**
     * getName.
     */
    public String getName() {
        return name;
    }

    /**
     * getId.
     */
    public String getId() {
        return id;
    }

    /**
     * getGroup.
     */
    public String getGroup() {
        return group;
    }

    /**
     * getEmail.
     */
    public String getEmail() {
        return email;
    }

    /**
     * setName.
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * setId.
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * setGroup.
     */
    public void setGroup(String group) {
        this.group = group;
    }

    /**
     * setEmail.
     */
    public void setEmail() {
        this.email = email;
    }

    /**
     * getInfo.
     */
    public String getInfo() {
        return name + " - " + id + " - " + group + " - " + email;
    }

    /**
     * Constructor.
     */
    public Student() {
        name = "Student";
        id = "000";
        group = "K62CB";
        email = "uet@vnu.edu.vn";
    }

    /**
     * Constructor.
     */
    Student(String name, String id, String email) {
        this.name = name;
        this.id = id;
        this.email = email;
        group = "K62CB";
    }

    /**
     * Constructor.
     */
    Student(Student s) {
        this.name = s.name;
        this.id = s.id;
        this.group = s.group;
        this.email = s.email;
    }
}


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--;
    }

}