Skip to content

Commit

Permalink
Add read command
Browse files Browse the repository at this point in the history
  • Loading branch information
NatLeong committed Mar 21, 2024
1 parent b582a4e commit bc41f3e
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/main/java/seedu/address/logic/commands/ReadCommand.java
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.ReadCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -77,6 +78,9 @@ public Command parseCommand(String userInput) throws ParseException {
case HelpCommand.COMMAND_WORD:
return new HelpCommand();

case ReadCommand.COMMAND_WORD:
return new ReadCommandParser().parse(arguments);

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/seedu/address/logic/parser/ReadCommandParser.java
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()));
}
}
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();
}
}

0 comments on commit bc41f3e

Please sign in to comment.