Skip to content

Commit

Permalink
Merge pull request #88 from ji-just-ji/Json-Storage-configurations
Browse files Browse the repository at this point in the history
Json storage configurations
  • Loading branch information
rocketninja7 authored Oct 12, 2023
2 parents 77812fd + 079feb1 commit ae1045d
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void setModule(Module target, Module editedModule) {
//=========== Filtered Person List Accessors =============================================================

/**
* Returns an unmodifiable view of the list of {@code Person} backed by the internal list of
* Returns an unmodifiable view of the list of {@code Module} backed by the internal list of
* {@code versionedAddressBook}
*/
@Override
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/seedu/address/model/module/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.util.List;
import java.util.Set;

/**
* Represents a Module in the system.
Expand All @@ -15,17 +16,16 @@ public class Module {

// Data fields
private final Description description;
private final List<Lecturer> lecturers;
private final Set<Lecturer> lecturers;
private final Year yearTaken;
private final Semester semesterTaken;
private final Grade grade;

/**
* Every field must be present and not null.
*/
public Module(
ModuleName name, ModuleCode moduleCode, Description description, List<Lecturer> lecturers,
Year yearTaken, Semester semesterTaken, Grade grade) {
public Module(ModuleCode moduleCode, Year yearTaken, Semester semesterTaken, Grade grade, ModuleName name,
Description description, Set<Lecturer> lecturers) {
requireAllNonNull(name, moduleCode, description, lecturers, yearTaken, semesterTaken, grade);
this.moduleName = name;
this.moduleCode = moduleCode;
Expand Down Expand Up @@ -60,7 +60,7 @@ public Description getDescription() {
return description;
}

public List<Lecturer> getLecturers() {
public Set<Lecturer> getLecturers() {
return lecturers;
}

Expand Down
47 changes: 47 additions & 0 deletions src/main/java/seedu/address/storage/JsonAdaptedLecturer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package seedu.address.storage;


import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.module.Lecturer;

/**
* Jackson-friendly version of {@link Lecturer}.
*/
public class JsonAdaptedLecturer {
private final String lecturerName;

/**
* Constructs a {@code JsonAdaptedLecturer} with the given {@code lecturerName}.
*/
@JsonCreator
public JsonAdaptedLecturer(String lecturerName) {
this.lecturerName = lecturerName;
}

/**
* Converts a given {@code Lecturer} into this class for Jackson use.
*/
public JsonAdaptedLecturer(Lecturer source) {
lecturerName = source.lecturerName;
}

@JsonValue
public String getLecturerName() {
return lecturerName;
}

/**
* Converts this Jackson-friendly adapted tag object into the model's {@code Tag} object.
*
* @throws IllegalValueException if there were any data constraints violated in the adapted tag.
*/
public Lecturer toModelType() throws IllegalValueException {
if (!Lecturer.isValidLecturer(lecturerName)) {
throw new IllegalValueException(Lecturer.MESSAGE_CONSTRAINTS);
}
return new Lecturer(lecturerName);
}

}
131 changes: 131 additions & 0 deletions src/main/java/seedu/address/storage/JsonAdaptedModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package seedu.address.storage;


import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.module.*;
import seedu.address.model.module.Module;
import seedu.address.model.person.*;
import seedu.address.model.tag.Tag;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Jackson-friendly version of {@link Module}.
*/
public class JsonAdaptedModule {
public static final String MISSING_FIELD_MESSAGE_FORMAT = "Module's %s field is missing!";

private final String code;
private final String year;
private final String sem;
private final String grade;
private final String name;
private final String description;
private final List<JsonAdaptedLecturer> lecturers = new ArrayList<>();

/**
* Constructs a {@code JsonAdaptedModule} with the given module details.
*/

@JsonCreator
public JsonAdaptedModule(@JsonProperty("code") String code, @JsonProperty("year") String year,
@JsonProperty("sem") String sem, @JsonProperty("grade") String grade,
@JsonProperty("name") String name,
@JsonProperty("description") String description,
@JsonProperty("lecturers") List<JsonAdaptedLecturer> lecturers) {
this.code = code;
this.year = year;
this.sem = sem;
this.grade = grade;
this.name = name;
this.description = description;
if (lecturers != null) {
this.lecturers.addAll(lecturers);
}
}
/**
* Converts a given {@code Module} into this class for Jackson use.
*/
public JsonAdaptedModule(Module source) {
code = source.getModuleCode().toString();
year = source.getYearTaken().toString();
sem = source.getSemesterTaken().toString();
grade = source.getGrade().toString();
name = source.getName().toString();
description = source.getDescription().toString();
lecturers.addAll(source.getLecturers().stream()
.map(JsonAdaptedLecturer::new)
.collect(Collectors.toList()));
}

/**
* Converts this Jackson-friendly adapted person object into the model's {@code Person} object.
*
* @throws IllegalValueException if there were any data constraints violated in the adapted person.
*/
public Module toModelType() throws IllegalValueException {
final List<Lecturer> moduleLecturers = new ArrayList<>();
for (JsonAdaptedLecturer lecturer : lecturers) {
moduleLecturers.add(lecturer.toModelType());
}

if (code == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, ModuleCode.class.getSimpleName()));
}
if (!ModuleCode.isValidModuleCode(code)) {
throw new IllegalValueException(ModuleCode.MESSAGE_CONSTRAINTS);
}
final ModuleCode modelCode = new ModuleCode(code);


if (year == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Year.class.getSimpleName()));
}
if (!Year.isValidYear(year)) {
throw new IllegalValueException(Year.MESSAGE_CONSTRAINTS);
}
final Year modelYear = new Year(year);

if (sem == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Semester.class.getSimpleName()));
}
if (!Semester.isValidSemester(sem)) {
throw new IllegalValueException(Semester.MESSAGE_CONSTRAINTS);
}
final Semester modelSem = new Semester(sem);

if (grade == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Grade.class.getSimpleName()));
}
if (!Grade.isValidGrade(grade)) {
throw new IllegalValueException(Grade.MESSAGE_CONSTRAINTS);
}
final Grade modelGrade = new Grade(grade);

if(name == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, ModuleName.class.getSimpleName()));
}
if (!ModuleName.isValidName(name)) {
throw new IllegalValueException(ModuleName.MESSAGE_CONSTRAINTS);
}
final ModuleName modelName = new ModuleName(name);

if (description == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Description.class.getSimpleName()));
}
if(!Description.isValidDescription(description)) {
throw new IllegalValueException(Description.MESSAGE_CONSTRAINTS);
}
final Description modelDescription = new Description(description);

final Set<Lecturer> modelLecturer = new HashSet<>(moduleLecturers);
return new Module(modelCode, modelYear, modelSem, modelGrade, modelName, modelDescription, modelLecturer);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.AddressBook;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.module.Module;
import seedu.address.model.person.Person;

/**
Expand All @@ -19,16 +20,17 @@
@JsonRootName(value = "addressbook")
class JsonSerializableAddressBook {

public static final String MESSAGE_DUPLICATE_PERSON = "Persons list contains duplicate person(s).";
public static final String MESSAGE_DUPLICATE_MODULE = "Module list contains duplicate module(s).";

private final List<JsonAdaptedPerson> persons = new ArrayList<>();
private final List<JsonAdaptedModule> modules = new ArrayList<>();

/**
* Constructs a {@code JsonSerializableAddressBook} with the given persons.
*/
@JsonCreator
public JsonSerializableAddressBook(@JsonProperty("persons") List<JsonAdaptedPerson> persons) {
this.persons.addAll(persons);
public JsonSerializableAddressBook(@JsonProperty("module") List<JsonAdaptedModule> modules) {
this.modules.addAll(modules);
}

/**
Expand All @@ -37,7 +39,7 @@ public JsonSerializableAddressBook(@JsonProperty("persons") List<JsonAdaptedPers
* @param source future changes to this will not affect the created {@code JsonSerializableAddressBook}.
*/
public JsonSerializableAddressBook(ReadOnlyAddressBook source) {
persons.addAll(source.getPersonList().stream().map(JsonAdaptedPerson::new).collect(Collectors.toList()));
modules.addAll(source.getModuleList().stream().map(JsonAdaptedModule::new).collect(Collectors.toList()));
}

/**
Expand All @@ -47,12 +49,12 @@ public JsonSerializableAddressBook(ReadOnlyAddressBook source) {
*/
public AddressBook toModelType() throws IllegalValueException {
AddressBook addressBook = new AddressBook();
for (JsonAdaptedPerson jsonAdaptedPerson : persons) {
Person person = jsonAdaptedPerson.toModelType();
if (addressBook.hasPerson(person)) {
throw new IllegalValueException(MESSAGE_DUPLICATE_PERSON);
for (JsonAdaptedModule jsonAdaptedModule : modules) {
Module module = jsonAdaptedModule.toModelType();
if (addressBook.hasModule(module)) {
throw new IllegalValueException(MESSAGE_DUPLICATE_MODULE);
}
addressBook.addPerson(person);
addressBook.addModule(module);
}
return addressBook;
}
Expand Down

0 comments on commit ae1045d

Please sign in to comment.