Skip to content

Commit

Permalink
Merge pull request #78 from techjay-c/add-patient-search
Browse files Browse the repository at this point in the history
Add patient search
  • Loading branch information
freddychenyouren2 authored Oct 17, 2023
2 parents 93573ae + 0694d21 commit 6d7e7ff
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,13 @@ public CommandResult execute(Model model) {
Predicate<Dentist> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
*
Expand All @@ -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<Patient> 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");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SearchPatientCommand> {

/**
* 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;
}
}
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ public interface Model {
*/
void updateFilteredPatientList(Predicate<Patient> predicate);

void updateFilteredPatientList(NameContainsKeywordsPredicate predicate);

/**
* Updates the filter of the filtered dentist list to filter by the given {@code predicate}.
*
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ public void updateFilteredPatientList(Predicate<Patient> predicate) {
filteredPatients.setPredicate(predicate);
}

@Override
public void updateFilteredPatientList(NameContainsKeywordsPredicate predicate) {
requireNonNull(predicate);
filteredPatients.setPredicate(predicate);
}

@Override
public void updateFilteredDentistList(Predicate<Dentist> predicate) {
requireNonNull(predicate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ public void updateFilteredPatientList(Predicate<Patient> 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<Dentist> predicate) {
throw new AssertionError("This method should not be called.");
Expand Down

0 comments on commit 6d7e7ff

Please sign in to comment.