Skip to content

Commit

Permalink
Merge pull request #75 from techjay-c/add-dentist-search
Browse files Browse the repository at this point in the history
Update Search Dentist Feature
  • Loading branch information
ruth-lim authored Oct 17, 2023
2 parents 2a51a03 + 49e1cd7 commit 40ef99c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 8 deletions.
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.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.dentist.Dentist;

/**
* Represents a command to search for dentists whose names contain specified keywords.
Expand All @@ -17,21 +20,42 @@ public class SearchDentistCommand extends Command {
public static final String COMMAND_WORD = "search-dentist";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all dentists whose names contain any of "
+ "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n"
+ "the specified keywords (case-insensitive) and displays them as a list.\n"
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n"
+ "Example: " + COMMAND_WORD + " John Tan";

private final NameContainsKeywordsPredicate predicate;
private final SearchType searchType;
private NameContainsKeywordsPredicate predicate = null;
private long dentistID;

/**
* An enumeration representing different types of search criteria.
*/
public enum SearchType {
BY_ID,
BY_NAME
}

/**
* Constructs a SearchDentistCommand with the specified predicate.
* Constructs a SearchDentistCommand to search for dentists by their names using the provided predicate.
*
* @param predicate The predicate used for searching dentists.
* @param predicate The predicate used for searching dentists by name.
*/
public SearchDentistCommand(NameContainsKeywordsPredicate predicate) {
this.searchType = SearchType.BY_NAME;
this.predicate = predicate;
}

/**
* Constructs a SearchDentistCommand to search for a dentist by their ID.
*
* @param dentistID The ID of the dentist to search for.
*/
public SearchDentistCommand(long dentistID) {
this.searchType = SearchType.BY_ID;
this.dentistID = dentistID;
}

/**
* Executes the search operation on the provided model.
*
Expand All @@ -41,9 +65,27 @@ public SearchDentistCommand(NameContainsKeywordsPredicate predicate) {
@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredDentistList(predicate);
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredDentistList().size()));

// Solution adapted and inspired from https://chat.openai.com/share/c95e431a-4406-4a1e-b88f-cbff729f1cca
// Solution adapted and inspired from https://chat.openai.com/share/d392c3fe-0d09-40ef-8f28-e4e5e6ced48b
if (searchType == SearchType.BY_NAME) {

model.updateFilteredDentistList(predicate);
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredDentistList().size()));

} else if (searchType == SearchType.BY_ID) {

if (dentistID != 0) {
Predicate<Dentist> dentistIdPredicate = dentist -> dentist.getId() == dentistID;
model.updateFilteredDentistList(dentistIdPredicate);

return new CommandResult("Dentist search for ID " + dentistID + " found.");
} else {
return new CommandResult("Invalid search");
}
}
return new CommandResult("Invalid search");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,42 @@ public class SearchDentistCommandParser implements Parser<SearchDentistCommand>
* @throws ParseException if the user input does not conform the expected format
*/
public SearchDentistCommand parse(String args) throws ParseException {

String trimmedArgs = args.trim();
if (trimmedArgs.isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, SearchDentistCommand.MESSAGE_USAGE));
}

// check input to determine if search by ID or name
// Solution adapted and inspired from https://chat.openai.com/share/c95e431a-4406-4a1e-b88f-cbff729f1cca
String[] nameKeywords = trimmedArgs.split("\\s+");
SearchDentistCommand.SearchType searchType = determineSearchType(nameKeywords);

if (searchType == SearchDentistCommand.SearchType.BY_NAME) {
return new SearchDentistCommand(new NameContainsKeywordsPredicate(Arrays.asList(nameKeywords)));
} else if (searchType == SearchDentistCommand.SearchType.BY_ID) {

if (nameKeywords.length == 1) {
try {
long dentistID = Long.parseLong(nameKeywords[0]);
System.out.println("Searched dentistID: " + dentistID);
return new SearchDentistCommand(dentistID);
} catch (NumberFormatException e) {
throw new ParseException("Dentist 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 Dentist ID or name");
}
}

return new SearchDentistCommand(new NameContainsKeywordsPredicate(Arrays.asList(nameKeywords)));
private SearchDentistCommand.SearchType determineSearchType(String[] nameKeywords) {
if (nameKeywords.length == 1 && nameKeywords[0].matches("\\d+")) {
return SearchDentistCommand.SearchType.BY_ID;
}
return SearchDentistCommand.SearchType.BY_NAME;
}
}

0 comments on commit 40ef99c

Please sign in to comment.