In: Computer Science
I need to deep clone these 4 classes, using the cloneable interface. This is my code so far. Is it correct or do I need to make any changes?
class Record implements Cloneable {
private String CNAME;
private ArrayList<Subject> eCores;
private Major eMajor;
private ArrayList<Subject> eElectives;
private int totalCredit;
private Status status;
private ArrayList<Student> students;
@Override
protected Record clone() throws CloneNotSupportedException {
Record record = (Record) super.clone();
record.eMajor = (Major) eMajor.clone();
eCores = new ArrayList<>();
for (Subject s : eCores) {
Subject s1 = (Subject) s.clone();
record.eCores.add(s1);
}
eElectives = new ArrayList<>();
for (Subject ss : eElectives) {
Subject ss1 = (Subject) ss.clone();
record.eElectives.add(ss1);
}
return record;
}
public class Subject implements Cloneable {
private String subjectName;
private String subjectCode;
private int subjectCreditPoint;
private ArrayList<Student> studentList;
@Override
protected Subject clone() throws CloneNotSupportedException {
return (Subject) super.clone();
}
abstract class Student implements Cloneable, Enrollment {
private String studentName;
private String studentDOB;
private String studentSex;//Student Gender
private int studentNumber;
private ArrayList<Record> record;
@Override
protected Student clone() throws CloneNotSupportedException {
Student student = (Student) super.clone();
record = new ArrayList<>();
for (Record r : record) {
Record r1 = (Record) r.clone();
student.record.add(r1);
}
return student;
}
class Major implements Cloneable {
private String mName;
private ArrayList<Subject> mCores;
@Override
protected Major clone() throws CloneNotSupportedException {
Major major = (Major) super.clone();
mCores = new ArrayList<>();
for (Subject s : mCores) {
Subject s1 = (Subject) s.clone();
major.mCores.add(s1);
}
return major;
}
/******************* Record.java *********************/
import java.util.ArrayList;
class Record implements Cloneable {
private String CNAME;
private ArrayList<Subject> eCores;
private Major eMajor;
private ArrayList<Subject> eElectives;
private int totalCredit;
private Status status;
private ArrayList<Student> students;
public Record() {
}
public Record(String cNAME,
ArrayList<Subject> eCores, Major eMajor,
ArrayList<Subject> eElectives, int totalCredit,
Status status,
ArrayList<Student> students) {
super();
CNAME = cNAME;
this.eCores = eCores;
this.eMajor = eMajor;
this.eElectives = eElectives;
this.totalCredit =
totalCredit;
this.status = status;
this.students = students;
}
@Override
public Record clone() {
Record record = null;
try {
record =
(Record) super.clone();
} catch (CloneNotSupportedException
cne) {
record = new
Record();
record.CNAME =
this.CNAME;
record.eCores =
new ArrayList<>();
for (Subject s :
this.eCores) {
Subject s1 = s.clone();
record.eCores.add(s1);
}
record.eMajor =
this.eMajor.clone();
record.eElectives = new ArrayList<>();
for (Subject ss
: this.eElectives) {
Subject ss1 = ss.clone();
record.eElectives.add(ss1);
}
record.status =
this.status;
record.students
= new ArrayList<>();
for (Student
stud : this.students) {
Student s = stud.clone();
record.students.add(s);
}
}
return record;
}
}
/********************* Subject.java ****************/
import java.util.ArrayList;
public class Subject implements Cloneable {
private String subjectName;
private String subjectCode;
private int subjectCreditPoint;
private ArrayList<Student> studentList;
@Override
public Subject clone() {
Subject subject = null;
try {
subject =
(Subject) super.clone();
} catch (CloneNotSupportedException
nse) {
subject.subjectName = this.subjectName;
subject.subjectCode = this.subjectCode;
subject.subjectCreditPoint = this.subjectCreditPoint;
subject.studentList = new ArrayList<>();
for (Student s :
this.studentList) {
Student stud = s.clone();
subject.studentList.add(stud);
}
}
return subject;
}
}
/******************* Student.java *******************/
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
public abstract class Student implements Cloneable {
private String studentName;
private String studentDOB;
private String studentSex;
private int studentNumber;
private ArrayList<Record> record;
public Student() {
}
public Student(String studentName, String
studentDOB, String studentSex, int studentNumber,
ArrayList<Record> record) {
super();
this.studentName =
studentName;
this.studentDOB = studentDOB;
this.studentSex = studentSex;
this.studentNumber =
studentNumber;
this.record = record;
}
@Override
public Student clone() {
Student student = null;
try {
student =
getClass().getDeclaredConstructor().newInstance();
} catch (InstantiationException |
IllegalAccessException | IllegalArgumentException |
SecurityException
| NoSuchMethodException |
InvocationTargetException e) {
e.printStackTrace();
}
student.studentName =
this.studentName;
student.studentDOB =
this.studentDOB;
student.studentSex =
this.studentSex;
student.studentNumber =
this.studentNumber;
student.record = new
ArrayList<>();
for (Record r : this.record)
{
Record r1 =
r.clone();
student.record.add(r1);
}
return student;
}
}
/*************** Major.java *******************/
import java.util.ArrayList;
public class Major implements Cloneable {
private String mName;
private ArrayList<Subject> mCores;
@Override
public Major clone() {
Major major = null;
try {
major = (Major)
super.clone();
} catch (CloneNotSupportedException
nse) {
major.mName =
this.mName;
mCores = new
ArrayList<>();
for (Subject s :
this.mCores) {
Subject s1 = s.clone();
major.mCores.add(s1);
}
}
return major;
}
}