Skip to content

Commit

Permalink
Add basic make itinerary and add to itinerary commands
Browse files Browse the repository at this point in the history
  • Loading branch information
limkoonkiat committed Oct 13, 2020
1 parent 3a2ef13 commit e18163f
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public class Messages {
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_ATTRACTION_DISPLAYED_INDEX = "The attraction index provided is invalid";
public static final String MESSAGE_ATTRACTIONS_LISTED_OVERVIEW = "%1$d attractions listed!";
public static final String MESSAGE_INVALID_ITINERARY_DISPLAYED_INDEX = "The itinerary index provided is invalid";
public static final String MESSAGE_ITINERARY_LISTED_OVERVIEW = "%1$d itineraries listed!";

}
78 changes: 78 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddiCommand.java
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 src/main/java/seedu/address/logic/commands/NewiCommand.java
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 src/main/java/seedu/address/logic/parser/AddiCommandParser.java
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 src/main/java/seedu/address/logic/parser/NewiCommandParser.java
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());
}
}
8 changes: 8 additions & 0 deletions src/main/java/seedu/address/logic/parser/TrackPadParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.regex.Pattern;

import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.AddiCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCommand;
Expand All @@ -15,6 +16,7 @@
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.NewiCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -68,6 +70,12 @@ public Command parseCommand(String userInput) throws ParseException {
case HelpCommand.COMMAND_WORD:
return new HelpCommand();

case NewiCommand.COMMAND_WORD:
return new NewiCommandParser().parse(arguments);

case AddiCommand.COMMAND_WORD:
return new AddiCommandParser().parse(arguments);

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down

0 comments on commit e18163f

Please sign in to comment.