forked from nus-cs2103-AY1819S2/addressbook-level4
-
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 #179 from CS2103-AY1819S2-W10-3/implement-route-fe…
…ature Implement route feature
- Loading branch information
Showing
10 changed files
with
317 additions
and
5 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
63 changes: 63 additions & 0 deletions
63
src/main/java/seedu/equipment/logic/commands/RouteCommand.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,63 @@ | ||
package seedu.equipment.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.List; | ||
|
||
import seedu.equipment.logic.CommandHistory; | ||
import seedu.equipment.logic.commands.exceptions.CommandException; | ||
import seedu.equipment.model.Model; | ||
import seedu.equipment.model.equipment.Address; | ||
import seedu.equipment.model.equipment.Equipment; | ||
|
||
/** | ||
* Selects an equipment identified using it's displayed index from the equipment manager. | ||
*/ | ||
public class RouteCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "route"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Produce a optimized route from current location to the displayed equipment list.\n" | ||
+ "Parameters: CURRENT ADDRESS (must be a valid address with postal code and country name)\n" | ||
+ "Example: " + COMMAND_WORD + " School of Computing, NUS, Singapore 117417"; | ||
|
||
public static final String MESSAGE_ROUTE_EQUIPMENT_SUCCESS = "Routes displayed on map."; | ||
|
||
public static final String MESSAGE_NO_EQUIPMENTS_TO_ROUTE = "At least 1 equipment in the equipment panel " | ||
+ " is required to use route command."; | ||
|
||
public static final String MESSAGE_TOO_MANY_EQUIPMENTS_TO_ROUTE = "At most 15 equipment in the equipment panel " | ||
+ " to use route command."; | ||
|
||
private final Address startendAddress; | ||
|
||
public RouteCommand(Address startendAddress) { | ||
this.startendAddress = startendAddress; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model, CommandHistory history) throws CommandException { | ||
requireNonNull(model); | ||
|
||
List<Equipment> filteredEquipmentList = model.getFilteredPersonList(); | ||
|
||
if (filteredEquipmentList.size() < 1) { | ||
throw new CommandException(MESSAGE_NO_EQUIPMENTS_TO_ROUTE); | ||
} | ||
|
||
if (filteredEquipmentList.size() > 15) { | ||
throw new CommandException(MESSAGE_TOO_MANY_EQUIPMENTS_TO_ROUTE); | ||
} | ||
|
||
return new CommandResult(MESSAGE_ROUTE_EQUIPMENT_SUCCESS, false, false, false, true, startendAddress); | ||
|
||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof RouteCommand // instanceof handles nulls | ||
&& startendAddress.equals(((RouteCommand) other).startendAddress)); // state check | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
src/main/java/seedu/equipment/logic/parser/RouteCommandParser.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,27 @@ | ||
package seedu.equipment.logic.parser; | ||
|
||
import seedu.equipment.commons.core.Messages; | ||
import seedu.equipment.logic.commands.RouteCommand; | ||
import seedu.equipment.logic.parser.exceptions.ParseException; | ||
import seedu.equipment.model.equipment.Address; | ||
|
||
/** | ||
* Parses input arguments and creates a new RouteCommand object | ||
*/ | ||
public class RouteCommandParser implements Parser<RouteCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the RoutetCommand | ||
* and returns an RouteCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public RouteCommand parse(String args) throws ParseException { | ||
try { | ||
Address address = ParserUtil.parseAddress(args); | ||
return new RouteCommand(address); | ||
} catch (ParseException pe) { | ||
throw new ParseException( | ||
String.format(Messages.MESSAGE_INVALID_COMMAND_FORMAT, RouteCommand.MESSAGE_USAGE), pe); | ||
} | ||
} | ||
} |
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
109 changes: 109 additions & 0 deletions
109
src/test/java/seedu/equipment/logic/commands/RouteCommandTest.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,109 @@ | ||
package seedu.equipment.logic.commands; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
import static seedu.equipment.testutil.TypicalEquipments.getTypicalAddressBook; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import seedu.equipment.logic.CommandHistory; | ||
import seedu.equipment.logic.commands.exceptions.CommandException; | ||
import seedu.equipment.logic.parser.RouteCommandParser; | ||
import seedu.equipment.logic.parser.exceptions.ParseException; | ||
import seedu.equipment.model.Model; | ||
import seedu.equipment.model.ModelManager; | ||
import seedu.equipment.model.UserPrefs; | ||
import seedu.equipment.model.equipment.Address; | ||
import seedu.equipment.model.equipment.Equipment; | ||
import seedu.equipment.testutil.EquipmentBuilder; | ||
|
||
public class RouteCommandTest { | ||
|
||
private Model model; | ||
private Model expectedModel; | ||
private Model emptyModel; | ||
private CommandHistory commandHistory = new CommandHistory(); | ||
|
||
@Before | ||
public void setUp() { | ||
model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
emptyModel = new ModelManager(); | ||
expectedModel = new ModelManager(model.getEquipmentManager(), new UserPrefs()); | ||
} | ||
|
||
@Test | ||
public void execute_routeCommandExecuteSuccessful() { | ||
try { | ||
CommandHistory expectedCommandHistory = new CommandHistory(commandHistory); | ||
Address testAddress = new Address("School of computing, NUS, Singapore"); | ||
CommandResult result = new RouteCommand(testAddress).execute(model, commandHistory); | ||
CommandResult expectedCommandResult = new CommandResult( | ||
RouteCommand.MESSAGE_ROUTE_EQUIPMENT_SUCCESS, false, false, false, true, testAddress); | ||
assertEquals(expectedCommandResult.toString(), result.toString()); | ||
assertEquals(expectedModel, model); | ||
assertEquals(expectedCommandHistory, commandHistory); | ||
} catch (CommandException ce) { | ||
fail("NO invalid command exception should be thrown."); | ||
} | ||
} | ||
|
||
@Test | ||
public void execute_routeCommandExecuteNoEquipmentExceptions() { | ||
try { | ||
Address testAddress = new Address("School of computing, NUS, Singapore"); | ||
CommandResult result = new RouteCommand(testAddress).execute(emptyModel, commandHistory); | ||
fail("Routing with empty equipment list should raise exception."); | ||
} catch (CommandException ce) { | ||
if (ce.getMessage() != RouteCommand.MESSAGE_NO_EQUIPMENTS_TO_ROUTE) { | ||
fail("Should display no equipment message to user."); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void execute_routeCommandExecuteTooManyEquipmentExceptions() { | ||
try { | ||
Address testAddress = new Address("School of computing, NUS, Singapore"); | ||
for (int i = 0; i < 16; i++) { | ||
Equipment newEquipment = new EquipmentBuilder().withName("PGPR" + i).withPhone("8482131") | ||
.withDate("10-05-2019").withAddress("PGPR, NUS, SINGAPORE") | ||
.withSerialNumber(String.valueOf(i)).build(); | ||
model.addEquipment(newEquipment); | ||
} | ||
CommandResult result = new RouteCommand(testAddress).execute(model, commandHistory); | ||
fail("Routing with 16 equipment list should raise exception."); | ||
} catch (CommandException ce) { | ||
if (ce.getMessage() != RouteCommand.MESSAGE_TOO_MANY_EQUIPMENTS_TO_ROUTE) { | ||
fail("Should display too many equipments message to user."); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void execute_routeCommandParserExecuteInvalidAddressExceptions() { | ||
try { | ||
CommandResult result = new RouteCommandParser().parse("").execute(model, commandHistory); | ||
fail("Routing with invalid starting address should raise exception."); | ||
} catch (CommandException ce) { | ||
fail("ParseException should be thrown but CommandException is thrown."); | ||
} catch (ParseException pe) { | ||
if (pe.getMessage() == null) { | ||
fail("Should display some error message."); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void execute_routeCommandEqualsTest() { | ||
try { | ||
RouteCommand command1 = new RouteCommandParser().parse("ABC"); | ||
RouteCommand command2 = new RouteCommandParser().parse("ABC"); | ||
if (!command1.equals(command2)) { | ||
fail("Route commands with same address should be equal."); | ||
} | ||
} catch (ParseException pe) { | ||
fail("No ParseException should be thrown."); | ||
} | ||
} | ||
} |
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.