Disclaimer: I just learnt how to do this yesterday, and I am desperate
I have 3 classes (Student
, ClassSchedule
, ClassEnrolment
)
public class ClassEnrolment {
ClassSchedule cs;
Student stud;
public ClassEnrolment(ClassSchedule cs, Student stud) {
super();
this.cs = cs;
this.stud = stud;
}
}
-----
public class Student{
private String name;
private String matricNo;
ClassEnrolment classroom;
ClassSchedule schedule;
}
-----
public class ClassSchedule {
String classType;
int index;
int group;
String day;
String time;
String venue;
String remark;
String courseCode;
}
I am trying to read/write a text file (database). I am having issues with 3 lines inside "/////////////////"
.
I am aware that the attributes declared in ClassEnrolment
are not int nor string. How should I do this? How do I bring ClassSchedule
and Student
as part of StringTokenizer
?
The textfile stores index from ClassSchedule
and matricNo from Student
. I have a feeling I am doing this wrongly.
public static ArrayList readEnrolment(String filename) throws IOException {
ArrayList stringArray = (ArrayList) read(filename);
ArrayList alr = new ArrayList();
for (int i = 0; i < stringArray.size(); i++) {
String st = (String) stringArray.get(i);
StringTokenizer star = new StringTokenizer(st, SEPARATOR);
///////////////////////////////////////////////////
int cs = Integer.parseInt(star.nextToken().trim());
String stud = star.nextToken().trim();
ClassEnrolment enrolment = new ClassEnrolment(cs, stud);
//////////////////////////////////////////////////
alr.add(enrolment);
}
return alr;
}
public static void saveEnrolment(String filename, List al) throws IOException {
List alw = new ArrayList();
for (int i = 0; i < al.size(); i++) {
ClassEnrolment enrolment = (ClassEnrolment) al.get(i);
StringBuilder st = new StringBuilder();
st.append(enrolment.getCs2());
st.append(SEPARATOR);
st.append(enrolment.getStud2());
alw.add(st.toString());
}
write(filename, alw);
}