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 ]
I think the approach you should be taking is something akin to this :
Now if you have a list of students say :
Here you can go through the list and add subjects to any or all of them.
Edit : added some getters and setters that makes it clearer how you access object info.
Edit 2 to address your updated post :
There are a number of issues with your code. Let’s go through them from the most high-level stuff to low (not the same as importance mind you)
1) Class names
A class should not be called “Main”. It is unclear what the class is. Instead, call it what it is : a student-subject-registry system.
class StudentRegistershould be just fine.2) Class contents
You included the class
Subjectas an inner class inStudent. This does not make sense, as a subject is not a part of a student (in literal terms in real life). This is not the worst part of it though; havingSubjectas an inner class will make you create loads of identical subjects for each and every student. This is completely redundant.Now you also included a list of all students in every Student object. Is this intentional? If so, it seems a bit misguided. Student
Adoes not need to, and probably will never know about all other students. Not to mention, everytime you add a new student N, you’ll need to addNto all student objects that currently exist. That is redundant as well.A proposed solution
Let’s think about what you know as a student. You know :
Note that you as a student have no control over what the course description and ID is
All those private variables are private because those are things that you as a student disclose on request of someone. It is akin to someone asking you “What’s your name?” or “What’s your age?” etc. This is what you accomplish by implementing getters like
getName(),getAge().Now let’s consider the part in italic. When someone asks you what courses you are taking, you simply refer to the list of courses that are common for you and other students that are enrolled at the universities. You do not create your own courses that nobody else knows about. Therefore, a Subject should not be an inner class in your Student class. It is instead a referance (i.e a pointer) to a course object that is offered by the university (continue reading for further explanation)
Now consider a course object. That course object does not need to know about :
Any given course object needs to know :
Finally, in your
StudentRegistryclass, you should have two lists now :– a list of students enrolled at the university :
List <Student> all_students;– a list of all subjects/courses offered : `List all_subjects;
Remember that each students have methods for adding subjects? You simply refer to the course objects that exist in the studentRegistry and ask each student to add those to their subject list. No new subject objects will be created. Any and all subjects in every student’s study plan will refer to objects in the
all_subjectslist contained in the StudentRegistry class