Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2122S1#65 from okyntary/improve-storage
Browse files Browse the repository at this point in the history
Implement storage to store personArrayList in Cca
  • Loading branch information
jovyntls authored Oct 17, 2021
2 parents c85fc82 + cc53da8 commit f788828
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/main/java/seedu/address/model/cca/Cca.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ public Cca(CcaName ccaName) {
/**
* Every field must be present and not null.
*/
public Cca(CcaName ccaName, int cid) {
public Cca(CcaName ccaName, Set<Person> personArrayList) {
requireAllNonNull(ccaName);
this.ccaName = ccaName;
this.personArrayList = new HashSet<>();
this.cid = new Cid(String.valueOf(cid));
this.personArrayList = personArrayList;
}

/**
Expand All @@ -42,6 +41,14 @@ public CcaName getName() {
return ccaName;
}

/**
* Returns the personArrayList of this CCA.
* @return the personArrayList of this CCA
*/
public Set<Person> getPersonArrayList() {
return personArrayList;
}

/**
* Returns the cid of this CCA.
* @return the cid of this CCA
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/seedu/address/storage/JsonAdaptedCca.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package seedu.address.storage;

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.cca.Cca;
import seedu.address.model.cca.CcaName;
import seedu.address.model.person.Person;

/**
* Jackson-friendly version of {@link Cca}.
Expand All @@ -14,20 +21,28 @@ public class JsonAdaptedCca {
public static final String MISSING_FIELD_MESSAGE_FORMAT = "Cca's %s field is missing!";

private final String name;
private final Set<JsonAdaptedPerson> personArrayList = new HashSet<>();

/**
* Constructs a {@code JsonAdaptedCca} with the given cca details.
*/
@JsonCreator
public JsonAdaptedCca(@JsonProperty("name") String name) {
public JsonAdaptedCca(@JsonProperty("name") String name,
@JsonProperty("members")Set<JsonAdaptedPerson> personArrayList) {
this.name = name;
if (personArrayList != null) {
this.personArrayList.addAll(personArrayList);
}
}

/**
* Converts a given {@code Cca} into this class for Jackson use.
*/
public JsonAdaptedCca(Cca source) {
name = source.getName().fullName;
personArrayList.addAll(source.getPersonArrayList().stream()
.map(JsonAdaptedPerson::new)
.collect(Collectors.toSet()));
}

/**
Expand All @@ -36,15 +51,21 @@ public JsonAdaptedCca(Cca source) {
* @throws IllegalValueException if there were any data constraints violated in the adapted person.
*/
public Cca toModelType() throws IllegalValueException {
final List<Person> personList = new ArrayList<>();
for (JsonAdaptedPerson person : personArrayList) {
personList.add(person.toModelType());
}

if (name == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, CcaName.class.getSimpleName()));
}
if (!CcaName.isValidName(name)) {
throw new IllegalValueException(CcaName.MESSAGE_CONSTRAINTS);
}
final CcaName modelName = new CcaName(name);
final Set<Person> personArrayList = new HashSet<>(personList);

return new Cca(modelName);
return new Cca(modelName, personArrayList);
}

}

0 comments on commit f788828

Please sign in to comment.