I have created a ArrayList<Student> in a object StudentList list1 that is saving[Serialization] a student's information (name,id,age,gpa,etc) into the list1, so that the list1[0] = 1st student's info, then the list1[1] = 2nd student's info and so on.
Also a new ArrayList<Subject> in a object SubjectList list2 for all subject of a student at index 0 example list2[0]=(java,math,etc) [for first student] list2[1]=(c++,english,etc) [for second student] saved in a file.
I want to add the subject next to the student info:
list1 index[0]=1st Student info, index[1]=1st Student's Subjects.
index[2]=1st Student info,index[3]=1st Student's Subjects.
I am stuck with this simple problem.Help please.
package studentPanel;
public class Main {
public static void main(String[] args){
Student s = new Student(null, null, null, null);
s.stulist.add(new Student("Smith", "1", "M", "3"));
s.stulist.add(new Student("Jenny", "2", "F", "4"));
s.stulist.add(new Student("Roger", "3", "M", "2"));
System.out.println(""+s.stulist);
for(int i=0;i<s.stulist.size();i++){
Student search = s.stulist.get(i);
if(search.toString().contains("Jenny")){
System.out.println("Found"+i);
s.addSubject(s.new Subject("OOP","007"));
s.addSubjects(s.sublist);
System.out.println(""+s.stulist.get(i)+""+s.sublist);
}
else System.out.println("Not Found"+i);
}
System.out.println(""+s.stulist);
}
}
package studentPanel;
import java.util.*;
public class Student {
public String name, id, gender, cgpa;
ArrayList<Subject> sublist = new ArrayList<Subject>();
ArrayList<Student> stulist = new ArrayList<Student>();
public void addSubject(Subject new_subject) {
sublist.add(new_subject);
}
public void addSubjects(List<Subject> subjects_list) {
for (Subject s : subjects_list)
addSubject(s);
}
public Student(String name, String id, String gender, String cgpa) {
this.name = name;
this.id = id;
this.gender = gender;
this.cgpa = cgpa;
}
public String toString() {
return "Name: " + name + "\tID: " + id + "\tGender: " + gender + "\tCGPA: " +
cgpa+ "\n";
}
public class Subject {
public String cname,cid;
public Subject(String cname, String cid) {
this.cname = cname;
this.cid = cid;
}
public String toString() {
return "Course: " + cname + "\tCode: " + cid;
}
}
}
I just can't get it right, it was suppose to add the subject `s.addSubject(s.new Subject
("OOP","007"));` to the student Jenny.
Output Should be like:
[Name: Smith ID: 1 Gender: M CGPA: 3
, Name: Jenny ID: 2 Gender: F CGPA: 4
, Name: Roger ID: 3 Gender: M CGPA: 2 ]
Not Found0
Found1
[ Name: Jenny ID: 2 Gender: F CGPA: 4 ][Course: OOP,Code: 007]
Not Found2
[Name: Smith ID: 1 Gender: M CGPA: 3
, Name: Jenny ID: 2 Gender: F CGPA: 4 [Course: OOP,Code: 007]
, Name: Roger ID: 3 Gender: M CGPA: 2 ]