import java.util.Arrays; import java.io.Serializable; /** Maintains a list of Student objects, and provides machinery for working with the list. This has been revised from an earlier version to maintain Student objects in sorted order, and to provide a getStudents method, returning an array of Student objects. Also updateRecordGui has been added. It is like updateRecord except that StudentGui has taken the place of StudentMenu. @author Melvin Fitting @version April, 2005 */ public class StudentList implements Serializable { private Student[] students; private int count = 0; private static final int DEFAULT_SIZE = 100; /** @param size maximum size of Student list */ public StudentList(int size) { students = new Student[size]; } /** Makes Student list of default size, currently 100. */ public StudentList() { this(DEFAULT_SIZE); } /** Adds a student to Student list and returns true, unless list is full. If so, false is returned. @param first Student first name @param last Student last name @return true if student added, false otherwise. */ public boolean addStudent(String first, String last) { if(students.length == count) return false; Student s = new Student(first, last); students[count] = s; count++; Arrays.sort(students, 0, count); return true; } /** Checks to see if a student is already in the list. @param first Student first name @param last Student last name @return true if student in list, false otherwise */ public boolean inList(String first, String last) { if (findStudent(first, last) < 0) return false; else return true; } /** Displays a StudentGui for updating a Student record, provided student is in the list. No action taken otherwise. @param first Student first name @param last Student last name */ public void updateRecord(String first, String last) { int index = findStudent(first, last); if (index >= 0) { StudentGui m = new StudentGui(students[index]); m.setVisible(true); } } /** Returns a list of Student objects of exactly the right size. @return array of Student */ public Student[] getStudents() { Student[] temp = new Student[count]; for(int n = 0; n < count; n++) temp[n] = students[n]; return temp; } private int findStudent(String first, String last) { for(int n = 0; n < count; n++) if(first.equalsIgnoreCase(students[n].getFirst()) && last.equalsIgnoreCase(students[n].getLast())) return n; return -1; } }