-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add Calendar Model #26
Changes from 39 commits
2a5d71b
0305d6c
b606645
1f187e7
51bbf4f
86ee380
1e4552a
8c9250b
26a04bf
fc21349
9403528
cc52809
16ee959
6f77fc0
db9d380
b51f05b
5fbb687
8780b22
6c781f5
ba6fe64
1a8dc5f
1b8e245
28b2fe6
86de798
d804915
8252320
2327785
dfbb32c
c874bf0
08665ea
3bd77e1
869000d
82e090f
29facbc
cf3c2f2
769d7ca
5f68126
2a60e0a
3378103
5be4fba
16711ba
9c5c0fd
72d83d4
d6acfa7
e002f18
7413df6
22c4224
f3f301a
47bcb05
365c5db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EVENT_DESCRIPTION; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EVENT_END_DATE_TIME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EVENT_START_DATE_TIME; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.event.Event; | ||
|
||
/** | ||
* Adds an event to the calendar. | ||
*/ | ||
public class AddEventCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "addEvent"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds an event to the calendar. " | ||
+ "Parameters: " | ||
+ PREFIX_EVENT_DESCRIPTION + "DESCRIPTION " | ||
+ PREFIX_EVENT_START_DATE_TIME + "START DATE AND TIME " | ||
+ PREFIX_EVENT_END_DATE_TIME + "END DATE AND TIME...\n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ PREFIX_EVENT_DESCRIPTION + "Nap " | ||
+ PREFIX_EVENT_START_DATE_TIME + "2024-01-01 12:00 " | ||
+ PREFIX_EVENT_END_DATE_TIME + "2024-01-01 18:00"; | ||
|
||
public static final String MESSAGE_SUCCESS = "New event added: %1$s"; | ||
|
||
public static final String MESSAGE_EVENT_CONFLICT = "This event is conflicting with another event"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a full-stop or ! after the sentence to keep it consistent with other messages. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For |
||
|
||
private final Event toAdd; | ||
|
||
/** | ||
* Creates an AddEventCommand to add the event into the calendar. | ||
* | ||
* @param event event to be added. | ||
*/ | ||
public AddEventCommand(Event event) { | ||
requireNonNull(event); | ||
toAdd = event; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
if (!model.canAddEvent(toAdd)) { | ||
throw new CommandException(MESSAGE_EVENT_CONFLICT); | ||
} | ||
|
||
model.addEvent(toAdd); | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd))); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof AddEventCommand)) { | ||
return false; | ||
} | ||
|
||
AddEventCommand otherAddCommand = (AddEventCommand) other; | ||
return toAdd.equals(otherAddCommand.toAdd); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("toAdd", toAdd) | ||
.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EVENT_DESCRIPTION; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EVENT_END_DATE_TIME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EVENT_START_DATE_TIME; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.AddEventCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.event.Event; | ||
import seedu.address.model.event.EventDescription; | ||
import seedu.address.model.event.EventPeriod; | ||
|
||
/** | ||
* Parses input arguments and creates a new AddEventCommand object. | ||
*/ | ||
public class AddEventCommandParser implements Parser<AddEventCommand> { | ||
@Override | ||
public AddEventCommand parse(String args) throws ParseException { | ||
ArgumentMultimap argMultiMap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_EVENT_DESCRIPTION, PREFIX_EVENT_START_DATE_TIME, | ||
PREFIX_EVENT_END_DATE_TIME); | ||
|
||
if (!arePrefixesPresent(argMultiMap, PREFIX_EVENT_DESCRIPTION, PREFIX_EVENT_START_DATE_TIME, | ||
PREFIX_EVENT_END_DATE_TIME) || !argMultiMap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddEventCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
argMultiMap.verifyNoDuplicatePrefixesFor(PREFIX_EVENT_DESCRIPTION, PREFIX_EVENT_START_DATE_TIME, | ||
PREFIX_EVENT_END_DATE_TIME); | ||
EventDescription description = ParserUtil.parseEventDescription(argMultiMap | ||
.getValue(PREFIX_EVENT_DESCRIPTION).get()); | ||
EventPeriod eventPeriod = ParserUtil.parseEventPeriod(argMultiMap.getValue(PREFIX_EVENT_START_DATE_TIME).get(), | ||
argMultiMap.getValue(PREFIX_EVENT_END_DATE_TIME).get()); | ||
|
||
return new AddEventCommand(new Event(description, eventPeriod)); | ||
} | ||
|
||
/** | ||
* Checks if all the given prefix fields are non-empty. | ||
* | ||
* @param argumentMultimap argumentMultimap managing arguments for this command. | ||
* @param prefixes prefixes to be tested. | ||
* @return true if all fields are non-empty, false if any field contains empty value. | ||
*/ | ||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since there is only 1 class that extends Component Parser it might be better to put the methods of this class into UniMateParser instead. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
import seedu.address.logic.commands.Command; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Represents an abstract parser for distinct components (e.g. AddressBook/Calendar). | ||
*/ | ||
public abstract class ComponentParser { | ||
public static final Pattern BASIC_COMMAND_FORMAT = Pattern.compile("(?<commandWord>\\S+)(?<arguments>.*)"); | ||
|
||
/** | ||
* Parses a user input string into the appropriate command object. | ||
* | ||
* @param userInput The user input string to be parsed. | ||
* @return A command object corresponding to the user's command. | ||
* @throws ParseException If the user input cannot be parsed successfully. | ||
*/ | ||
public abstract Command parseCommand(String userInput) throws ParseException; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; | ||
|
||
import java.util.Collection; | ||
import java.util.HashSet; | ||
|
@@ -9,6 +10,8 @@ | |
import seedu.address.commons.core.index.Index; | ||
import seedu.address.commons.util.StringUtil; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.event.EventDescription; | ||
import seedu.address.model.event.EventPeriod; | ||
import seedu.address.model.person.Address; | ||
import seedu.address.model.person.Email; | ||
import seedu.address.model.person.Name; | ||
|
@@ -121,4 +124,38 @@ public static Set<Tag> parseTags(Collection<String> tags) throws ParseException | |
} | ||
return tagSet; | ||
} | ||
|
||
/** | ||
* Parses the event description String into a EventDescription object. | ||
* | ||
* @param description description String. | ||
* @return EventDescription object with the given description String. | ||
* @throws ParseException if the description is empty. | ||
*/ | ||
public static EventDescription parseEventDescription(String description) throws ParseException { | ||
requireNonNull(description); | ||
String trimmedDescription = description.trim(); | ||
if (trimmedDescription.isEmpty()) { | ||
throw new ParseException(EventDescription.MESSAGE_CONSTRAINTS); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider putting this check into the EventDescription constructor instead as a validity check. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Abstracted out the checking of whether the Will still keep the check to be consistent with |
||
} | ||
return new EventDescription(trimmedDescription); | ||
} | ||
|
||
/** | ||
* Parses given start date string and end date string into an EventPeriod object. | ||
* | ||
* @param startDate start date string in 'yyyy-MM-dd HH:mm' format. | ||
* @param endDate end date string in 'yyyy-MM-dd HH:mm' format. | ||
* @return EventPeriod object describing the time period between startDate and endDate. | ||
* @throws ParseException if the startDate or endDate strings are in inproper format. | ||
*/ | ||
public static EventPeriod parseEventPeriod(String startDate, String endDate) throws ParseException { | ||
requireAllNonNull(startDate, endDate); | ||
String trimmedStartDate = startDate.trim(); | ||
String trimmedEndDate = endDate.trim(); | ||
if (!EventPeriod.isValidPeriod(trimmedStartDate, trimmedEndDate)) { | ||
throw new ParseException(EventPeriod.MESSAGE_CONSTRAINTS); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. Consider putting this check into EventPeriod constructor instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will still keep the check to be consistent with |
||
} | ||
return new EventPeriod(trimmedStartDate, trimmedEndDate); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The keyword "this' can be removed.