I’m trying to finish up a program (yes, homework) that is a simple Student Management System.
The student’s names and grades (homework and exams) are parsed from an XML document, then the names are stored in a JList and grades in an ArrayList.
I’m trying to figure out how to implement a Query button so that the user can enter the student’s name and get the student’s name and grades listed in a Text Area within the GUI. Here is the method I’ve coded for the query so far:
void queryStudent(){
if (!students.isEmpty()) {
queryResultTextArea.setText("Student's Name: " + students.get(0).getName() + newline);
for(int i=0; i<6; i++){
queryResultTextArea.append("Homework " + (i + 1) + ": " + String.valueOf(students.get(0).getRecordList().get(i).getResult()) + newline);
}
for(int j=0; j<3; j++){
queryResultTextArea.append("Exam " + (j + 1) + ": " + String.valueOf(students.get(0).getRecordList().get(j+6).getResult()) + newline);
}
queryResultTextArea.append("Final Grade: " + String.valueOf(students.get(1).getFinalGrade()));
}else {
queryResultTextArea.setText("Please load data first!");
}
}
So as you can see, I’m not sure how to go about coding the method so that the student queried for will be associated with the grades and returned in the Text Area. I’m not really even sure I’m communicating this correctly.
I would imagine that you have a Student class that holds a Student’s name, grade and perhaps identification code, and that your JList’s model holds Student objects and has a custom cell renderer to display each Student by his name. Perhaps the best way to search by name would be to put this same collection of Students into a
Map<String, Student>such as aHashMap<String, Student>so that you could find the Student using his/her name as key. This would require that there are no duplicate names though.