Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class StudentManagement {
- // TODO: khai bao thuoc tinh students la array chua cac doi tuong thuoc lop Student (max. 100)
- public Student[] students = new Student[100];
- private static int size = 0;
- public static boolean sameGroup(Student s1, Student s2) {
- return (s1.getGroup().equals(s2.getGroup()));
- }
- public void addStudent(Student newStudent){
- students[size] = newStudent;
- size++;
- }
- public String studentsByGroup() {
- String ans = "";
- String[] gr = new String[100];
- int index = 0;
- int k = 0;
- int grsize = 1;
- gr[0] = students[0].getGroup();
- //tao mang chua group ko trung nhau
- for(int i=1; i<size; i++){
- boolean check = true;
- for(int j=0; j<grsize; j++){
- if(students[i].getGroup().equals(gr[j])) {
- check = false;
- break;
- }
- }
- if(check){
- gr[grsize] = students[i].getGroup();
- grsize++;
- }
- }
- for(int i=0; i<grsize; i++){
- ans += gr[i] + "\n";
- for(int j =0; j<size; j++){
- if(students[j].getGroup().equals(gr[i])) ans += students[j].getInfo() + "\n";
- }
- }
- return ans;
- }
- public void removeStudent(String id) {
- for(int i=0; i< size; i++){
- if(students[i].getId().equals(id)){
- for(int j=i; i<size; i++){
- students[j] = students[j+1];
- }
- break;
- }
- }
- size --;
- /*
- Student[] newarray = new Student[99];
- int k = 0;
- for(int i=0; i<students.length; i++){
- if(students[i].getId().equals(id)) continue;
- newarray[k++] = students[i];
- }
- students = newarray;
- */
- }
- public static void main(String[] args) {
- Student a = new Student("a", "12", "a@gmail.com");
- Student b = new Student("b", "32", "b@gmail.com");
- Student c = new Student();
- c.setName("c");
- c.setId("1111");
- c.setGroup("k64ca");
- c.setEmail("c@gmail.com");
- StudentManagement list = new StudentManagement();
- list.addStudent(a);
- list.addStudent(b);
- list.addStudent(c);
- System.out.println(list.studentsByGroup());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement