Skip to content
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 password for deletion #96

Merged
merged 5 commits into from
Oct 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 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,6 @@ public class Messages {
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_LOAN_DISPLAYED_INDEX = "The loan index provided is invalid";
public static final String MESSAGE_LOANS_LISTED_OVERVIEW = "%1$d loans listed!";
public static final String MESSAGE_INVALID_PASSWORD = "The password provided is incorrect!";

}
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class DeleteCommand extends Command {

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes the loan identified by the index number used in the displayed loan list.\n"
wn marked this conversation as resolved.
Show resolved Hide resolved
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";
+ "Parameters: INDEX (must be a positive integer) and PASSWORD\n"
+ "Example: " + COMMAND_WORD + " i/1 x/12345";
Copy link
Collaborator

@OrangeJuice7 OrangeJuice7 Oct 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "parameters" and "example" are not consistent with the other commands. Either do
"Parameters: INDEX (must be a positive integer) PASSWORD\n"
"Example: " + COMMAND_WORD + " 1 12345"
or
"Parameters: " + PREFIX_INDEX + "INDEX (must be a positive integer) " + PREFIX_PASSWORD + "PASSWORD\n"
"Example: " + COMMAND_WORD + " i/1 x/12345"


public static final String MESSAGE_DELETE_LOAN_SUCCESS = "Deleted Loan: %1$s";

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class CliSyntax {

/* Prefix definitions */
public static final Prefix PREFIX_NAME = new Prefix("n/");
public static final Prefix PREFIX_INDEX = new Prefix("i/");
public static final Prefix PREFIX_PASSWORD = new Prefix("x/");
public static final Prefix PREFIX_PHONE = new Prefix("p/");
public static final Prefix PREFIX_EMAIL = new Prefix("e/");
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
Expand Down
30 changes: 23 additions & 7 deletions src/main/java/seedu/address/logic/parser/DeleteCommandParser.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_PASSWORD;
import static seedu.address.logic.parser.CliSyntax.PREFIX_INDEX;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PASSWORD;

import java.util.stream.Stream;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new DeleteCommand object
*/
public class DeleteCommandParser implements Parser<DeleteCommand> {
private static String PASSWORD = "a12345"; // Currently hard-coded. To add command that allows setting of password.

/**
* Parses the given {@code String} of arguments in the context of the DeleteCommand
* and returns an DeleteCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public DeleteCommand parse(String args) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
return new DeleteCommand(index);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE), pe);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_INDEX, PREFIX_PASSWORD);
if (!arePrefixesPresent(argMultimap, PREFIX_INDEX, PREFIX_PASSWORD)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
}
if (!PASSWORD.equals(argMultimap.getValue(PREFIX_PASSWORD).get())) {
throw new ParseException(String.format(MESSAGE_INVALID_PASSWORD, DeleteCommand.MESSAGE_USAGE));
}
return new DeleteCommand(ParserUtil.parseIndex(argMultimap.getValue(PREFIX_INDEX).get()));
}

/**
* 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());
}

}
2 changes: 1 addition & 1 deletion src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void execute_invalidCommandFormat_throwsParseException() {

@Test
public void execute_commandExecutionError_throwsCommandException() {
String deleteCommand = "delete 9";
String deleteCommand = "delete i/9 x/a12345";
assertCommandException(deleteCommand, MESSAGE_INVALID_LOAN_DISPLAYED_INDEX);
assertHistoryCorrect(deleteCommand);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void parseCommand_clear() throws Exception {
@Test
public void parseCommand_delete() throws Exception {
DeleteCommand command = (DeleteCommand) parser.parseCommand(
DeleteCommand.COMMAND_WORD + " " + INDEX_FIRST_LOAN.getOneBased());
DeleteCommand.COMMAND_WORD + " i/" + INDEX_FIRST_LOAN.getOneBased() + " x/a12345");
assertEquals(new DeleteCommand(INDEX_FIRST_LOAN), command);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class DeleteCommandParserTest {

@Test
public void parse_validArgs_returnsDeleteCommand() {
assertParseSuccess(parser, "1", new DeleteCommand(INDEX_FIRST_LOAN));
assertParseSuccess(parser, " i/1 x/a12345", new DeleteCommand(INDEX_FIRST_LOAN));
}

@Test
Expand Down
26 changes: 16 additions & 10 deletions src/test/java/systemtests/DeleteCommandSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ public void delete() {

/* Case: delete the first loan in the list, command with leading spaces and trailing spaces -> deleted */
Model expectedModel = getModel();
String command = " " + DeleteCommand.COMMAND_WORD + " " + INDEX_FIRST_LOAN.getOneBased() + " ";
String command = " "
+ DeleteCommand.COMMAND_WORD
+ " i/"
+ INDEX_FIRST_LOAN.getOneBased()
+ " x/a12345"
+ " ";
Loan deletedLoan = removeLoan(expectedModel, INDEX_FIRST_LOAN);
String expectedResultMessage = String.format(MESSAGE_DELETE_LOAN_SUCCESS, deletedLoan);
assertCommandSuccess(command, expectedModel, expectedResultMessage);
Expand Down Expand Up @@ -69,7 +74,7 @@ public void delete() {
*/
showLoansWithName(KEYWORD_MATCHING_MEIER);
int invalidIndex = getModel().getAddressBook().getLoanList().size();
command = DeleteCommand.COMMAND_WORD + " " + invalidIndex;
command = DeleteCommand.COMMAND_WORD + " i/" + invalidIndex + " x/a12345";
assertCommandFailure(command, MESSAGE_INVALID_LOAN_DISPLAYED_INDEX);

/* --------------------- Performing delete operation while a loan card is selected ------------------------ */
Expand All @@ -80,32 +85,32 @@ public void delete() {
Index selectedIndex = getLastIndex(expectedModel);
Index expectedIndex = Index.fromZeroBased(selectedIndex.getZeroBased() - 1);
selectLoan(selectedIndex);
command = DeleteCommand.COMMAND_WORD + " " + selectedIndex.getOneBased();
command = DeleteCommand.COMMAND_WORD + " i/" + selectedIndex.getOneBased() + " x/a12345";
deletedLoan = removeLoan(expectedModel, selectedIndex);
expectedResultMessage = String.format(MESSAGE_DELETE_LOAN_SUCCESS, deletedLoan);
assertCommandSuccess(command, expectedModel, expectedResultMessage, expectedIndex);

/* --------------------------------- Performing invalid delete operation ------------------------------------ */

/* Case: invalid index (0) -> rejected */
command = DeleteCommand.COMMAND_WORD + " 0";
command = DeleteCommand.COMMAND_WORD + " i/0";
assertCommandFailure(command, MESSAGE_INVALID_DELETE_COMMAND_FORMAT);

/* Case: invalid index (-1) -> rejected */
command = DeleteCommand.COMMAND_WORD + " -1";
command = DeleteCommand.COMMAND_WORD + " i/-1";
assertCommandFailure(command, MESSAGE_INVALID_DELETE_COMMAND_FORMAT);

/* Case: invalid index (size + 1) -> rejected */
Index outOfBoundsIndex = Index.fromOneBased(
getModel().getAddressBook().getLoanList().size() + 1);
command = DeleteCommand.COMMAND_WORD + " " + outOfBoundsIndex.getOneBased();
command = DeleteCommand.COMMAND_WORD + " i/" + outOfBoundsIndex.getOneBased() + " x/a12345";
assertCommandFailure(command, MESSAGE_INVALID_LOAN_DISPLAYED_INDEX);

/* Case: invalid arguments (alphabets) -> rejected */
assertCommandFailure(DeleteCommand.COMMAND_WORD + " abc", MESSAGE_INVALID_DELETE_COMMAND_FORMAT);
assertCommandFailure(DeleteCommand.COMMAND_WORD + " i/abc", MESSAGE_INVALID_DELETE_COMMAND_FORMAT);

/* Case: invalid arguments (extra argument) -> rejected */
assertCommandFailure(DeleteCommand.COMMAND_WORD + " 1 abc", MESSAGE_INVALID_DELETE_COMMAND_FORMAT);
assertCommandFailure(DeleteCommand.COMMAND_WORD + " i/1 abc", MESSAGE_INVALID_DELETE_COMMAND_FORMAT);

/* Case: mixed case command word -> rejected */
assertCommandFailure("DelETE 1", MESSAGE_UNKNOWN_COMMAND);
Expand All @@ -131,8 +136,9 @@ private void assertCommandSuccess(Index toDelete) {
Loan deletedLoan = removeLoan(expectedModel, toDelete);
String expectedResultMessage = String.format(MESSAGE_DELETE_LOAN_SUCCESS, deletedLoan);

assertCommandSuccess(
DeleteCommand.COMMAND_WORD + " " + toDelete.getOneBased(), expectedModel, expectedResultMessage);
assertCommandSuccess(DeleteCommand.COMMAND_WORD
+ " i/" + toDelete.getOneBased()
+ " x/a12345", expectedModel, expectedResultMessage);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/systemtests/FindCommandSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void find() {
assertCommandFailure(command, expectedResultMessage);

/* Case: find same loans in address book after deleting 1 of them -> 1 loan found */
executeCommand(DeleteCommand.COMMAND_WORD + " 1");
executeCommand(DeleteCommand.COMMAND_WORD + " i/1 x/a12345");
assertFalse(getModel().getAddressBook().getLoanList().contains(BENSON));
command = FindCommand.COMMAND_WORD + " " + KEYWORD_MATCHING_MEIER;
expectedModel = getModel();
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/systemtests/HelpCommandSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void openHelpWindow() {

// assert that the status bar too is updated correctly while the help window is open
// note: the select command tested above does not update the status bar
executeCommand(DeleteCommand.COMMAND_WORD + " " + INDEX_FIRST_LOAN.getOneBased());
executeCommand(DeleteCommand.COMMAND_WORD + " i/" + INDEX_FIRST_LOAN.getOneBased() + " x/a12345");
assertNotEquals(StatusBarFooter.SYNC_STATUS_INITIAL, getStatusBarFooter().getSyncStatus());
}

Expand Down