forked from nus-cs2103-AY2021S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nus-cs2103-AY2021S1#74 from nicholasyeo/branch-tim…
…etable-storage Allow fitNUS to save and load timetable slots
- Loading branch information
Showing
13 changed files
with
280 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
src/main/java/seedu/address/storage/JsonAdaptedSlot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package seedu.address.storage; | ||
|
||
import java.time.LocalTime; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import seedu.address.commons.exceptions.IllegalValueException; | ||
import seedu.address.model.person.Activity; | ||
import seedu.address.model.person.Day; | ||
import seedu.address.model.person.Duration; | ||
import seedu.address.model.person.Exercise; | ||
import seedu.address.model.person.Lesson; | ||
import seedu.address.model.person.Name; | ||
import seedu.address.model.person.Routine; | ||
import seedu.address.model.person.Slot; | ||
import seedu.address.model.tag.Tag; | ||
|
||
/** | ||
* Jackson-friendly version of {@link Slot}. | ||
*/ | ||
public class JsonAdaptedSlot { | ||
public static final String TYPE_ROUTINE = "routine"; | ||
public static final String TYPE_LESSON = "lesson"; | ||
|
||
public static final String MISSING_FIELD_MESSAGE_FORMAT = "Slot's %s field is missing!"; | ||
|
||
private final String activityName; | ||
private final String type; | ||
private final String day; | ||
private final String duration; | ||
private final List<JsonAdaptedTag> tagsForLesson = new ArrayList<>(); | ||
private final List<JsonAdaptedExercise> exercisesForRoutine = new ArrayList<>(); | ||
|
||
/** | ||
* Constructs a {@code JsonAdaptedSlot} with the given slot details. | ||
*/ | ||
@JsonCreator | ||
public JsonAdaptedSlot(@JsonProperty("activityName") String activityName, | ||
@JsonProperty("type") String type, | ||
@JsonProperty("day") String day, | ||
@JsonProperty("duration") String duration, | ||
@JsonProperty("tagsForLesson") List<JsonAdaptedTag> tags, | ||
@JsonProperty("exercisesForRoutine") List<JsonAdaptedExercise> exercises) { | ||
this.activityName = activityName; | ||
this.type = type; | ||
this.day = day; | ||
this.duration = duration; | ||
if (type.equals(TYPE_LESSON)) { | ||
if (tags != null) { | ||
tagsForLesson.addAll(tags); | ||
} | ||
} else { | ||
if (exercises != null) { | ||
exercisesForRoutine.addAll(exercises); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Converts a given {@code Slot} into this class for Jackson use. | ||
*/ | ||
public JsonAdaptedSlot(Slot source) { | ||
activityName = source.getActivity().getName().fullName; | ||
type = source.getActivity() instanceof Routine | ||
? TYPE_ROUTINE | ||
: TYPE_LESSON; | ||
day = source.getDay().toString(); | ||
duration = source.getDuration().toString(); | ||
if (type.equals(TYPE_LESSON)) { | ||
Lesson lesson = (Lesson) source.getActivity(); | ||
tagsForLesson.addAll(lesson.getTags() | ||
.stream() | ||
.map(JsonAdaptedTag::new) | ||
.collect(Collectors.toList())); | ||
} else { | ||
Routine routine = (Routine) source.getActivity(); | ||
exercisesForRoutine.addAll(routine.getExercises() | ||
.stream() | ||
.map(JsonAdaptedExercise::new) | ||
.collect(Collectors.toList())); | ||
} | ||
} | ||
|
||
/** | ||
* Converts this Jackson-friendly adapted slot object into the model's {@code Slot} object. | ||
* | ||
* @throws IllegalValueException if there were any data constraints violated in the adapted exercise. | ||
*/ | ||
public Slot toModelType() throws IllegalValueException { | ||
|
||
if (activityName == null) { | ||
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Name.class.getSimpleName())); | ||
} | ||
if (!Name.isValidName(activityName)) { | ||
throw new IllegalValueException(Name.MESSAGE_CONSTRAINTS); | ||
} | ||
if (Day.isUnknownDay(Day.getDayEnum(day))) { | ||
throw new IllegalValueException(Day.MESSAGE_CONSTRAINTS); | ||
} | ||
if (!Duration.isValidDuration(duration)) { | ||
throw new IllegalValueException(Duration.MESSAGE_CONSTRAINTS_FORMAT); | ||
} | ||
|
||
final Name modelName = new Name(activityName); | ||
|
||
final Activity modelActivity; | ||
if (type.equals(TYPE_LESSON)) { | ||
final List<Tag> lessonTags = new ArrayList<>(); | ||
for (JsonAdaptedTag tag : tagsForLesson) { | ||
lessonTags.add(tag.toModelType()); | ||
} | ||
final Set<Tag> modelTags = new HashSet<>(lessonTags); | ||
modelActivity = new Lesson(modelName, modelTags); | ||
} else { | ||
final List<Exercise> routineExercises = new ArrayList<>(); | ||
for (JsonAdaptedExercise exercise : exercisesForRoutine) { | ||
routineExercises.add(exercise.toModelType()); | ||
} | ||
final Set<Exercise> modelExercise = new HashSet<>(routineExercises); | ||
Routine modelRoutine = new Routine(modelName); | ||
for (Exercise exercise : modelExercise) { | ||
modelRoutine.addExercise(exercise); | ||
} | ||
modelActivity = modelRoutine; | ||
} | ||
|
||
final Day modelDay = Day.getDayEnum(day); | ||
|
||
final Duration modelDuration = parseDuration(duration); | ||
|
||
return new Slot(modelActivity, modelDay, modelDuration); | ||
} | ||
|
||
private Duration parseDuration(String toParse) throws IllegalValueException { | ||
String[] timeSplit = toParse.split("-"); | ||
|
||
int startHour = Integer.parseInt(timeSplit[0].substring(0, 2)); | ||
int startMinute = Integer.parseInt(timeSplit[0].substring(2, 4)); | ||
LocalTime startTime = LocalTime.of(startHour, startMinute); | ||
|
||
int endHour = Integer.parseInt(timeSplit[1].substring(0, 2)); | ||
int endMinute = Integer.parseInt(timeSplit[1].substring(2, 4)); | ||
LocalTime endTime = LocalTime.of(endHour, endMinute); | ||
|
||
if (!Duration.isValidDuration(startTime, endTime)) { | ||
throw new IllegalValueException(Duration.MESSAGE_CONSTRAINTS_ORDER); | ||
} | ||
|
||
return new Duration(startTime, endTime); | ||
} | ||
} |
Oops, something went wrong.