forked from nus-cs2103-AY2021S1/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.
Add basic make itinerary and add to itinerary commands
- Loading branch information
1 parent
3a2ef13
commit e18163f
Showing
6 changed files
with
214 additions
and
0 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
78 changes: 78 additions & 0 deletions
78
src/main/java/seedu/address/logic/commands/AddiCommand.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,78 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.List; | ||
|
||
import seedu.address.commons.core.Messages; | ||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.attraction.Attraction; | ||
import seedu.address.model.itinerary.Itinerary; | ||
import seedu.address.model.itinerary.ItineraryAttraction; | ||
|
||
/** | ||
* Adds an attraction identified using it's displayed index from the attractions list to an itinerary identified | ||
* using it's displayed index from the itinerary list in TrackPad. | ||
*/ | ||
public class AddiCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "addi"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds an attraction identified by the index number used" | ||
+ " in the displayed attraction list to the itinerary identified by the the index number used in the" | ||
+ " displayed itinerary list.\n " | ||
+ "Parameters: ATTRACTIONINDEX ITINERARYINDEX (must be a positive integer) " | ||
+ "Example: " + COMMAND_WORD + " 1 1"; | ||
|
||
public static final String MESSAGE_ADD_ATTRACTION_SUCCESS = "Added Attraction: %1$s to Itinerary: %1$s"; | ||
public static final String MESSAGE_DUPLICATE_ATTRACTION = "This attraction already exists in the itinerary."; | ||
|
||
private final Index attractionIndex; | ||
private final Index itineraryIndex; | ||
|
||
/** | ||
* @param attractionIndex of the attraction in the filtered attraction list to add | ||
* @param itineraryIndex of the itinerary in the filtered itinerary list to add to | ||
*/ | ||
public AddiCommand(Index attractionIndex, Index itineraryIndex) { | ||
this.attractionIndex = attractionIndex; | ||
this.itineraryIndex = itineraryIndex; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
List<Attraction> lastShownAttractionList = model.getFilteredAttractionList(); | ||
List<Itinerary> lastShownItineraryList = model.getFilteredItineraryList(); | ||
|
||
if (attractionIndex.getZeroBased() >= lastShownAttractionList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_ATTRACTION_DISPLAYED_INDEX); | ||
} | ||
|
||
if (itineraryIndex.getZeroBased() >= lastShownItineraryList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_ITINERARY_DISPLAYED_INDEX); | ||
} | ||
|
||
ItineraryAttraction itineraryAttractionToAdd = | ||
new ItineraryAttraction(lastShownAttractionList.get(attractionIndex.getZeroBased())); | ||
Itinerary itineraryToAddTo = lastShownItineraryList.get(itineraryIndex.getZeroBased()); | ||
|
||
if (itineraryToAddTo.contains(itineraryAttractionToAdd)) { | ||
throw new CommandException(MESSAGE_DUPLICATE_ATTRACTION); | ||
} | ||
|
||
itineraryToAddTo.addItineraryAttraction(itineraryAttractionToAdd); | ||
return new CommandResult(String.format(MESSAGE_ADD_ATTRACTION_SUCCESS, itineraryAttractionToAdd)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof AddiCommand // instanceof handles nulls | ||
&& attractionIndex.equals(((AddiCommand) other).attractionIndex)) | ||
&& itineraryIndex.equals(((AddiCommand) other).itineraryIndex); | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/seedu/address/logic/commands/NewiCommand.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,48 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
|
||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.itinerary.Itinerary; | ||
|
||
public class NewiCommand extends Command { | ||
public static final String COMMAND_WORD = "newi"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds an itinerary to TrackPad. " | ||
+ "Parameters: " | ||
+ PREFIX_NAME + "NAME "; | ||
|
||
public static final String MESSAGE_SUCCESS = "New itinerary added: %1$s"; | ||
public static final String MESSAGE_DUPLICATE_ITINERARY = "This itinerary already exists in TrackPad"; | ||
|
||
private final Itinerary toAdd; | ||
|
||
/** | ||
* Creates an NewiCommand to add the specified {@code Itinerary} | ||
*/ | ||
public NewiCommand(Itinerary itinerary) { | ||
requireNonNull(itinerary); | ||
toAdd = itinerary; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
if (model.hasItinerary(toAdd)) { | ||
throw new CommandException(MESSAGE_DUPLICATE_ITINERARY); | ||
} | ||
|
||
model.addItinerary(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 NewiCommand // instanceof handles nulls | ||
&& toAdd.equals(((NewiCommand) other).toAdd)); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/seedu/address/logic/parser/AddiCommandParser.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,29 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.AddiCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
public class AddiCommandParser implements Parser<AddiCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the AddiCommand | ||
* and returns an AddiCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public AddiCommand parse(String args) throws ParseException { | ||
try { | ||
// todo might need to find a better place for splitting the input indexes. Maybe ParserUtil? | ||
String[] indexes = args.trim().split(" "); | ||
Index attractionIndex = ParserUtil.parseIndex(indexes[0]); | ||
Index itineraryIndex = ParserUtil.parseIndex(indexes[1]); | ||
|
||
return new AddiCommand(attractionIndex, itineraryIndex); | ||
} catch (ParseException pe) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddiCommand.MESSAGE_USAGE), pe); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/seedu/address/logic/parser/NewiCommandParser.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,49 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
|
||
import java.util.ArrayList; | ||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.NewiCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.attraction.Name; | ||
import seedu.address.model.itinerary.Itinerary; | ||
import seedu.address.model.itinerary.ItineraryAttraction; | ||
|
||
/** | ||
* Parses input arguments and creates a new NewiCommand object | ||
*/ | ||
public class NewiCommandParser implements Parser<NewiCommand> { | ||
/** | ||
* Parses the given {@code String} of arguments in the context of the NewiCommand | ||
* and returns an NewiCommand object for execution. | ||
* | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public NewiCommand parse(String args) throws ParseException { | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_NAME); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_NAME) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, NewiCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
// Name is not optional | ||
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get()); | ||
|
||
Itinerary itinerary = new Itinerary(name, new ArrayList<ItineraryAttraction>()); | ||
|
||
return new NewiCommand(itinerary); | ||
} | ||
|
||
/** | ||
* 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