Skip to content

Commit

Permalink
Change LocalDate to new EventDate (#76)
Browse files Browse the repository at this point in the history
* Update Attendee Test

* Add date to EventCard

* Make Name of person uneditable

* Update User Guide for Edit

* Refactor Events

* Update Attendees tests

* Update some tests for events

* Replace LocalDate with new EventDate

* Change LocalDate to EventDate to validate date input

* Refactor Event related Predicates

* Refactor to follow LoD

* Update SelectCommand to include date parameters

* Update tests for SelectCommand
  • Loading branch information
jieliangang authored and IcedCoffeeBoy committed Oct 31, 2018
1 parent c84a8ce commit ce1265f
Show file tree
Hide file tree
Showing 28 changed files with 693 additions and 104 deletions.
14 changes: 8 additions & 6 deletions src/main/java/seedu/address/logic/commands/EditEventCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_START_DATE;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_EVENTS;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

Expand All @@ -21,6 +20,7 @@
import seedu.address.model.event.Description;
import seedu.address.model.event.EndTime;
import seedu.address.model.event.Event;
import seedu.address.model.event.EventDate;
import seedu.address.model.event.EventName;
import seedu.address.model.event.Location;
import seedu.address.model.event.StartTime;
Expand Down Expand Up @@ -73,7 +73,7 @@ private static Event createEditedEvent(Event eventToEdit, EditEventDescriptor ed

EventName updatedName = editEventDescriptor.getEventName().orElse(eventToEdit.getEventName());
Description updatedDescription = editEventDescriptor.getDescription().orElse(eventToEdit.getDescription());
LocalDate updatedDate = editEventDescriptor.getDate().orElse(eventToEdit.getDate());
EventDate updatedDate = editEventDescriptor.getDate().orElse(eventToEdit.getDate());
StartTime updatedStartTime = editEventDescriptor.getStartTime().orElse(eventToEdit.getStartTime());
EndTime updatedEndTime = editEventDescriptor.getEndTime().orElse(eventToEdit.getEndTime());
Location updatedLocation = editEventDescriptor.getLocation().orElse(eventToEdit.getLocation());
Expand Down Expand Up @@ -129,7 +129,7 @@ public boolean equals(Object other) {
public static class EditEventDescriptor {
private EventName eventName;
private Description description;
private LocalDate date;
private EventDate date;
private StartTime startTime;
private EndTime endTime;
private Location location;
Expand All @@ -147,6 +147,7 @@ public EditEventDescriptor(EditEventDescriptor toCopy) {
setStartTime(toCopy.startTime);
setEndTime(toCopy.endTime);
setLocation(toCopy.location);
setDate(toCopy.date);
}

/**
Expand All @@ -172,11 +173,11 @@ public void setDescription(Description description) {
this.description = description;
}

public Optional<LocalDate> getDate() {
public Optional<EventDate> getDate() {
return Optional.ofNullable(date);
}

public void setDate(LocalDate date) {
public void setDate(EventDate date) {
this.date = date;
}

Expand Down Expand Up @@ -223,7 +224,8 @@ public boolean equals(Object other) {
&& getDescription().equals(e.getDescription())
&& getStartTime().equals(e.getStartTime())
&& getEndTime().equals(e.getEndTime())
&& getLocation().equals(e.getLocation());
&& getLocation().equals(e.getLocation())
&& getDate().equals(e.getDate());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.event.Attendees;
import seedu.address.model.event.Event;
import seedu.address.model.person.Person;

Expand Down Expand Up @@ -56,9 +55,8 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
Event event = lastShownEventList.get(indexEvent.getZeroBased());

String personName = person.getName().toString();
Attendees attendeesList = event.getAttendees();

if (!attendeesList.isSetEmpty() && attendeesList.hasName(personName)) {
if (!event.isAttendeeEmpty() && event.hasAttendee(personName)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/seedu/address/logic/commands/RemoveCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.event.Attendees;
import seedu.address.model.event.Event;
import seedu.address.model.person.Person;

Expand Down Expand Up @@ -58,11 +57,10 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
Event event = lastShownEventList.get(indexEvent.getZeroBased());

String personName = person.getName().toString();
Attendees attendeesList = event.getAttendees();

if (attendeesList.isSetEmpty()) {
if (event.isAttendeeEmpty()) {
throw new CommandException(MESSAGE_ATTENDEE_EMPTY);
} else if (!attendeesList.hasName(personName)) {
} else if (!event.hasAttendee(personName)) {
throw new CommandException(MESSAGE_ABSENT_PERSON);
}

Expand Down
48 changes: 41 additions & 7 deletions src/main/java/seedu/address/logic/commands/SelectCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MONTH;
import static seedu.address.logic.parser.CliSyntax.PREFIX_YEAR;

import java.util.List;

Expand All @@ -11,7 +14,9 @@
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.event.EventContainsAttendeePredicate;
import seedu.address.model.event.AttendeeContainsNamePredicate;
import seedu.address.model.event.EventContainsAttendeeAndDatePredicate;
import seedu.address.model.event.TimeType;
import seedu.address.model.person.Person;

//@@author jieliangang
Expand All @@ -24,16 +29,31 @@ public class SelectCommand extends Command {

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Selects the person identified by the index number used in the displayed person list "
+ "and display events the person is attending\n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";
+ "and display events the person is attending. Show events in selected date/month/year if indicated\n"
+ "Parameters: INDEX (must be a positive integer) "
+ "[" + PREFIX_DATE + "DATE] "
+ "[" + PREFIX_YEAR + "YEAR] "
+ "[" + PREFIX_MONTH + "MONTH] "
+ "If " + PREFIX_DATE + " is used, " + PREFIX_YEAR + " and " + PREFIX_MONTH + " will be ignored\n"
+ "Example: " + COMMAND_WORD + " 1 " + PREFIX_DATE + "2018-05-24" + " or "
+ COMMAND_WORD + " 1 " + PREFIX_MONTH + "05";

public static final String MESSAGE_SELECT_PERSON_SUCCESS = "Selected Person: %1$s";

private final Index targetIndex;
private final String date;
private final TimeType type;

public SelectCommand(Index targetIndex) {
this.targetIndex = targetIndex;
this.date = null;
this.type = TimeType.NONE;
}

public SelectCommand(Index targetIndex, String date, TimeType type) {
this.targetIndex = targetIndex;
this.date = date;
this.type = type;
}

@Override
Expand All @@ -48,8 +68,18 @@ public CommandResult execute(Model model, CommandHistory history) throws Command

Person person = filteredPersonList.get(targetIndex.getZeroBased());
String personName = person.getName().toString();
EventContainsAttendeePredicate predicate = new EventContainsAttendeePredicate(personName);
model.updateFilteredEventList(predicate);
AttendeeContainsNamePredicate predicate = new AttendeeContainsNamePredicate(personName);

if (type == TimeType.NONE) {
model.updateFilteredEventList(predicate);
} else {
assert date != null;
EventContainsAttendeeAndDatePredicate predicate2 =
new EventContainsAttendeeAndDatePredicate(personName, date, type);
model.updateFilteredEventList(predicate2);

}

model.sortByDate();

EventsCenter.getInstance().post(new JumpToListRequestEvent(targetIndex));
Expand All @@ -61,6 +91,10 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof SelectCommand // instanceof handles nulls
&& targetIndex.equals(((SelectCommand) other).targetIndex)); // state check
&& targetIndex.equals(((SelectCommand) other).targetIndex) // state check
&& (((date == null && (((SelectCommand) other).date) == null)) // short circuit if both are null
|| ((date != null && (((SelectCommand) other).date) != null)
&& date.equals(((SelectCommand) other).date)))
&& type.equals(((SelectCommand) other).type));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_START_DATE;

import java.time.LocalDate;
import java.util.stream.Stream;

import seedu.address.logic.commands.AddEventCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.event.Description;
import seedu.address.model.event.EndTime;
import seedu.address.model.event.Event;
import seedu.address.model.event.EventDate;
import seedu.address.model.event.EventName;
import seedu.address.model.event.Location;
import seedu.address.model.event.StartTime;
Expand Down Expand Up @@ -45,7 +45,7 @@ public AddEventCommand parse(String args) throws ParseException {
EventName name = ParserUtil.parseEventName(argMultimap.getValue(PREFIX_NAME).get());
Description description = ParserUtil.parseDescription(argMultimap.getValue(PREFIX_DESCRIPTION).get());
Location location = ParserUtil.parseLocation(argMultimap.getValue(PREFIX_LOCATION).get());
LocalDate date = ParserUtil.parseDate(argMultimap.getValue(PREFIX_DATE).get());
EventDate date = ParserUtil.parseDate(argMultimap.getValue(PREFIX_DATE).get());
StartTime startDate = ParserUtil.parseStartTime(argMultimap.getValue(PREFIX_START_DATE).get());
EndTime endDate = ParserUtil.parseEndTime(argMultimap.getValue(PREFIX_END_DATE).get());

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class CliSyntax {
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_DATE = new Prefix("date/");
public static final Prefix PREFIX_MONTH = new Prefix("m/");
public static final Prefix PREFIX_YEAR = new Prefix("y/");
public static final Prefix PREFIX_START_DATE = new Prefix("s/");
public static final Prefix PREFIX_END_DATE = new Prefix("e/");
public static final Prefix PREFIX_DESCRIPTION = new Prefix("d/");
Expand All @@ -20,4 +22,5 @@ public class CliSyntax {
public static final Prefix PREFIX_TO = new Prefix("to/");
public static final Prefix PREFIX_FROM = new Prefix("from/");


}
52 changes: 44 additions & 8 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static java.util.Objects.requireNonNull;

import java.time.LocalDate;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -12,6 +11,7 @@
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.event.Description;
import seedu.address.model.event.EndTime;
import seedu.address.model.event.EventDate;
import seedu.address.model.event.EventName;
import seedu.address.model.event.Location;
import seedu.address.model.event.StartTime;
Expand All @@ -27,7 +27,12 @@
*/
public class ParserUtil {

public static final String MONTH_VALIDATION_REGEX = "0[1-9]|1[012]";
public static final String YEAR_VALIDATION_REGEX = "\\d{4}";

public static final String MESSAGE_INVALID_INDEX = "Index is not a non-zero unsigned integer.";
public static final String MESSAGE_INVALID_MONTH = "Input for month is not valid or according to MM format.";
public static final String MESSAGE_INVALID_YEAR = "Input for year is not valid or according to YYYY format.";

/**
* Parses {@code oneBasedIndex} into an {@code Index} and returns it. Leading and trailing whitespaces will be
Expand Down Expand Up @@ -196,16 +201,47 @@ public static Location parseLocation(String location) throws ParseException {
*
* @throws ParseException if the given {@code name} is invalid.
*/
public static LocalDate parseDate(String date) throws ParseException {
public static EventDate parseDate(String date) throws ParseException {
requireNonNull(date);
String trimmedStartTime = date.trim();
// TODO: WORK ON VALIDATING DATE
//if (!Location.isValidLocation(trimmedLocation)) {
// throw new ParseException(Location.MESSAGE_LOCATION_CONSTRAINTS);
//}
return LocalDate.parse(date);
String trimmedDate = date.trim();
if (!EventDate.isValidDate(trimmedDate)) {
throw new ParseException(EventDate.MESSAGE_DATE_CONSTRAINTS);
}
return new EventDate(trimmedDate);
}

/**
* Parses a {@code String month} into a {@code String}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code month} is invalid.
*/
public static String parseMonth(String month) throws ParseException {
requireNonNull(month);
String trimmedDate = month.trim();
if (!trimmedDate.matches(MONTH_VALIDATION_REGEX)) {
throw new ParseException(MESSAGE_INVALID_MONTH);
}
return month;
}

/**
* Parses a {@code String year} into a {@code String}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code year} is invalid.
*/
public static String parseYear(String year) throws ParseException {
requireNonNull(year);
String trimmedDate = year.trim();
if (!trimmedDate.matches(YEAR_VALIDATION_REGEX)) {
throw new ParseException(MESSAGE_INVALID_YEAR);
}
return year;
}




/**
* Parses a {@code String startTime} into a {@code StartTime}.
Expand Down
42 changes: 40 additions & 2 deletions src/main/java/seedu/address/logic/parser/SelectCommandParser.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package seedu.address.logic.parser;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MONTH;
import static seedu.address.logic.parser.CliSyntax.PREFIX_YEAR;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.SelectCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.event.EventDate;
import seedu.address.model.event.TimeType;


/**
* Parses input arguments and creates a new SelectCommand object
Expand All @@ -17,12 +24,43 @@ public class SelectCommandParser implements Parser<SelectCommand> {
* @throws ParseException if the user input does not conform the expected format
*/
public SelectCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_DATE, PREFIX_MONTH, PREFIX_YEAR);

Index index;

try {
Index index = ParserUtil.parseIndex(args);
return new SelectCommand(index);
index = ParserUtil.parseIndex(argMultimap.getPreamble());
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, SelectCommand.MESSAGE_USAGE), pe);
}

boolean hasDate = argMultimap.getValue(PREFIX_DATE).isPresent();
boolean hasMonth = argMultimap.getValue(PREFIX_MONTH).isPresent();
boolean hasYear = argMultimap.getValue(PREFIX_YEAR).isPresent();

if (!hasDate && !hasMonth && !hasYear) {
return new SelectCommand(index);
} else if (hasDate && !hasMonth && !hasYear) {
EventDate date = ParserUtil.parseDate(argMultimap.getValue(PREFIX_DATE).get());
return new SelectCommand(index, date.eventDate, TimeType.DAY);
} else if (hasMonth && !hasYear && !hasDate) {
String month = ParserUtil.parseMonth(argMultimap.getValue(PREFIX_MONTH).get());
return new SelectCommand(index, month, TimeType.MONTH);
} else if (!hasMonth && hasYear && !hasDate) {
String year = ParserUtil.parseYear(argMultimap.getValue(PREFIX_YEAR).get());
return new SelectCommand(index, year, TimeType.YEAR);
} else if (hasMonth && hasYear && !hasDate) {
String month = ParserUtil.parseMonth(argMultimap.getValue(PREFIX_MONTH).get());
String year = ParserUtil.parseYear(argMultimap.getValue(PREFIX_YEAR).get());
String combined = year + "-" + month;
return new SelectCommand(index, combined, TimeType.MONTH_AND_YEAR);
} else {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, SelectCommand.MESSAGE_USAGE));
}

}
}
Loading

0 comments on commit ce1265f

Please sign in to comment.