Untitled
unknown
plain_text
3 years ago
4.1 kB
3
Indexable
import java.util.Objects; public class StudentManagement { static Student[] students = new Student[100]; static int index = 0; static int indexGroup = 0; static String[] gs = new String[100]; /** *An abstract class that represents an algorithm. * * @author zhangtj * * @version 1.0 */ public static boolean sameGroup(Student s1, Student s2) { if (Objects.equals(s1.getGroup(), s2.getGroup())) { return true; } else { return false; } } /** *An abstract class that represents an algorithm. * * @author zhangtj * * @version 1.0 */ public static boolean groupExist(String s) { for (int i = 0; i < 100; i++) { if (Objects.equals(s, gs[i])) { return true; } } return false; } /** *An abstract class that represents an algorithm. * * @author zhangtj * * @version 1.0 */ public static boolean sameStudent(Student s1, Student s2) { return Objects.equals(s1.getName(), s2.getName()) && Objects.equals(s1.getGroup(), s2.getGroup()) && Objects.equals(s1.getId(), s2.getId()) && Objects.equals(s1.getEmail(), s2.getEmail()); } /** *An abstract class that represents an algorithm. * * @author zhangtj * * @version 1.0 */ public void addStudent(Student newStudent) { boolean check = false; for (int i = 0; i < index; i++) { if (sameStudent(newStudent, students[i])) { check = true; } } if (!check) { if (index < students.length) { students[index] = newStudent; index++; } else { System.out.println("No more room for student"); } } } /** *An abstract class that represents an algorithm. * * @author zhangtj * * @version 1.0 */ public String studentsByGroup() { String str = ""; for (int i = 0; i < index; i++) { if (!groupExist(students[i].getGroup())) { String newGroup = students[i].getGroup(); gs[indexGroup] = newGroup; indexGroup++; } } for (int i = 0; i < indexGroup; i++) { str = str + gs[i] + "\n"; for (int j = 0; j < index; j++) { if (Objects.equals(gs[i], students[j].getGroup())) { str = str + students[j].getInfor() + "\n"; } } } return str; } /** *An abstract class that represents an algorithm. * * @author zhangtj * * @version 1.0 */ public void removeStudent(String id) { String tempId = ""; boolean check = false; for (int i = 0; i < index; i++) { if (Objects.equals(students[i].getId(), id)) { tempId = students[i].getGroup(); for (int j = i; j < index - 1; j++) { students[j] = students[j + 1]; } students[index - 1] = null; index = index - 1; } } for (int i = 0; i < index; i++) { if (Objects.equals(students[i].getGroup(), tempId)) { check = true; } } if (!check && indexGroup > 0) { for (int i = 0; i < indexGroup - 1; i++) { gs[i] = gs[i + 1]; } gs[indexGroup - 1] = null; indexGroup = indexGroup - 1; } } /** *An abstract class that represents an algorithm. * * @author zhangtj * * @version 1.0 */ public static void main(String[] args) { } }
Editor is loading...