Skip to content

Commit

Permalink
Merge pull request AY2425S1-CS2103T-T12-4#118
Browse files Browse the repository at this point in the history
Add PreferredTime to Person and Update Storage
  • Loading branch information
JJtan2002 authored Oct 17, 2024
2 parents 1eca052 + 71af9ca commit 38da93a
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 52 deletions.
28 changes: 27 additions & 1 deletion src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.preferredtime.PreferredTime;
import seedu.address.model.tag.Tag;

/**
Expand Down Expand Up @@ -106,8 +107,11 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
Address updatedAddress = editPersonDescriptor.getAddress().orElse(personToEdit.getAddress());
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());
Map<String, Game> updatedGames = editPersonDescriptor.getGames().orElse(personToEdit.getGames());
Set<PreferredTime> updatedPreferredTimes =
editPersonDescriptor.getPreferredTimes().orElse(personToEdit.getPreferredTimes());

return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags, updatedGames);
return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags,
updatedGames, updatedPreferredTimes);
}

@Override
Expand Down Expand Up @@ -145,6 +149,7 @@ public static class EditPersonDescriptor {
private Address address;
private Set<Tag> tags;
private Map<String, Game> games;
private Set<PreferredTime> preferredTimes;


public EditPersonDescriptor() {}
Expand All @@ -160,6 +165,7 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) {
setAddress(toCopy.address);
setTags(toCopy.tags);
setGames(toCopy.games);
setPreferredTimes(toCopy.preferredTimes);
}

/**
Expand Down Expand Up @@ -234,6 +240,26 @@ public void setGames(Map<String, Game> games) {
public Optional<Map<String, Game>> getGames() {
return (games != null) ? Optional.of(Collections.unmodifiableMap(games)) : Optional.empty();
}

/**
* Sets {@code preferredTimes} to this object's {@code preferredTimes}.
* A defensive copy of {@code preferredTimes} is used internally.
*/
public void setPreferredTimes(Set<PreferredTime> preferredTimes) {
this.preferredTimes = (preferredTimes != null) ? new HashSet<>(preferredTimes) : null;
}

/**
* Returns an unmodifiable preferredTime set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code preferredTimes} is null.
*/
public Optional<Set<PreferredTime>> getPreferredTimes() {
return (preferredTimes != null)
? Optional.of(Collections.unmodifiableSet(preferredTimes))
: Optional.empty();
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_GAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PREFERREDTIME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import java.util.Map;
Expand All @@ -20,6 +21,7 @@
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.preferredtime.PreferredTime;
import seedu.address.model.tag.Tag;

/**
Expand All @@ -35,7 +37,7 @@ public class AddCommandParser implements Parser<AddCommand> {
public AddCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_TAG,
PREFIX_GAME);
PREFIX_GAME, PREFIX_PREFERREDTIME);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL)
|| !argMultimap.getPreamble().isEmpty()) {
Expand All @@ -49,11 +51,10 @@ public AddCommand parse(String args) throws ParseException {
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
Map<String, Game> gameList = ParserUtil.parseGames(argMultimap.getAllValues(PREFIX_GAME));
Set<PreferredTime> preferredTimeList =
ParserUtil.parsePreferredTimes(argMultimap.getAllValues(PREFIX_PREFERREDTIME));

// TODO: use get a Set<PreferredTime> with the help of ParserUtil
// TODO: update the Person construction

Person person = new Person(name, phone, email, address, tagList, gameList);
Person person = new Person(name, phone, email, address, tagList, gameList, preferredTimeList);

return new AddCommand(person);
}
Expand Down
28 changes: 27 additions & 1 deletion src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
import seedu.address.model.preferredtime.PreferredTime;
import seedu.address.model.tag.Tag;

/**
Expand Down Expand Up @@ -202,6 +203,31 @@ public static Map<String, Game> parseGames(Collection<String> games) throws Pars
}


// TODO: parsePreferredTime and parsePreferredTimes
/**
* Parses a {@code String preferredTime} into a {@code PreferredTime}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code preferredTime} is invalid.
*/
public static PreferredTime parsePreferredTime(String preferredTime) throws ParseException {
requireNonNull(preferredTime);
String trimmedPreferredTime = preferredTime.trim();
if (!PreferredTime.isValidPreferredTime(trimmedPreferredTime)) {
throw new ParseException(PreferredTime.MESSAGE_CONSTRAINTS);
}
return new PreferredTime(trimmedPreferredTime);
}

/**
* Parses {@code Collection<String> preferredTimes} into a {@code Set<PreferredTime>}.
*/
public static Set<PreferredTime> parsePreferredTimes(Collection<String> preferredTimes) throws ParseException {
requireNonNull(preferredTimes);
final Set<PreferredTime> preferredTimeSet = new HashSet<>();
for (String preferredTime : preferredTimes) {
preferredTimeSet.add(parsePreferredTime(preferredTime));
}
return preferredTimeSet;
}

}
26 changes: 19 additions & 7 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.game.Game;
import seedu.address.model.preferredtime.PreferredTime;
import seedu.address.model.tag.Tag;

/**
Expand All @@ -28,21 +29,21 @@ public class Person {
private final Address address;
private final Set<Tag> tags = new HashSet<>();
private final Map<String, Game> games = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

// TODO: update the preferredTime field
// TODO: update the constructors and some other method
private final Set<PreferredTime> preferredTimes = new HashSet<>();

/**
* Every field must be present and not null.
*/
public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tags, Map<String, Game> games) {
requireAllNonNull(name, phone, email, address, tags, games);
public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tags,
Map<String, Game> games, Set<PreferredTime> preferredTimes) {
requireAllNonNull(name, phone, email, address, tags, games, preferredTimes);
this.name = name;
this.phone = phone;
this.email = email;
this.address = address;
this.tags.addAll(tags);
this.games.putAll(games);
this.preferredTimes.addAll(preferredTimes);
}

public Name getName() {
Expand Down Expand Up @@ -76,6 +77,15 @@ public Set<Tag> getTags() {
public Map<String, Game> getGames() {
return games;
}

/**
* Returns an immutable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
*/
public Set<PreferredTime> getPreferredTimes() {
return Collections.unmodifiableSet(preferredTimes);
}

/**
* Returns true if both persons have the same name.
* This defines a weaker notion of equality between two persons.
Expand Down Expand Up @@ -110,14 +120,15 @@ public boolean equals(Object other) {
&& email.equals(otherPerson.email)
&& address.equals(otherPerson.address)
&& tags.equals(otherPerson.tags)
&& games.equals(otherPerson.games);
&& games.equals(otherPerson.games)
&& preferredTimes.equals(otherPerson.preferredTimes);

}

@Override
public int hashCode() {
// use this method for custom fields hashing instead of implementing your own
return Objects.hash(name, phone, email, address, tags, games);
return Objects.hash(name, phone, email, address, tags, games, preferredTimes);
}

@Override
Expand All @@ -129,6 +140,7 @@ public String toString() {
.add("address", address)
.add("tags", tags)
.add("games", games)
.add("preferred times", preferredTimes)
.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ public class PreferredTime {
"PreferredTime should consists of Day and Time in the format 'Day HHmm'.\n"
+ "There should be exactly one space in between";
public static final String VALIDATION_REGEX = "(\\p{L}+)\\s(\\d{4})$";
private static final Pattern VALIDATED_PATTERN = Pattern.compile(VALIDATION_REGEX);
public static final Pattern VALIDATED_PATTERN = Pattern.compile(VALIDATION_REGEX);

public final String preferredTime;
public final Day day;
public final Time time;


// TODO: constructor
/**
* Constructs a {@code PreferredTime}.
*
Expand Down
29 changes: 22 additions & 7 deletions src/main/java/seedu/address/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.preferredtime.PreferredTime;
import seedu.address.model.tag.Tag;


Expand All @@ -32,23 +33,28 @@ public static Person[] getSamplePersons() {
return new Person[] {
new Person(new Name("Alex Yeoh"), new Phone("87438807"), new Email("[email protected]"),
new Address("Blk 30 Geylang Street 29, #06-40"),
getTagSet("friends"),
sample),
getTagSet("friends"), sample,
getPreferredTimeSet("Monday 2100")),
new Person(new Name("Bernice Yu"), new Phone("99272758"), new Email("[email protected]"),
new Address("Blk 30 Lorong 3 Serangoon Gardens, #07-18"),
getTagSet("colleagues", "friends"), getGameMap("LoL")),
getTagSet("colleagues", "friends"), getGameMap("LoL"),
getPreferredTimeSet("Monday 2030")),
new Person(new Name("Charlotte Oliveiro"), new Phone("93210283"), new Email("[email protected]"),
new Address("Blk 11 Ang Mo Kio Street 74, #11-04"),
getTagSet("neighbours"), getGameMap("LoL")),
getTagSet("neighbours"), getGameMap("LoL"),
getPreferredTimeSet("Saturday 1900")),
new Person(new Name("David Li"), new Phone("91031282"), new Email("[email protected]"),
new Address("Blk 436 Serangoon Gardens Street 26, #16-43"),
getTagSet("family"), getGameMap("LoL")),
getTagSet("family"), getGameMap("LoL"),
getPreferredTimeSet("Tuesday 2100", "Thursday 1900")),
new Person(new Name("Irfan Ibrahim"), new Phone("92492021"), new Email("[email protected]"),
new Address("Blk 47 Tampines Street 20, #17-35"),
getTagSet("classmates"), getGameMap("LoL")),
getTagSet("classmates"), getGameMap("LoL"),
getPreferredTimeSet("Sunday 1800")),
new Person(new Name("Roy Balakrishnan"), new Phone("92624417"), new Email("[email protected]"),
new Address("Blk 45 Aljunied Street 85, #11-31"),
getTagSet("colleagues"), getGameMap("LoL"))
getTagSet("colleagues"), getGameMap("LoL"),
getPreferredTimeSet("Friday 2100"))
};
}

Expand Down Expand Up @@ -80,4 +86,13 @@ public static Map<String, Game> getGameMap(String... strings) {
return gameMap;
}

/**
* Returns a preferredTime set containing the list of strings given.
*/
public static Set<PreferredTime> getPreferredTimeSet(String... strings) {
return Arrays.stream(strings)
.map(PreferredTime::new)
.collect(Collectors.toSet());
}

}
18 changes: 16 additions & 2 deletions src/main/java/seedu/address/storage/JsonAdaptedPerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.preferredtime.PreferredTime;
import seedu.address.model.tag.Tag;

/**
Expand All @@ -33,6 +34,7 @@ class JsonAdaptedPerson {
private final String address;
private final List<JsonAdaptedTag> tags = new ArrayList<>();
private final List<JsonAdaptedGame> games = new ArrayList<>();
private final List<JsonAdaptedPreferredTime> preferredTimes = new ArrayList<>();


/**
Expand All @@ -42,7 +44,8 @@ class JsonAdaptedPerson {
public JsonAdaptedPerson(@JsonProperty("name") String name, @JsonProperty("phone") String phone,
@JsonProperty("email") String email, @JsonProperty("address") String address,
@JsonProperty("tags") List<JsonAdaptedTag> tags,
@JsonProperty("games") List<JsonAdaptedGame> games) {
@JsonProperty("games") List<JsonAdaptedGame> games,
@JsonProperty("preferred times") List<JsonAdaptedPreferredTime> preferredTimes) {
this.name = name;
this.phone = phone;
this.email = email;
Expand All @@ -53,6 +56,9 @@ public JsonAdaptedPerson(@JsonProperty("name") String name, @JsonProperty("phone
if (games != null) {
this.games.addAll(games);
}
if (preferredTimes != null) {
this.preferredTimes.addAll(preferredTimes);
}
}

/**
Expand All @@ -69,6 +75,9 @@ public JsonAdaptedPerson(Person source) {
games.addAll(source.getGames().values().stream()
.map(JsonAdaptedGame::new)
.collect(Collectors.toList()));
preferredTimes.addAll(source.getPreferredTimes().stream()
.map(JsonAdaptedPreferredTime::new)
.collect(Collectors.toList()));
}

/**
Expand All @@ -85,6 +94,10 @@ public Person toModelType() throws IllegalValueException {
for (JsonAdaptedGame game : games) {
personGames.put(game.getGameName(), game.toModelType());
}
final List<PreferredTime> personPreferredTimes = new ArrayList<>();
for (JsonAdaptedPreferredTime preferredTime: preferredTimes) {
personPreferredTimes.add(preferredTime.toModelType());
}

if (name == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Name.class.getSimpleName()));
Expand Down Expand Up @@ -120,8 +133,9 @@ public Person toModelType() throws IllegalValueException {

final Set<Tag> modelTags = new HashSet<>(personTags);
final Map<String, Game> modelGames = new HashMap<>(personGames);
final Set<PreferredTime> modelPreferredTimes = new HashSet<>(personPreferredTimes);

return new Person(modelName, modelPhone, modelEmail, modelAddress, modelTags, modelGames);
return new Person(modelName, modelPhone, modelEmail, modelAddress, modelTags, modelGames, modelPreferredTimes);
}

}
Loading

0 comments on commit 38da93a

Please sign in to comment.