11package database .bean .student ;
22
33import java .util .Arrays ;
4+ import java .util .List ;
5+ import java .util .stream .Collectors ;
46
57import database .bean .Bean ;
68import utils .ValidationType ;
79
810/**
911 * This object is used to seperate a {@code Student}'s data including his biodata, educational
10- * background etc from the actual {@code Student} object that stores the key details of the
11- * student. This object would be returned by StudentManager when the Student data is required
12+ * background etc from the actual {@code Student} object that stores the key details of a
13+ * {@code Student}. <br>
14+ * This object is composed of {@link Biodata}, an array of {@link EducationalBackgoround}, an array fo
15+ * {@link MeansOfDiscovery}, an array of {@link Phone} and an array of {@link ProfessionalExperience}.<br>
16+ * The set methods for the arra attruibutes of this object removes any null values from its argument
17+ * before it is set.
1218 * @author Chidiebere
1319 *
1420 */
@@ -33,12 +39,13 @@ public class StudentData implements Bean
3339 * @param experiences
3440 */
3541 public StudentData ( Biodata data , EducationalBackground [] edu ,
36- Phone [] phoneNumbers , ProfessionalExperience [] experiences )
42+ Phone [] phoneNumbers , ProfessionalExperience [] experiences , MeanOfDiscovery [] meansOfDis )
3743 {
3844 setBiodata (data );
3945 setEducation (edu );
4046 setPhoneNumbers (phoneNumbers );
4147 setExperiences (experiences );
48+ setMeansOfDiscovery (meansOfDis );
4249 }
4350
4451
@@ -55,29 +62,51 @@ public boolean isValid(ValidationType type)
5562 /*This method validates this object by ensuring that all its instances have
5663 * the same student id attribute and that they are all valid
5764 */
58-
5965 final String studentId = getBiodata ().getStudentId ();
66+
67+ // This ensures that all the attributes of this object have the same studentId and that the length of the
68+ // array attributes are valid
69+
70+ boolean allSame = areArraysValid () && Arrays .stream ( getEducation ()).allMatch (edu -> edu .getStudentId ().equals (studentId ) ) &&
71+ Arrays .stream (getExperiences ()).allMatch (exp -> exp .getStudentId ().equals (studentId )) &&
72+ Arrays .stream (getMeansOfDiscovery ()).allMatch (m -> m .getStudentId ().equals (studentId ) ) &&
73+ Arrays .stream (getPhoneNumbers ()).allMatch (p ->p .getStudentId ().equals (studentId ) );
74+
75+
6076 if ( studentId == null || studentId .length () < 1 ) return false ;
6177 switch (type )
6278 {
6379
6480 case NEW_BEAN :
65- return Arrays .stream (getEducation ()).allMatch (edu -> edu .equals (studentId ) && edu .isValid (ValidationType .NEW_BEAN )) &&
66- Arrays .stream (getExperiences ()).allMatch (exp -> exp .equals (studentId ) && exp .isValid (ValidationType .NEW_BEAN )) &&
67- Arrays .stream (getMeansOfDiscovery ()).allMatch (means -> means .equals (studentId ) && means .isValid (ValidationType .NEW_BEAN )) &&
68- Arrays .stream (getPhoneNumbers ()).allMatch (phone -> phone .equals (studentId ) && phone .isValid ( ValidationType .NEW_BEAN ));
69-
81+ boolean isvalMean = Arrays .stream (getMeansOfDiscovery ()).allMatch (mean -> mean .isValid (ValidationType .NEW_BEAN ) );
82+ boolean isvalEdu = Arrays .stream (getEducation ()).allMatch (edu ->edu .isValid (ValidationType .NEW_BEAN ) ) ;
83+ boolean isValExp = Arrays .stream (getExperiences ()).allMatch (exp ->exp .isValid (ValidationType .NEW_BEAN ) ) ;
84+ boolean phoneVal = Arrays .stream (getPhoneNumbers ()).allMatch (phone ->phone .isValid (ValidationType .NEW_BEAN ) );
85+
86+ return allSame && isvalEdu && isvalMean && isValExp && phoneVal ;
87+
7088 case EXISTING_BEAN :
71- return Arrays .stream (getEducation ()).allMatch (edu -> edu .equals ( studentId ) && edu . isValid (ValidationType .EXISTING_BEAN )) &&
72- Arrays .stream (getExperiences ()).allMatch (exp -> exp .equals ( studentId ) && exp . isValid (ValidationType .EXISTING_BEAN )) &&
73- Arrays .stream (getMeansOfDiscovery ()).allMatch (means -> means . equals ( studentId ) && means . isValid (ValidationType .EXISTING_BEAN )) &&
74- Arrays .stream (getPhoneNumbers ()).allMatch (phone -> phone .equals ( studentId ) && phone . isValid ( ValidationType .EXISTING_BEAN ));
75-
89+ return allSame && Arrays .stream (getEducation ()).allMatch (edu ->edu .isValid (ValidationType .EXISTING_BEAN ) ) &&
90+ Arrays .stream (getExperiences ()).allMatch (exp ->exp .isValid (ValidationType .EXISTING_BEAN ) ) &&
91+ Arrays .stream (getMeansOfDiscovery ()).allMatch (mean -> mean . isValid (ValidationType .EXISTING_BEAN ) ) &&
92+ Arrays .stream (getPhoneNumbers ()).allMatch (phone ->phone .isValid (ValidationType .EXISTING_BEAN ) );
93+
7694
7795 }
7896 return false ;
7997 }
8098
99+ /**
100+ * Checks that none of this object's attributes is {@code null}
101+ * @return true only if there are no {@code null } values
102+ */
103+ private boolean areArraysValid ()
104+ {
105+ return getEducation () != null && getExperiences () != null && getBiodata () != null &&
106+ getMeansOfDiscovery () != null && getPhoneNumbers () != null &&
107+ getEducation ().length > 0 && getMeansOfDiscovery ().length >0 && getPhoneNumbers ().length > 0 ;
108+ }
109+
81110
82111 public Biodata getBiodata ()
83112 {
@@ -99,7 +128,12 @@ public EducationalBackground[] getEducation()
99128
100129 public void setEducation (EducationalBackground [] education )
101130 {
102- this .education = education ;
131+ List <EducationalBackground > list =
132+ Arrays .stream (education )
133+ .filter (e -> e != null )
134+ .collect (Collectors .toList ());
135+
136+ this .education = list .toArray (new EducationalBackground [ list .size ()]);
103137 }
104138
105139
@@ -111,7 +145,11 @@ public MeanOfDiscovery[] getMeansOfDiscovery()
111145
112146 public void setMeansOfDiscovery (MeanOfDiscovery [] meansOfDiscovery )
113147 {
114- this .meansOfDiscovery = meansOfDiscovery ;
148+ List <MeanOfDiscovery > list =
149+ Arrays .stream (meansOfDiscovery )
150+ .filter (means -> means != null )
151+ .collect (Collectors .toList ());
152+ this .meansOfDiscovery = list .toArray (new MeanOfDiscovery [ list .size ()]);
115153 }
116154
117155
@@ -123,7 +161,11 @@ public Phone[] getPhoneNumbers()
123161
124162 public void setPhoneNumbers (Phone [] phoneNumbers )
125163 {
126- this .phoneNumbers = phoneNumbers ;
164+ List <Phone > list =
165+ Arrays .stream (phoneNumbers )
166+ .filter (means -> means != null )
167+ .collect (Collectors .toList ());
168+ this .phoneNumbers = list .toArray ( new Phone [ list .size ()] );
127169 }
128170
129171
@@ -132,10 +174,19 @@ public ProfessionalExperience[] getExperiences()
132174 return experiences ;
133175 }
134176
135-
177+ /**
178+ * This sets the sets the {@code ProfessionalExperience} stored in this object
179+ * to the argument supplied. It does this by first removing any {@code null} values
180+ * in its argument before setting the {@code ProfessionalExperience} array attribute
181+ * @param experiences the profesional experiences of the Student
182+ */
136183 public void setExperiences (ProfessionalExperience [] experiences )
137184 {
138- this .experiences = experiences ;
185+ List <ProfessionalExperience > list =
186+ Arrays .stream (experiences )
187+ .filter (exp -> exp != null )
188+ .collect (Collectors .toList ());
189+ this .experiences = list .toArray ( new ProfessionalExperience [ list .size ()] );
139190 }
140191
141192}
0 commit comments