diff --git a/src/main/java/seedu/address/logic/commands/SearchDentistCommand.java b/src/main/java/seedu/address/logic/commands/SearchDentistCommand.java index a5f550df111..b9e32ef51d9 100644 --- a/src/main/java/seedu/address/logic/commands/SearchDentistCommand.java +++ b/src/main/java/seedu/address/logic/commands/SearchDentistCommand.java @@ -80,9 +80,13 @@ public CommandResult execute(Model model) { Predicate dentistIdPredicate = dentist -> dentist.getId() == dentistID; model.updateFilteredDentistList(dentistIdPredicate); - return new CommandResult("Dentist search for ID " + dentistID + " found."); + if (model.getFilteredDentistList().isEmpty()) { + return new CommandResult("No dentist found with dentist ID " + dentistID); + } else { + return new CommandResult("Dentist with dentist ID " + dentistID + " found."); + } } else { - return new CommandResult("Invalid search"); + return new CommandResult("No dentist with dentist ID 0"); } } return new CommandResult("Invalid search"); diff --git a/src/main/java/seedu/address/logic/commands/SearchPatientCommand.java b/src/main/java/seedu/address/logic/commands/SearchPatientCommand.java index 4396634147d..6aef6b38717 100644 --- a/src/main/java/seedu/address/logic/commands/SearchPatientCommand.java +++ b/src/main/java/seedu/address/logic/commands/SearchPatientCommand.java @@ -2,10 +2,13 @@ import static java.util.Objects.requireNonNull; +import java.util.function.Predicate; + import seedu.address.commons.util.ToStringBuilder; import seedu.address.logic.Messages; import seedu.address.model.Model; -import seedu.address.model.PatientNameContainsKeywordsPredicate; +import seedu.address.model.person.NameContainsKeywordsPredicate; +import seedu.address.model.person.patients.Patient; /** * Represents a command to search for patients whose names contain specified keywords. @@ -21,17 +24,38 @@ public class SearchPatientCommand extends Command { + "Parameters: KEYWORD [MORE_KEYWORDS]...\n" + "Example: " + COMMAND_WORD + " John Tan"; - private final PatientNameContainsKeywordsPredicate predicate; + private final SearchType searchType; + private long patientID; + private NameContainsKeywordsPredicate predicate; + + /** + * An enumeration representing different types of search criteria. + */ + public enum SearchType { + BY_ID, + BY_NAME + } /** * Constructs a SearchPatientCommand with the specified predicate. * * @param predicate The predicate used for searching patients. */ - public SearchPatientCommand(PatientNameContainsKeywordsPredicate predicate) { + public SearchPatientCommand(NameContainsKeywordsPredicate predicate) { + this.searchType = SearchType.BY_NAME; this.predicate = predicate; } + /** + * Constructs a SearchPatientCommand to search for a patient by their ID. + * + * @param patientID The ID of the patient to search for. + */ + public SearchPatientCommand(long patientID) { + this.searchType = SearchType.BY_ID; + this.patientID = patientID; + } + /** * Executes the search operation on the provided model. * @@ -41,9 +65,29 @@ public SearchPatientCommand(PatientNameContainsKeywordsPredicate predicate) { @Override public CommandResult execute(Model model) { requireNonNull(model); - model.updateFilteredPatientList(predicate); - return new CommandResult( - String.format(Messages.MESSAGE_PATIENTS_LISTED_OVERVIEW, model.getFilteredPatientList().size())); + + if (searchType == SearchPatientCommand.SearchType.BY_NAME) { + + model.updateFilteredPatientList(predicate); + return new CommandResult( + String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPatientList().size())); + + } else if (searchType == SearchPatientCommand.SearchType.BY_ID) { + + if (patientID != 0) { + Predicate patientIdPredicate = patient -> patient.getId() == patientID; + model.updateFilteredPatientList(patientIdPredicate); + + if (model.getFilteredPatientList().isEmpty()) { + return new CommandResult("No patient found with patient ID " + patientID); + } else { + return new CommandResult("Patient with patient ID " + patientID + " found."); + } + } else { + return new CommandResult("No patient with patient ID 0"); + } + } + return new CommandResult("Invalid search"); } /** diff --git a/src/main/java/seedu/address/logic/parser/SearchPatientCommandParser.java b/src/main/java/seedu/address/logic/parser/SearchPatientCommandParser.java index d646b593677..2bb42a0a555 100644 --- a/src/main/java/seedu/address/logic/parser/SearchPatientCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/SearchPatientCommandParser.java @@ -6,28 +6,55 @@ import seedu.address.logic.commands.SearchPatientCommand; import seedu.address.logic.parser.exceptions.ParseException; -import seedu.address.model.PatientNameContainsKeywordsPredicate; +import seedu.address.model.person.NameContainsKeywordsPredicate; /** - * Parses input arguments and creates a new SearchDentistCommand object + * Parses input arguments and creates a new SearchPatientCommand object */ public class SearchPatientCommandParser implements Parser { /** - * Parses the given {@code String} of arguments in the context of the SearchDentistCommand - * and returns a SearchDentistCommand object for execution. + * Parses the given {@code String} of arguments in the context of the SearchPatientCommand + * and returns a SearchPatientCommand object for execution. * * @throws ParseException if the user input does not conform the expected format */ public SearchPatientCommand parse(String args) throws ParseException { + String trimmedArgs = args.trim(); if (trimmedArgs.isEmpty()) { throw new ParseException( String.format(MESSAGE_INVALID_COMMAND_FORMAT, SearchPatientCommand.MESSAGE_USAGE)); } + // check input to determine if search by ID or name String[] nameKeywords = trimmedArgs.split("\\s+"); + SearchPatientCommand.SearchType searchType = determineSearchType(nameKeywords); + + if (searchType == SearchPatientCommand.SearchType.BY_NAME) { + return new SearchPatientCommand(new NameContainsKeywordsPredicate(Arrays.asList(nameKeywords))); + } else if (searchType == SearchPatientCommand.SearchType.BY_ID) { + + if (nameKeywords.length == 1) { + try { + long patientID = Long.parseLong(nameKeywords[0]); + System.out.println("Searched patientID: " + patientID); + return new SearchPatientCommand(patientID); + } catch (NumberFormatException e) { + throw new ParseException("Patient ID should be a valid number"); + } + } else { + throw new ParseException("Invalid search format. Enter a number to search for."); + } + } else { + throw new ParseException("Search cannot be performed. Enter a valid Patient ID or name"); + } + } - return new SearchPatientCommand(new PatientNameContainsKeywordsPredicate(Arrays.asList(nameKeywords))); + private SearchPatientCommand.SearchType determineSearchType(String[] nameKeywords) { + if (nameKeywords.length == 1 && nameKeywords[0].matches("\\d+")) { + return SearchPatientCommand.SearchType.BY_ID; + } + return SearchPatientCommand.SearchType.BY_NAME; } } diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 3b28d4698a8..149f1150e12 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -154,6 +154,8 @@ public interface Model { */ void updateFilteredPatientList(Predicate predicate); + void updateFilteredPatientList(NameContainsKeywordsPredicate predicate); + /** * Updates the filter of the filtered dentist list to filter by the given {@code predicate}. * diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 4f4c4e16442..2e6fe2321a1 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -234,6 +234,12 @@ public void updateFilteredPatientList(Predicate predicate) { filteredPatients.setPredicate(predicate); } + @Override + public void updateFilteredPatientList(NameContainsKeywordsPredicate predicate) { + requireNonNull(predicate); + filteredPatients.setPredicate(predicate); + } + @Override public void updateFilteredDentistList(Predicate predicate) { requireNonNull(predicate); diff --git a/src/test/java/seedu/address/logic/commands/AddCommandTest.java b/src/test/java/seedu/address/logic/commands/AddCommandTest.java index 68960c27ef0..a7b09a1d87e 100644 --- a/src/test/java/seedu/address/logic/commands/AddCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddCommandTest.java @@ -178,6 +178,11 @@ public void updateFilteredPatientList(Predicate predicate) { throw new AssertionError("This method should not be called."); } + @Override + public void updateFilteredPatientList(NameContainsKeywordsPredicate predicate) { + throw new AssertionError("This method should not be called."); + } + @Override public void updateFilteredDentistList(Predicate predicate) { throw new AssertionError("This method should not be called.");