forked from nus-cs2103-AY2122S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from chongjunwei/event-model
Event model and eadd command
- Loading branch information
Showing
27 changed files
with
894 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package safeforhall.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import safeforhall.logic.commands.exceptions.CommandException; | ||
import safeforhall.logic.parser.CliSyntax; | ||
import safeforhall.model.Model; | ||
import safeforhall.model.event.Event; | ||
|
||
public class EaddCommand extends Command { | ||
public static final String COMMAND_WORD = "eadd"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds an event to the address book. " | ||
+ "Parameters: " | ||
+ CliSyntax.PREFIX_NAME + "NAME " | ||
+ CliSyntax.PREFIX_DATE + "DATE " | ||
+ CliSyntax.PREFIX_VENUE + "VENUE " | ||
+ CliSyntax.PREFIX_CAPACITY + "CAPACITY " | ||
|
||
+ "Example: " + COMMAND_WORD + " " | ||
+ CliSyntax.PREFIX_NAME + "Football " | ||
+ CliSyntax.PREFIX_DATE + "03-01-2021 " | ||
+ CliSyntax.PREFIX_VENUE + "NUS field " | ||
+ CliSyntax.PREFIX_CAPACITY + "5 "; | ||
|
||
public static final String MESSAGE_SUCCESS = "New event added: %1$s"; | ||
public static final String MESSAGE_DUPLICATE_EVENT = "This event already exists in the address book"; | ||
|
||
private final Event toAdd; | ||
|
||
/** | ||
* Creates an EaddCommand to add the specified {@code Event} | ||
*/ | ||
public EaddCommand(Event event) { | ||
requireNonNull(event); | ||
toAdd = event; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
if (model.hasEvent(toAdd)) { | ||
throw new CommandException(MESSAGE_DUPLICATE_EVENT); | ||
} | ||
|
||
model.addEvent(toAdd); | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof EaddCommand // instanceof handles nulls | ||
&& toAdd.equals(((EaddCommand) other).toAdd)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/safeforhall/logic/parser/EaddCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package safeforhall.logic.parser; | ||
|
||
import static safeforhall.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static safeforhall.logic.parser.CliSyntax.PREFIX_CAPACITY; | ||
import static safeforhall.logic.parser.CliSyntax.PREFIX_DATE; | ||
import static safeforhall.logic.parser.CliSyntax.PREFIX_NAME; | ||
import static safeforhall.logic.parser.CliSyntax.PREFIX_VENUE; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import safeforhall.logic.commands.EaddCommand; | ||
import safeforhall.logic.parser.exceptions.ParseException; | ||
import safeforhall.model.event.Capacity; | ||
import safeforhall.model.event.Event; | ||
import safeforhall.model.event.EventDate; | ||
import safeforhall.model.event.EventName; | ||
import safeforhall.model.event.Venue; | ||
|
||
/** | ||
* Parses input arguments and creates a new EaddCommand object | ||
*/ | ||
public class EaddCommandParser implements Parser<EaddCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the EaddCommand | ||
* and returns an EaddCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public EaddCommand parse(String args) throws ParseException { | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_DATE, | ||
PREFIX_VENUE, PREFIX_CAPACITY); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_DATE, | ||
PREFIX_VENUE, PREFIX_CAPACITY) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EaddCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
EventName eventName = ParserUtil.parseEventName(argMultimap.getValue(PREFIX_NAME).get()); | ||
EventDate eventDate = ParserUtil.parseEventDate(argMultimap.getValue(PREFIX_DATE).get()); | ||
Venue venue = ParserUtil.parseVenue(argMultimap.getValue(PREFIX_VENUE).get()); | ||
Capacity capacity = ParserUtil.parseCapacity(argMultimap.getValue(PREFIX_CAPACITY).get()); | ||
|
||
Event event = new Event(eventName, eventDate, venue, capacity); | ||
return new EaddCommand(event); | ||
} | ||
|
||
/** | ||
* Returns true if none of the prefixes contains empty {@code Optional} values in the given | ||
* {@code ArgumentMultimap}. | ||
*/ | ||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.