0

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);
}
5
  • by creating an instance of your objects maybe new ClassSchedule(theSchedule) etc? Commented Mar 31, 2017 at 11:25
  • Maybe (if you can) you could use JSON to save/read back (see Gson for example) Commented Mar 31, 2017 at 11:27
  • Does the problem require you to use StringTokenizer? To me this looks like a poor approach to the problem. (Regardless, what you are doing is called serialization or marshalling - you need to come up with a way to represent your classes as text, and be able to convert from one to the other Commented Mar 31, 2017 at 11:51
  • Other misc advice: use generics! saveEnrolment should take a List<ClassEnrolment> for example. And return interfaces rather than concrete classes (so you can change the implementation later without breaking callers) - thus read should return a List<String> rather than ArrayList<String> or ArrayList. Commented Mar 31, 2017 at 11:53
  • I can't use JSON :( Thanks everybody! Commented Mar 31, 2017 at 15:07

1 Answer 1

2

Your problem here is an issue of Serialization. You could very well declare your classEnrolment as Serializable(do not forget the serialUUID) and let the classic java serialization process to write your objects as bytes in your file.

However, what you are trying here is to conceive a custom serializer for your class. I'd advise you to use generics in the likes of :

public interface Serializer<T> {

    byte[] write(T objectToSerialize);

    //to build a factory/service around it
    boolean canDeserialize(byte[] serializedObject);

    T read(byte[] serializedObject);
}

So that your methods read/writeEnrollment will simply be creating a Serializer<ClassEnrollment> and letting it do its job (which will probably create a Serializer<Student> and a Serializer<ClassSchedule>) .

For the matter of the Serialization process, I have to say that the StringTokenizer is a legacy class from JDK 1 whose use is discouraged by its own javadoc :

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

And if you want to Serialize objects of potentially different classes than just your ClassEnrollment, it would be nice to be able to distinguish the class used. There are many ways to do it, and as one commenter said, json serialization would fare very well.

Sign up to request clarification or add additional context in comments.

1 Comment

Understood :) Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.