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

Change MoreInfoCommand #278

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

import java.util.List;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.ui.MoreInfoWindow;

Expand All @@ -22,7 +22,7 @@
* followed by the client's name. For example, {@code moreinfo n/Amy}.
*
* Command Format:
* moreinfo n/client_name
* moreinfo n/client_index
*
* Attributes
* {@code COMMAND_WORD} - The command word to trigger this command.
Expand All @@ -36,33 +36,27 @@
public class MoreInfoCommand extends Command {
public static final String COMMAND_WORD = "moreinfo";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Open a window to more information "
public static final String MESSAGE_USAGE = COMMAND_WORD + "CLIENT_INDEX" + ": Open a window to more information "
+ "about the client.\n"
+ "Example: " + COMMAND_WORD + " Amy";
+ "Example: " + COMMAND_WORD + " 1";

public static final String SHOWING_MORE_INFO_MESSAGE = "Opened window for client's information.";

private final Name targetName;
private final Index targetIndex;

public MoreInfoCommand(Name targetName) {
this.targetName = targetName;
public MoreInfoCommand(Index targetIndex) {
this.targetIndex = targetIndex;
}
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

Person personMoreInfo = model.getPersonByName(targetName);

if (!lastShownList.contains(personMoreInfo)) {
String closestMatch = findClosestMatch(targetName.toString(), lastShownList);

if (closestMatch != null) {
throw new CommandException(String.format(Messages.MESSAGE_SUGGESTION, closestMatch));
} else {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_INPUT);
}
int zeroBasedPerson = targetIndex.getZeroBased();
if (zeroBasedPerson >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
Person personMoreInfo = lastShownList.get(zeroBasedPerson);

MoreInfoWindow moreInfoWindow = new MoreInfoWindow(personMoreInfo);
moreInfoWindow.show();
Expand All @@ -80,13 +74,13 @@ public boolean equals(Object other) {
return false;
}

return targetName.equals(otherMoreInfoCommand.targetName);
return targetIndex.equals(otherMoreInfoCommand.targetIndex);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("targetName", targetName)
.add("targetIndex", targetIndex)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.clientcommands.MoreInfoCommand;
import seedu.address.logic.parser.ArgumentMultimap;
import seedu.address.logic.parser.ArgumentTokenizer;
import seedu.address.logic.parser.Parser;
import seedu.address.logic.parser.ParserUtil;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Name;

/**
* Parses input arguments and creates a new MoreInfoCommand object
Expand All @@ -24,8 +24,8 @@ public MoreInfoCommand parse(String args) throws ParseException {

try {
// Parse the name and return the MoreInfoCommand
Name name = ParserUtil.parseName(argumentMultimap.getPreamble());
return new MoreInfoCommand(name);
Index index = ParserUtil.parseIndex(argumentMultimap.getPreamble());
return new MoreInfoCommand(index);
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
MoreInfoCommand.MESSAGE_USAGE), pe);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ public EditListingCommand parse(String args) throws ParseException {
editListingDescriptor.setSellerIndex(sellerIndex);
}

if (!editListingDescriptor.isAnyFieldEdited()) {
throw new ParseException(EditListingCommand.MESSAGE_NOT_EDITED);
}

return new EditListingCommand(currentListingIndex, editListingDescriptor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.showPersonWithName;
import static seedu.address.testutil.TypicalPersons.ALICE;
import static seedu.address.testutil.TypicalPersons.BENSON;
import static seedu.address.logic.commands.CommandTestUtil.showPersonAtIndex;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;
import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON;
import static seedu.address.testutil.TypicalIndexes.PERSON_INDEX_OUT_OF_BOUNDS;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;
import static seedu.address.testutil.TypicalPersons.getTypicalNames;

import java.util.List;
import java.util.Random;

import org.junit.jupiter.api.Test;

Expand All @@ -20,58 +17,35 @@
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;

public class MoreInfoCommandTest {
private static final Name DO_NOT_EXIST_NAME = new Name("DO NOT EXIST NAME");
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs(), new Listings());
@Test
public void execute_invalidNameUnfilteredList_throwsCommandException() {
MoreInfoCommand moreInfoCommand = new MoreInfoCommand(DO_NOT_EXIST_NAME);
public void execute_clientIndexOutOfBoundsUnfilteredList_throwsCommandException() {
MoreInfoCommand moreInfoCommand = new MoreInfoCommand(PERSON_INDEX_OUT_OF_BOUNDS);

assertCommandFailure(moreInfoCommand, model, Messages.MESSAGE_INVALID_PERSON_INPUT);
assertCommandFailure(moreInfoCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

@Test
public void execute_invalidNameFilteredList_throwsCommandException() {
Random random = new Random();
List<Name> typicalNames = getTypicalNames();
int randomIndex = random.nextInt(typicalNames.size() - 2);
showPersonWithName(model, typicalNames.get(randomIndex));

MoreInfoCommand moreInfoCommand = new MoreInfoCommand(typicalNames
.get(randomIndex + 1));
public void execute_clientIndexOutOfBoundsFilteredList_throwsCommandException() {
showPersonAtIndex(model, INDEX_FIRST_PERSON);

assertCommandFailure(moreInfoCommand, model, Messages.MESSAGE_INVALID_PERSON_INPUT);
}
@Test
public void execute_subName_throwsCommandException() {
Random random = new Random();
List<Name> typicalNames = getTypicalNames();
int randomIndex = random.nextInt(typicalNames.size() - 1);
Person personToMoreInfo = model.getPersonByName(typicalNames.get(randomIndex));
String personToMoreInfoNameString = personToMoreInfo.getName().toString();
Name subNamePersonToMoreInfo =
new Name(personToMoreInfoNameString
.substring(0, personToMoreInfoNameString.length() - 1));
MoreInfoCommand moreInfoCommand =
new MoreInfoCommand(subNamePersonToMoreInfo);
MoreInfoCommand moreInfoCommand = new MoreInfoCommand(INDEX_SECOND_PERSON);

assertCommandFailure(moreInfoCommand, model,
String.format(Messages.MESSAGE_SUGGESTION, personToMoreInfo.getName()));
assertCommandFailure(moreInfoCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

@Test
public void equals() {
MoreInfoCommand moreInfoFirstCommand = new MoreInfoCommand(ALICE.getName());
MoreInfoCommand moreInfoSecondCommand = new MoreInfoCommand(BENSON.getName());
MoreInfoCommand moreInfoFirstCommand = new MoreInfoCommand(INDEX_FIRST_PERSON);
MoreInfoCommand moreInfoSecondCommand = new MoreInfoCommand(INDEX_SECOND_PERSON);

// same object -> returns true
assertTrue(moreInfoFirstCommand.equals(moreInfoFirstCommand));

// same values -> returns true
MoreInfoCommand moreInfoFirstCommandCopy = new MoreInfoCommand(ALICE.getName());
MoreInfoCommand moreInfoFirstCommandCopy = new MoreInfoCommand(INDEX_FIRST_PERSON);
assertTrue(moreInfoFirstCommand.equals(moreInfoFirstCommandCopy));

// different types -> returns false
Expand All @@ -86,8 +60,8 @@ public void equals() {

@Test
public void toStringMethod() {
MoreInfoCommand moreInfoCommand = new MoreInfoCommand(ALICE.getName());
String expected = MoreInfoCommand.class.getCanonicalName() + "{targetName=" + ALICE.getName() + "}";
MoreInfoCommand moreInfoCommand = new MoreInfoCommand(INDEX_FIRST_PERSON);
String expected = MoreInfoCommand.class.getCanonicalName() + "{targetIndex=" + INDEX_FIRST_PERSON + "}";
assertEquals(expected, moreInfoCommand.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;
import static seedu.address.testutil.TypicalPersons.ALICE;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;

import org.junit.jupiter.api.Test;

Expand All @@ -14,7 +14,7 @@ public class MoreInfoCommandParserTest {

@Test
public void parse_validArgs_returnsMoreInfoCommand() {
assertParseSuccess(parser, "Alice Pauline", new MoreInfoCommand(ALICE.getName()));
assertParseSuccess(parser, "1", new MoreInfoCommand(INDEX_FIRST_PERSON));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public void parse_emptyArg_throwsParseException() {
assertThrows(ParseException.class, () -> parser.parse(""),
String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditListingCommand.MESSAGE_USAGE));
}

@Test
public void parse_multipleNames_throwsParseException() {
String userInput = EditListingCommand.COMMAND_WORD + " "
Expand Down
9 changes: 3 additions & 6 deletions src/test/java/seedu/address/ui/MoreInfoControllerUiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import seedu.address.MainApp;
import seedu.address.model.person.Name;

public class MoreInfoControllerUiTest extends ApplicationTest {
private MainApp app;
private Name name;
private String index;

@BeforeEach
public void setUp() throws Exception {
Expand All @@ -31,13 +30,11 @@ public void setUp() throws Exception {
WaitForAsyncUtils.waitForFxEvents(20);
FxRobot robot = new FxRobot();
if (robot.lookup("#name").tryQuery().isEmpty()) {
name = new Name("Jackson");
robot.clickOn("#commandBoxPlaceholder");
robot.write("buyer n/Jackson p/98294924 e/jackson@gmail.com");
robot.type(KeyCode.ENTER);
} else {
Label nameArea = robot.lookup("#name").query();
name = new Name(nameArea.getText());
}

}
Expand All @@ -49,7 +46,7 @@ public void tearDown() throws TimeoutException {
public void openMoreInfoWindowUponCommandAndCloses_success() {
FxRobot robot = new FxRobot();
robot.clickOn("#commandBoxPlaceholder");
robot.write("moreinfo " + name);
robot.write("moreinfo 1");
robot.type(KeyCode.ENTER);
assertTrue(robot.lookup("#remarkInput").tryQuery().isPresent(),
"Command opens more info window successfully");
Expand All @@ -62,7 +59,7 @@ public void openMoreInfoWindowUponCommandAndCloses_success() {
public void handleRemarkInputCorrectly_success() {
FxRobot robot = new FxRobot();
robot.clickOn("#commandBoxPlaceholder");
robot.write("moreinfo " + name);
robot.write("moreinfo 1");
robot.type(KeyCode.ENTER);
robot.clickOn("#remarkInput");
robot.write("Test");
Expand Down