Skip to content

Commit

Permalink
Merge pull request #69 from tsulim/48-update-with-module
Browse files Browse the repository at this point in the history
Update code to use `Module` attribute
  • Loading branch information
iynixil authored Mar 14, 2024
2 parents ec4037b + 6c4cc36 commit 618b8b8
Show file tree
Hide file tree
Showing 33 changed files with 380 additions and 137 deletions.
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 @@ -43,6 +43,8 @@ public static String format(Person person) {
.append(person.getEmail())
.append("; Venue: ")
.append(person.getVenue())
.append("; Module: ")
.append(person.getModule())
.append("; Tags: ");
person.getTags().forEach(builder::append);
return builder.toString();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/staffconnect/logic/commands/AddCommand.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.parser.CliSyntax.PREFIX_EMAIL;
import static staffconnect.logic.parser.CliSyntax.PREFIX_MODULE;
import static staffconnect.logic.parser.CliSyntax.PREFIX_NAME;
import static staffconnect.logic.parser.CliSyntax.PREFIX_PHONE;
import static staffconnect.logic.parser.CliSyntax.PREFIX_TAG;
Expand All @@ -26,12 +27,14 @@ public class AddCommand extends Command {
+ PREFIX_PHONE + "PHONE "
+ PREFIX_EMAIL + "EMAIL "
+ PREFIX_VENUE + "VENUE "
+ PREFIX_MODULE + "MODULE "
+ "[" + PREFIX_TAG + "TAG]...\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";

Expand Down
20 changes: 18 additions & 2 deletions src/main/java/staffconnect/logic/commands/EditCommand.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.parser.CliSyntax.PREFIX_EMAIL;
import static staffconnect.logic.parser.CliSyntax.PREFIX_MODULE;
import static staffconnect.logic.parser.CliSyntax.PREFIX_NAME;
import static staffconnect.logic.parser.CliSyntax.PREFIX_PHONE;
import static staffconnect.logic.parser.CliSyntax.PREFIX_TAG;
Expand All @@ -22,6 +23,7 @@
import staffconnect.logic.commands.exceptions.CommandException;
import staffconnect.model.Model;
import staffconnect.model.person.Email;
import staffconnect.model.person.Module;
import staffconnect.model.person.Name;
import staffconnect.model.person.Person;
import staffconnect.model.person.Phone;
Expand All @@ -43,6 +45,7 @@ public class EditCommand extends Command {
+ "[" + PREFIX_PHONE + "PHONE] "
+ "[" + PREFIX_EMAIL + "EMAIL] "
+ "[" + PREFIX_VENUE + "VENUE] "
+ "[" + PREFIX_MODULE + "MODULE] "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_PHONE + "91234567 "
Expand Down Expand Up @@ -99,9 +102,10 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
Phone updatedPhone = editPersonDescriptor.getPhone().orElse(personToEdit.getPhone());
Email updatedEmail = editPersonDescriptor.getEmail().orElse(personToEdit.getEmail());
Venue updatedVenue = editPersonDescriptor.getVenue().orElse(personToEdit.getVenue());
Module updatedModule = editPersonDescriptor.getModule().orElse(personToEdit.getModule());
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());

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

@Override
Expand Down Expand Up @@ -137,6 +141,7 @@ public static class EditPersonDescriptor {
private Phone phone;
private Email email;
private Venue venue;
private Module module;
private Set<Tag> tags;

public EditPersonDescriptor() {}
Expand All @@ -150,14 +155,15 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) {
setPhone(toCopy.phone);
setEmail(toCopy.email);
setVenue(toCopy.venue);
setModule(toCopy.module);
setTags(toCopy.tags);
}

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

public void setName(Name name) {
Expand Down Expand Up @@ -192,6 +198,14 @@ public Optional<Venue> getVenue() {
return Optional.ofNullable(venue);
}

public void setModule(Module module) {
this.module = module;
}

public Optional<Module> getModule() {
return Optional.ofNullable(module);
}

/**
* Sets {@code tags} to this object's {@code tags}.
* A defensive copy of {@code tags} is used internally.
Expand Down Expand Up @@ -225,6 +239,7 @@ public boolean equals(Object other) {
&& Objects.equals(phone, otherEditPersonDescriptor.phone)
&& Objects.equals(email, otherEditPersonDescriptor.email)
&& Objects.equals(venue, otherEditPersonDescriptor.venue)
&& Objects.equals(module, otherEditPersonDescriptor.module)
&& Objects.equals(tags, otherEditPersonDescriptor.tags);
}

Expand All @@ -235,6 +250,7 @@ public String toString() {
.add("phone", phone)
.add("email", email)
.add("venue", venue)
.add("module", module)
.add("tags", tags)
.toString();
}
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/staffconnect/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static staffconnect.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static staffconnect.logic.parser.CliSyntax.PREFIX_EMAIL;
import static staffconnect.logic.parser.CliSyntax.PREFIX_MODULE;
import static staffconnect.logic.parser.CliSyntax.PREFIX_NAME;
import static staffconnect.logic.parser.CliSyntax.PREFIX_PHONE;
import static staffconnect.logic.parser.CliSyntax.PREFIX_TAG;
Expand All @@ -13,6 +14,7 @@
import staffconnect.logic.commands.AddCommand;
import staffconnect.logic.parser.exceptions.ParseException;
import staffconnect.model.person.Email;
import staffconnect.model.person.Module;
import staffconnect.model.person.Name;
import staffconnect.model.person.Person;
import staffconnect.model.person.Phone;
Expand All @@ -30,22 +32,24 @@ public class AddCommandParser implements Parser<AddCommand> {
* @throws ParseException if the user input does not conform the expected format
*/
public AddCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_VENUE, PREFIX_TAG);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE,
PREFIX_EMAIL, PREFIX_VENUE, PREFIX_MODULE, PREFIX_TAG);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_VENUE, PREFIX_PHONE, PREFIX_EMAIL)
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));
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_VENUE);
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_VENUE, PREFIX_MODULE);
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
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));

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

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 @@ -10,6 +10,7 @@ public class CliSyntax {
public static final Prefix PREFIX_PHONE = new Prefix("p/");
public static final Prefix PREFIX_EMAIL = new Prefix("e/");
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/");

}
10 changes: 8 additions & 2 deletions src/main/java/staffconnect/logic/parser/EditCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.util.Objects.requireNonNull;
import static staffconnect.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static staffconnect.logic.parser.CliSyntax.PREFIX_EMAIL;
import static staffconnect.logic.parser.CliSyntax.PREFIX_MODULE;
import static staffconnect.logic.parser.CliSyntax.PREFIX_NAME;
import static staffconnect.logic.parser.CliSyntax.PREFIX_PHONE;
import static staffconnect.logic.parser.CliSyntax.PREFIX_TAG;
Expand Down Expand Up @@ -32,7 +33,8 @@ public class EditCommandParser implements Parser<EditCommand> {
public EditCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_VENUE, PREFIX_TAG);
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_VENUE, PREFIX_MODULE, PREFIX_TAG);

Index index;

Expand All @@ -42,7 +44,8 @@ public EditCommand parse(String args) throws ParseException {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE), pe);
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_VENUE);
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_VENUE, PREFIX_MODULE);

EditPersonDescriptor editPersonDescriptor = new EditPersonDescriptor();

Expand All @@ -58,6 +61,9 @@ public EditCommand parse(String args) throws ParseException {
if (argMultimap.getValue(PREFIX_VENUE).isPresent()) {
editPersonDescriptor.setVenue(ParserUtil.parseVenue(argMultimap.getValue(PREFIX_VENUE).get()));
}
if (argMultimap.getValue(PREFIX_MODULE).isPresent()) {
editPersonDescriptor.setModule(ParserUtil.parseModule(argMultimap.getValue(PREFIX_MODULE).get()));
}
parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editPersonDescriptor::setTags);

if (!editPersonDescriptor.isAnyFieldEdited()) {
Expand Down
32 changes: 24 additions & 8 deletions src/main/java/staffconnect/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import staffconnect.commons.util.StringUtil;
import staffconnect.logic.parser.exceptions.ParseException;
import staffconnect.model.person.Email;
import staffconnect.model.person.Module;
import staffconnect.model.person.Name;
import staffconnect.model.person.Phone;
import staffconnect.model.person.Venue;
Expand Down Expand Up @@ -65,6 +66,21 @@ public static Phone parsePhone(String phone) throws ParseException {
return new Phone(trimmedPhone);
}

/**
* Parses a {@code String email} into an {@code Email}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code email} is invalid.
*/
public static Email parseEmail(String email) throws ParseException {
requireNonNull(email);
String trimmedEmail = email.trim();
if (!Email.isValidEmail(trimmedEmail)) {
throw new ParseException(Email.MESSAGE_CONSTRAINTS);
}
return new Email(trimmedEmail);
}

/**
* Parses a {@code String venue} into an {@code Venue}.
* Leading and trailing whitespaces will be trimmed.
Expand All @@ -81,18 +97,18 @@ public static Venue parseVenue(String venue) throws ParseException {
}

/**
* Parses a {@code String email} into an {@code Email}.
* Parses a {@code String module} into an {@code Module}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code email} is invalid.
* @throws ParseException if the given {@code module} is invalid.
*/
public static Email parseEmail(String email) throws ParseException {
requireNonNull(email);
String trimmedEmail = email.trim();
if (!Email.isValidEmail(trimmedEmail)) {
throw new ParseException(Email.MESSAGE_CONSTRAINTS);
public static Module parseModule(String module) throws ParseException {
requireNonNull(module);
String trimmedModule = module.trim();
if (!Module.isValidModule(trimmedModule)) {
throw new ParseException(Module.MESSAGE_CONSTRAINTS);
}
return new Email(trimmedEmail);
return new Module(trimmedModule);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/staffconnect/model/person/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Name {
"Names should only contain alphanumeric characters and spaces, and it should not be blank";

/*
* The first character of the address must not be a whitespace,
* The first character of the name must not be a whitespace,
* otherwise " " (a blank string) becomes a valid input.
*/
public static final String VALIDATION_REGEX = "[\\p{Alnum}][\\p{Alnum} ]*";
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/staffconnect/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@ public class Person {

// Data fields
private final Venue venue;
private final Module module;
private final Set<Tag> tags = new HashSet<>();

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

Expand All @@ -53,6 +55,10 @@ public Venue getVenue() {
return venue;
}

public Module getModule() {
return module;
}

/**
* Returns an immutable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
Expand Down Expand Up @@ -94,13 +100,14 @@ public boolean equals(Object other) {
&& phone.equals(otherPerson.phone)
&& email.equals(otherPerson.email)
&& venue.equals(otherPerson.venue)
&& module.equals(otherPerson.module)
&& tags.equals(otherPerson.tags);
}

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

@Override
Expand All @@ -110,6 +117,7 @@ public String toString() {
.add("phone", phone)
.add("email", email)
.add("venue", venue)
.add("module", module)
.add("tags", tags)
.toString();
}
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/staffconnect/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import staffconnect.model.ReadOnlyStaffBook;
import staffconnect.model.StaffBook;
import staffconnect.model.person.Email;
import staffconnect.model.person.Module;
import staffconnect.model.person.Name;
import staffconnect.model.person.Person;
import staffconnect.model.person.Phone;
Expand All @@ -20,23 +21,23 @@ public class SampleDataUtil {
public static Person[] getSamplePersons() {
return new Person[] {
new Person(new Name("Alex Yeoh"), new Phone("87438807"), new Email("[email protected]"),
new Venue("Blk 30 Geylang Street 29, #06-40"),
getTagSet("friends")),
new Venue("Blk 30 Geylang Street 29, #06-40"), new Module("CS1101S"),
getTagSet("professor")),
new Person(new Name("Bernice Yu"), new Phone("99272758"), new Email("[email protected]"),
new Venue("Blk 30 Lorong 3 Serangoon Gardens, #07-18"),
getTagSet("colleagues", "friends")),
new Venue("Blk 30 Lorong 3 Serangoon Gardens, #07-18"), new Module("CS1231S"),
getTagSet("tutor", "friends")),
new Person(new Name("Charlotte Oliveiro"), new Phone("93210283"), new Email("[email protected]"),
new Venue("Blk 11 Ang Mo Kio Street 74, #11-04"),
getTagSet("neighbours")),
new Venue("Blk 11 Ang Mo Kio Street 74, #11-04"), new Module("CS2030S"),
getTagSet("professor")),
new Person(new Name("David Li"), new Phone("91031282"), new Email("[email protected]"),
new Venue("Blk 436 Serangoon Gardens Street 26, #16-43"),
getTagSet("family")),
new Venue("Blk 436 Serangoon Gardens Street 26, #16-43"), new Module("CS2040S"),
getTagSet("professor")),
new Person(new Name("Irfan Ibrahim"), new Phone("92492021"), new Email("[email protected]"),
new Venue("Blk 47 Tampines Street 20, #17-35"),
getTagSet("classmates")),
new Venue("Blk 47 Tampines Street 20, #17-35"), new Module("CS2100"),
getTagSet("tutor")),
new Person(new Name("Roy Balakrishnan"), new Phone("92624417"), new Email("[email protected]"),
new Venue("Blk 45 Aljunied Street 85, #11-31"),
getTagSet("colleagues"))
new Venue("Blk 45 Aljunied Street 85, #11-31"), new Module("CS2101"),
getTagSet("professor"))
};
}

Expand Down
Loading

0 comments on commit 618b8b8

Please sign in to comment.