Skip to content

Commit c3c006c

Browse files
author
bryans17
committed
Improve SLAP
1 parent c41f18b commit c3c006c

File tree

1 file changed

+51
-15
lines changed

1 file changed

+51
-15
lines changed

src/main/java/seedu/address/storage/JsonAdaptedStudent.java

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import java.util.HashSet;
55
import java.util.List;
66
import java.util.Set;
7+
import java.util.logging.Logger;
78
import java.util.stream.Collectors;
89

910
import com.fasterxml.jackson.annotation.JsonCreator;
1011
import com.fasterxml.jackson.annotation.JsonProperty;
1112

13+
import seedu.address.commons.core.LogsCenter;
1214
import seedu.address.commons.exceptions.IllegalValueException;
1315
import seedu.address.model.lab.Lab;
1416
import seedu.address.model.student.Email;
@@ -25,6 +27,7 @@
2527
class JsonAdaptedStudent {
2628

2729
public static final String MISSING_FIELD_MESSAGE_FORMAT = "Person's %s field is missing!";
30+
private static final Logger logger = LogsCenter.getLogger(JsonAdaptedStudent.class);
2831

2932
private final String name;
3033
private final String email;
@@ -89,68 +92,101 @@ public JsonAdaptedStudent(Student source) {
8992
* @throws IllegalValueException if there were any data constraints violated in the adapted student.
9093
*/
9194
public Student toModelType() throws IllegalValueException {
92-
final List<Tag> personTags = new ArrayList<>();
93-
for (JsonAdaptedTag tag : tagged) {
94-
personTags.add(tag.toModelType());
95-
}
96-
final List<Lab> personLabs = new ArrayList<>();
95+
final List<Tag> personTags = deserializeTags();
96+
97+
final List<Lab> personLabs = deserializeLabs();
98+
99+
final Name modelName = deserializeName();
100+
101+
final Email modelEmail = deserializeEmail();
102+
103+
final GithubUsername modelUsername = deserializeGithub();
104+
105+
final Telegram modelTelegram = deserializeTelegram();
106+
107+
final StudentId modelId = deserializeStudentId();
108+
109+
final Set<Tag> modelTags = new HashSet<>(personTags);
110+
111+
Student s = new Student(modelName, modelEmail, modelTags, modelUsername, modelTelegram, modelId);
112+
s.getLabs().setLabs(personLabs);
113+
114+
return s;
115+
}
97116

117+
private List<Lab> deserializeLabs() throws IllegalValueException {
118+
List<Lab> personLabs = new ArrayList<>();
98119
for (int i = 0; i < labNumbers.size(); i++) {
99120
// Incase of wrong lab status format we will just add the lab with LabStatus.UNSUBMITTED
100121
try {
101122
personLabs.add(labNumbers.get(i).toModelType().of(labStatuses.get(i).getLabStatus()));
102123
} catch (IllegalArgumentException e) {
124+
logger.info("Illegal lab status found when converting labs " + e);
103125
personLabs.add(labNumbers.get(i).toModelType());
104126
}
105127
}
128+
return personLabs;
129+
}
130+
131+
private List<Tag> deserializeTags() throws IllegalValueException {
132+
List<Tag> personTags = new ArrayList<>();
133+
for (JsonAdaptedTag tag : tagged) {
134+
personTags.add(tag.toModelType());
135+
}
136+
return personTags;
137+
}
106138

139+
private Name deserializeName() throws IllegalValueException {
107140
if (name == null) {
108141
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Name.class.getSimpleName()));
109142
}
110143
if (!Name.isValidName(name)) {
111144
throw new IllegalValueException(Name.MESSAGE_CONSTRAINTS);
112145
}
113-
final Name modelName = new Name(name);
146+
return new Name(name);
147+
}
114148

149+
private Email deserializeEmail() throws IllegalValueException {
115150
if (email == null) {
116151
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Email.class.getSimpleName()));
117152
}
118153
if (!Email.isValidEmail(email)) {
119154
throw new IllegalValueException(Email.MESSAGE_CONSTRAINTS);
120155
}
121-
final Email modelEmail = new Email(email);
156+
return new Email(email);
157+
}
122158

159+
private GithubUsername deserializeGithub() throws IllegalValueException {
123160
if (githubUsername == null) {
124161
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT,
125162
GithubUsername.class.getSimpleName()));
126163
}
127164
if (!GithubUsername.isValidGithubUsername(githubUsername)) {
128165
throw new IllegalValueException(GithubUsername.MESSAGE_CONSTRAINTS);
129166
}
130-
final GithubUsername modelUsername = new GithubUsername(githubUsername);
167+
return new GithubUsername(githubUsername);
168+
}
131169

170+
private Telegram deserializeTelegram() throws IllegalValueException {
132171
if (telegram == null) {
133172
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT,
134173
Telegram.class.getSimpleName()));
135174
}
136175
if (!Telegram.isValidTelegram(telegram)) {
137176
throw new IllegalValueException(Telegram.MESSAGE_CONSTRAINTS);
138177
}
139-
final Telegram modelTelegram = new Telegram(telegram);
178+
return new Telegram(telegram);
179+
}
140180

181+
private StudentId deserializeStudentId() throws IllegalValueException {
141182
if (studentId == null) {
142183
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT,
143184
StudentId.class.getSimpleName()));
144185
}
145186
if (!StudentId.isValidStudentId(studentId)) {
146187
throw new IllegalValueException(StudentId.MESSAGE_CONSTRAINTS);
147188
}
148-
final StudentId modelId = new StudentId(studentId);
149-
150-
final Set<Tag> modelTags = new HashSet<>(personTags);
151-
Student s = new Student(modelName, modelEmail, modelTags, modelUsername, modelTelegram, modelId);
152-
s.getLabs().setLabs(personLabs);
153-
return s;
189+
return new StudentId(studentId);
154190
}
155191

156192
}

0 commit comments

Comments
 (0)