forked from AY2425S1-CS2103T-F15-2/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
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 AY2425S1-CS2103T-F15-2#75 from JYL27/branch-FindCo…
…mmand Add Find Command (BACKEND)
- Loading branch information
Showing
18 changed files
with
590 additions
and
74 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
92 changes: 92 additions & 0 deletions
92
src/main/java/seedu/address/logic/commands/FindCommand.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,92 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.List; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.model.person.StudentId; | ||
|
||
/** | ||
* Finds a person identified using their Student ID and displays their details. | ||
*/ | ||
public class FindCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "find"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Finds the student identified by the Student ID used and displays their details.\n" | ||
+ "Parameters: " | ||
+ "ID\n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ "12345678"; | ||
|
||
public static final String MESSAGE_FIND_PERSON_SUCCESS = "Found Student: %1$s"; | ||
public static final String MESSAGE_PERSON_NOT_FOUND = "No student is found with Student ID: %1$s"; | ||
private final StudentId studentId; | ||
|
||
/** | ||
* Creates a FindCommand to find the person identified by the specified {@code StudentId}. | ||
* | ||
* @param studentId The student ID of the person to find. | ||
* @throws NullPointerException if the {@code studentId} is null. | ||
*/ | ||
public FindCommand(StudentId studentId) { | ||
requireNonNull(studentId); | ||
this.studentId = studentId; | ||
} | ||
|
||
/** | ||
* Executes the find command and displays the student identified by the given studentID. | ||
* | ||
* @param model the model that contains the data of persons | ||
* @return a CommandResult that shows the outcome of the command | ||
* @throws CommandException if the studentID is invalid or not found | ||
*/ | ||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
List<Person> lastShownList = model.getFilteredPersonList(); | ||
Person toFind = null; | ||
|
||
for (Person person : lastShownList) { | ||
if (person.getStudentId().equals(studentId)) { | ||
toFind = person; | ||
break; | ||
} | ||
} | ||
|
||
if (toFind == null) { | ||
throw new CommandException(String.format(MESSAGE_PERSON_NOT_FOUND, studentId)); | ||
} | ||
|
||
model.setPersonToDisplay(toFind); | ||
return new CommandResult(String.format(MESSAGE_FIND_PERSON_SUCCESS, Messages.format(toFind))); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof FindCommand)) { | ||
return false; | ||
} | ||
|
||
FindCommand otherFindCommand = (FindCommand) other; | ||
return studentId.equals(otherFindCommand.studentId); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("targetStudentId", studentId) | ||
.toString(); | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
src/main/java/seedu/address/logic/parser/FindCommandParser.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.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import seedu.address.logic.commands.FindCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.person.StudentId; | ||
|
||
/** | ||
* Parses input arguments and creates a new FindCommand object | ||
*/ | ||
public class FindCommandParser implements Parser<FindCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the FindCommand | ||
* and returns a FindCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public FindCommand parse(String args) throws ParseException { | ||
String trimmedArg = args.trim(); | ||
if (trimmedArg.contains(" ")) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
StudentId studentId = ParserUtil.parseStudentId(trimmedArg); | ||
return new FindCommand(studentId); | ||
} | ||
} |
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
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
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.