Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update code to use Availability attribute #80

Merged
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
40f8735
Add `Availability` data field to Person
tsulim Mar 14, 2024
7f10c06
Update SampleDataUtil with `Availability` data
tsulim Mar 14, 2024
56119e3
Create JsonAdaptedAvailability class
tsulim Mar 14, 2024
ea9ded8
Add `Availability` data field to JsonAdaptedPerson
tsulim Mar 14, 2024
7f3d183
Update Messages with `Availability` data field
tsulim Mar 14, 2024
b6c40b4
Add Prefix "a/" for specifying `Availability`
tsulim Mar 14, 2024
549d44a
Update AddCommand with `Availability` data field
tsulim Mar 14, 2024
239c102
Update ParserUtil with `Availability` data field
tsulim Mar 14, 2024
0a9723c
Update AddCommandParser with `Availability` data field
tsulim Mar 14, 2024
740035c
Add `Availability` data field to PersonBuilder
tsulim Mar 14, 2024
e07bb2f
Update EditCommand with `Availability` data field
tsulim Mar 14, 2024
60ca861
Update usage message in AddCommand
tsulim Mar 14, 2024
56e6ddc
Update EditCommandParser with `Availability` data field
tsulim Mar 14, 2024
a6e7e28
Update EditPersonDescriptorBuilder with `Availability` data field
tsulim Mar 14, 2024
c439cce
Update CommandTestUtil with `Availability` data
tsulim Mar 14, 2024
dff8753
Update TypicalPersons with `Availability` data
tsulim Mar 14, 2024
0237e03
Update EditCommandTest with `Availability` data
tsulim Mar 14, 2024
0e4890b
Update AddCommandParserTest with `Availability` data field
tsulim Mar 14, 2024
35e04bf
Update PersonUtil with `Availability` data field
tsulim Mar 15, 2024
af02feb
Update JsonAdaptedPersonTest with `Availability` data field
tsulim Mar 15, 2024
590bdaa
Update PersonTest with `Availability` data field
tsulim Mar 15, 2024
0cc2b96
Fix error in ParserUtil and EditPersonDescriptorTest
tsulim Mar 15, 2024
a522cc2
Update EditCommandParserTest with `Availability` data field
tsulim Mar 15, 2024
3678600
Fix error in AddCommandParser and AddCommandParserTest
tsulim Mar 15, 2024
b65a51b
Update typicalPersonsStaffBook.json with `Availability` data
tsulim Mar 15, 2024
b4cd150
Fix error in LogicManagerTest
tsulim Mar 15, 2024
95228a5
Update ui with `Availability` data field
tsulim Mar 15, 2024
0522330
Fix error in PersonUtil
tsulim Mar 15, 2024
e784d83
Merge remote-tracking branch 'upstream/master'
tsulim Mar 15, 2024
833048f
Merge branch 'master' into 66-update-with-availability
tsulim Mar 15, 2024
255a3af
Merge remote-tracking branch 'upstream/master'
tsulim Mar 15, 2024
a4d230f
Merge remote-tracking branch 'upstream/master'
tsulim Mar 16, 2024
fcab6f9
Fix merge conflicts
tsulim Mar 16, 2024
6e71003
Fix errors in test
tsulim Mar 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/staffconnect/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public static String format(Person person) {
.append(person.getModule())
.append("; Tags: ");
person.getTags().forEach(builder::append);
builder.append("; Availabilities: ");
person.getAvailabilities().forEach(builder::append);
return builder.toString();
}

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/staffconnect/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package staffconnect.logic.commands;

import static java.util.Objects.requireNonNull;
import static staffconnect.logic.parser.CliSyntax.PREFIX_AVAILABILITY;
import static staffconnect.logic.parser.CliSyntax.PREFIX_EMAIL;
import static staffconnect.logic.parser.CliSyntax.PREFIX_MODULE;
import static staffconnect.logic.parser.CliSyntax.PREFIX_NAME;
Expand Down Expand Up @@ -28,15 +29,18 @@ public class AddCommand extends Command {
+ PREFIX_EMAIL + "EMAIL "
+ PREFIX_VENUE + "VENUE "
+ PREFIX_MODULE + "MODULE "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "[" + PREFIX_TAG + "TAG]... "
+ "[" + PREFIX_AVAILABILITY + "AVAILABILITY]...\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "John Doe "
+ PREFIX_PHONE + "98765432 "
+ PREFIX_EMAIL + "[email protected] "
+ PREFIX_VENUE + "311, Clementi Ave 2, #02-25 "
+ PREFIX_MODULE + "CS2103 "
+ PREFIX_TAG + "friends "
+ PREFIX_TAG + "owesMoney";
+ PREFIX_TAG + "owesMoney"
+ PREFIX_AVAILABILITY + "mon"
+ PREFIX_AVAILABILITY + "thur";

public static final String MESSAGE_SUCCESS = "New person added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the staff book";
Expand Down
39 changes: 34 additions & 5 deletions src/main/java/staffconnect/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package staffconnect.logic.commands;

import static java.util.Objects.requireNonNull;
import static staffconnect.logic.parser.CliSyntax.PREFIX_AVAILABILITY;
import static staffconnect.logic.parser.CliSyntax.PREFIX_EMAIL;
import static staffconnect.logic.parser.CliSyntax.PREFIX_MODULE;
import static staffconnect.logic.parser.CliSyntax.PREFIX_NAME;
Expand All @@ -22,6 +23,7 @@
import staffconnect.logic.Messages;
import staffconnect.logic.commands.exceptions.CommandException;
import staffconnect.model.Model;
import staffconnect.model.availability.Availability;
import staffconnect.model.person.Email;
import staffconnect.model.person.Module;
import staffconnect.model.person.Name;
Expand All @@ -46,7 +48,8 @@ public class EditCommand extends Command {
+ "[" + PREFIX_EMAIL + "EMAIL] "
+ "[" + PREFIX_VENUE + "VENUE] "
+ "[" + PREFIX_MODULE + "MODULE] "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "[" + PREFIX_TAG + "TAG]... "
+ "[" + PREFIX_AVAILABILITY + "AVAILABILITY]...\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_PHONE + "91234567 "
+ PREFIX_EMAIL + "[email protected]";
Expand Down Expand Up @@ -104,8 +107,11 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
Venue updatedVenue = editPersonDescriptor.getVenue().orElse(personToEdit.getVenue());
Module updatedModule = editPersonDescriptor.getModule().orElse(personToEdit.getModule());
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());
Set<Availability> updatedAvailabilities = editPersonDescriptor.getAvailabilities()
.orElse(personToEdit.getAvailabilities());

return new Person(updatedName, updatedPhone, updatedEmail, updatedVenue, updatedModule, updatedTags);
return new Person(updatedName, updatedPhone, updatedEmail, updatedVenue, updatedModule,
updatedTags, updatedAvailabilities);
}

@Override
Expand Down Expand Up @@ -143,12 +149,13 @@ public static class EditPersonDescriptor {
private Venue venue;
private Module module;
private Set<Tag> tags;
private Set<Availability> availabilities;

public EditPersonDescriptor() {}

/**
* Copy constructor.
* A defensive copy of {@code tags} is used internally.
* A defensive copy of {@code tags} and {@code availabilities} is used internally.
*/
public EditPersonDescriptor(EditPersonDescriptor toCopy) {
setName(toCopy.name);
Expand All @@ -157,13 +164,14 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) {
setVenue(toCopy.venue);
setModule(toCopy.module);
setTags(toCopy.tags);
setAvailabilities(toCopy.availabilities);
}

/**
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(name, phone, email, venue, module, tags);
return CollectionUtil.isAnyNonNull(name, phone, email, venue, module, tags, availabilities);
}

public void setName(Name name) {
Expand Down Expand Up @@ -223,6 +231,25 @@ public Optional<Set<Tag>> getTags() {
return (tags != null) ? Optional.of(Collections.unmodifiableSet(tags)) : Optional.empty();
}

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

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

@Override
public boolean equals(Object other) {
if (other == this) {
Expand All @@ -240,7 +267,8 @@ public boolean equals(Object other) {
&& Objects.equals(email, otherEditPersonDescriptor.email)
&& Objects.equals(venue, otherEditPersonDescriptor.venue)
&& Objects.equals(module, otherEditPersonDescriptor.module)
&& Objects.equals(tags, otherEditPersonDescriptor.tags);
&& Objects.equals(tags, otherEditPersonDescriptor.tags)
&& Objects.equals(availabilities, otherEditPersonDescriptor.availabilities);
}

@Override
Expand All @@ -252,6 +280,7 @@ public String toString() {
.add("venue", venue)
.add("module", module)
.add("tags", tags)
.add("availabilities", availabilities)
.toString();
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/staffconnect/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package staffconnect.logic.parser;

import static staffconnect.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static staffconnect.logic.parser.CliSyntax.PREFIX_AVAILABILITY;
import static staffconnect.logic.parser.CliSyntax.PREFIX_EMAIL;
import static staffconnect.logic.parser.CliSyntax.PREFIX_MODULE;
import static staffconnect.logic.parser.CliSyntax.PREFIX_NAME;
Expand All @@ -13,6 +14,7 @@

import staffconnect.logic.commands.AddCommand;
import staffconnect.logic.parser.exceptions.ParseException;
import staffconnect.model.availability.Availability;
import staffconnect.model.person.Email;
import staffconnect.model.person.Module;
import staffconnect.model.person.Name;
Expand All @@ -33,9 +35,10 @@ 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_VENUE, PREFIX_MODULE, PREFIX_TAG);
PREFIX_EMAIL, PREFIX_VENUE, PREFIX_MODULE, PREFIX_TAG, PREFIX_AVAILABILITY);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_VENUE, PREFIX_MODULE)
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_VENUE,
PREFIX_MODULE)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}
Expand All @@ -48,8 +51,10 @@ public AddCommand parse(String args) throws ParseException {
Venue venue = ParserUtil.parseVenue(argMultimap.getValue(PREFIX_VENUE).get());
Module module = ParserUtil.parseModule(argMultimap.getValue(PREFIX_MODULE).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
Set<Availability> availabilityList =
ParserUtil.parseAvailabilities(argMultimap.getAllValues(PREFIX_AVAILABILITY));
Comment on lines +56 to +57
Copy link

@Pluiexo Pluiexo Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current default behaviour if the the user types nothing for availability, the availability is listed as none of the days, rather than any or all of the days

Copy link
Collaborator

@whitesnowx whitesnowx Mar 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that behaviour is intuitive imo, as opposed to the alternative be that availability be defaulted to all the days

Possible future implementation idea: user can type a/ALL and system parses it as the 7 days


Person person = new Person(name, phone, email, venue, module, tagList);
Person person = new Person(name, phone, email, venue, module, tagList, availabilityList);

return new AddCommand(person);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/staffconnect/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public class CliSyntax {
public static final Prefix PREFIX_VENUE = new Prefix("v/");
public static final Prefix PREFIX_MODULE = new Prefix("m/");
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_AVAILABILITY = new Prefix("a/");

}
25 changes: 24 additions & 1 deletion src/main/java/staffconnect/logic/parser/EditCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.requireNonNull;
import static staffconnect.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static staffconnect.logic.parser.CliSyntax.PREFIX_AVAILABILITY;
import static staffconnect.logic.parser.CliSyntax.PREFIX_EMAIL;
import static staffconnect.logic.parser.CliSyntax.PREFIX_MODULE;
import static staffconnect.logic.parser.CliSyntax.PREFIX_NAME;
Expand All @@ -18,6 +19,7 @@
import staffconnect.logic.commands.EditCommand;
import staffconnect.logic.commands.EditCommand.EditPersonDescriptor;
import staffconnect.logic.parser.exceptions.ParseException;
import staffconnect.model.availability.Availability;
import staffconnect.model.tag.Tag;

/**
Expand All @@ -34,7 +36,7 @@ public EditCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_VENUE, PREFIX_MODULE, PREFIX_TAG);
PREFIX_VENUE, PREFIX_MODULE, PREFIX_TAG, PREFIX_AVAILABILITY);

Index index;

Expand Down Expand Up @@ -65,6 +67,8 @@ public EditCommand parse(String args) throws ParseException {
editPersonDescriptor.setModule(ParserUtil.parseModule(argMultimap.getValue(PREFIX_MODULE).get()));
}
parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editPersonDescriptor::setTags);
parseAvailabilitiesForEdit(argMultimap.getAllValues(PREFIX_AVAILABILITY))
.ifPresent(editPersonDescriptor::setAvailabilities);

if (!editPersonDescriptor.isAnyFieldEdited()) {
throw new ParseException(EditCommand.MESSAGE_NOT_EDITED);
Expand All @@ -88,4 +92,23 @@ private Optional<Set<Tag>> parseTagsForEdit(Collection<String> tags) throws Pars
return Optional.of(ParserUtil.parseTags(tagSet));
}

/**
* Parses {@code Collection<String> availabilities} into a {@code Set<Availability>}
* if {@code availabilities} is non-empty.
* If {@code availabilities} contain only one element which is an empty string, it will be parsed
* into a {@code Set<Availability>} containing zero availabilities.
*/
private Optional<Set<Availability>> parseAvailabilitiesForEdit(Collection<String> availabilities)
throws ParseException {
assert availabilities != null;

if (availabilities.isEmpty()) {
return Optional.empty();
}
Collection<String> availabilitySet =
availabilities.size() == 1 && availabilities.contains("")
? Collections.emptySet()
: availabilities;
return Optional.of(ParserUtil.parseAvailabilities(availabilitySet));
}
}
28 changes: 28 additions & 0 deletions src/main/java/staffconnect/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import staffconnect.commons.core.index.Index;
import staffconnect.commons.util.StringUtil;
import staffconnect.logic.parser.exceptions.ParseException;
import staffconnect.model.availability.Availability;
import staffconnect.model.person.Email;
import staffconnect.model.person.Module;
import staffconnect.model.person.Name;
Expand Down Expand Up @@ -137,4 +138,31 @@ public static Set<Tag> parseTags(Collection<String> tags) throws ParseException
}
return tagSet;
}

/**
* Parses a {@code String availability} into a {@code Availability}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code availability} is invalid.
*/
public static Availability parseAvailability(String availability) throws ParseException {
requireNonNull(availability);
String trimmedAvailability = availability.trim();
if (!Availability.isValidAvailability(trimmedAvailability)) {
throw new ParseException(Availability.MESSAGE_CONSTRAINTS);
}
return new Availability(trimmedAvailability);
}

/**
* Parses {@code Collection<String> availabilities} into a {@code Set<Availability>}.
*/
public static Set<Availability> parseAvailabilities(Collection<String> availabilities) throws ParseException {
requireNonNull(availabilities);
final Set<Availability> availabilitySet = new HashSet<>();
for (String value : availabilities) {
availabilitySet.add(parseAvailability(value));
}
return availabilitySet;
}
}
22 changes: 18 additions & 4 deletions src/main/java/staffconnect/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Set;

import staffconnect.commons.util.ToStringBuilder;
import staffconnect.model.availability.Availability;
import staffconnect.model.tag.Tag;

/**
Expand All @@ -25,18 +26,21 @@
private final Venue venue;
private final Module module;
private final Set<Tag> tags = new HashSet<>();
private final Set<Availability> availabilities = new HashSet<>();

/**
* Every field must be present and not null.
*/
public Person(Name name, Phone phone, Email email, Venue venue, Module module, Set<Tag> tags) {
requireAllNonNull(name, phone, email, venue, module, tags);
public Person(Name name, Phone phone, Email email, Venue venue, Module module,
Set<Tag> tags, Set<Availability> availabilities) {
requireAllNonNull(name, phone, email, venue, module, tags, availabilities);
this.name = name;
this.phone = phone;
this.email = email;
this.venue = venue;
this.module = module;
this.tags.addAll(tags);
this.availabilities.addAll(availabilities);
}

public Name getName() {
Expand Down Expand Up @@ -67,6 +71,14 @@
return Collections.unmodifiableSet(tags);
}

/**
* Returns an immutable availability set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
*/
public Set<Availability> getAvailabilities() {
return Collections.unmodifiableSet(availabilities);
}

/**
* Returns true if both persons have the same name.
* This defines a weaker notion of equality between two persons.
Expand Down Expand Up @@ -101,13 +113,14 @@
&& email.equals(otherPerson.email)
&& venue.equals(otherPerson.venue)
&& module.equals(otherPerson.module)
&& tags.equals(otherPerson.tags);
&& tags.equals(otherPerson.tags)
&& availabilities.equals(otherPerson.availabilities);
}

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

Check warning on line 123 in src/main/java/staffconnect/model/person/Person.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/model/person/Person.java#L123

Added line #L123 was not covered by tests
}

@Override
Expand All @@ -119,6 +132,7 @@
.add("venue", venue)
.add("module", module)
.add("tags", tags)
.add("availabilities", availabilities)
.toString();
}

Expand Down
Loading
Loading