forked from AY2324S2-CS2103T-T08-1/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.
- Loading branch information
NatLeong
committed
Mar 21, 2024
1 parent
b582a4e
commit bc41f3e
Showing
4 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
src/main/java/seedu/address/logic/commands/ReadCommand.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,84 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NRIC; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.Address; | ||
import seedu.address.model.person.DateOfBirth; | ||
import seedu.address.model.person.Name; | ||
import seedu.address.model.person.Nric; | ||
import seedu.address.model.person.NricContainsKeywordsPredicate; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.model.person.Phone; | ||
import seedu.address.model.person.Sex; | ||
import seedu.address.model.person.Status; | ||
|
||
/** | ||
* Reads the details of an existing person in the address book. | ||
*/ | ||
public class ReadCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "read"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Reads the details of the person identified " | ||
+ "by the NRIC specified. " | ||
+ "Example: " + COMMAND_WORD | ||
+ PREFIX_NRIC + "T0123456A"; | ||
|
||
public static final String MESSAGE_READ_PERSON_SUCCESS = "Read Person: %1$s"; | ||
public static final String MESSAGE_NO_PERSON = "There is no such person with this NRIC."; | ||
public static final String MESSAGE_NOT_READ = "NRIC to be specified."; | ||
private final Nric nric; | ||
|
||
/** | ||
* @param nric of the person to read | ||
*/ | ||
public ReadCommand(Nric nric) { | ||
requireNonNull(nric); | ||
|
||
this.nric = nric; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
Person sample = new Person(this.nric, new Name("sample"), new Phone("12345678"), new Address("123 Lane"), | ||
new DateOfBirth("1900-01-01"), new Sex("F"), new Status("HEALTHY")); | ||
// Todo: change to find if person exists using only NRIC | ||
if (model.hasPerson(sample)) { | ||
throw new CommandException(MESSAGE_NO_PERSON); | ||
} | ||
|
||
model.updateFilteredPersonList(new NricContainsKeywordsPredicate(nric.toString())); | ||
Person readPerson = model.getFilteredPersonList().get(0); | ||
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); | ||
|
||
return new CommandResult(String.format(MESSAGE_READ_PERSON_SUCCESS, readPerson.toDetailedString())); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof ReadCommand)) { | ||
return false; | ||
} | ||
|
||
ReadCommand otherReadCommand = (ReadCommand) other; | ||
return this.nric.equals(otherReadCommand.nric); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("nric", nric) | ||
.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
28 changes: 28 additions & 0 deletions
28
src/main/java/seedu/address/logic/parser/ReadCommandParser.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,28 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NRIC; | ||
|
||
import seedu.address.logic.commands.ReadCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new ReadCommand object | ||
*/ | ||
public class ReadCommandParser { | ||
/** | ||
* Parses the given {@code String} of argument in the context of the ReadCommand | ||
* and returns an ReadCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public ReadCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NRIC); | ||
|
||
if (argMultimap.getValue(PREFIX_NRIC).isEmpty()) { | ||
throw new ParseException(ReadCommand.MESSAGE_NOT_READ); | ||
} | ||
|
||
return new ReadCommand(ParserUtil.parseNric(argMultimap.getValue(PREFIX_NRIC).get())); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/seedu/address/model/person/NricContainsKeywordsPredicate.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,42 @@ | ||
package seedu.address.model.person; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.StringUtil; | ||
import seedu.address.commons.util.ToStringBuilder; | ||
|
||
/** | ||
* Tests that a {@code Person}'s {@code Nric} matches any of the keywords given. | ||
*/ | ||
public class NricContainsKeywordsPredicate implements Predicate<Person> { | ||
private final String keywords; | ||
|
||
public NricContainsKeywordsPredicate(String keywords) { | ||
this.keywords = keywords; | ||
} | ||
|
||
@Override | ||
public boolean test(Person person) { | ||
return StringUtil.containsWordIgnoreCase(person.getNric().toString(), keywords); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof NricContainsKeywordsPredicate)) { | ||
return false; | ||
} | ||
|
||
NricContainsKeywordsPredicate otherNricContainsKeywordsPredicate = (NricContainsKeywordsPredicate) other; | ||
return keywords.equals(otherNricContainsKeywordsPredicate.keywords); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).add("keywords", keywords).toString(); | ||
} | ||
} |