forked from nus-cs2103-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from chonguschonguschongus/branch-findAppoint…
…ment Add function to find Appointments by ICs of those involved
- Loading branch information
Showing
17 changed files
with
204 additions
and
5 deletions.
There are no files selected for viewing
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
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
68 changes: 68 additions & 0 deletions
68
src/main/java/seedu/address/logic/commands/FindAppointmentCommand.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,68 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
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.appointment.Appointment; | ||
|
||
/** | ||
* Finds and lists all persons in in address book whose attributes match the predicate. | ||
* Keyword matching is case insensitive. | ||
*/ | ||
public class FindAppointmentCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "find-appt"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all appointments who involve the " | ||
+ "the specified case-sensitive NRIC and displays them as a list with index numbers.\n" | ||
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n" | ||
+ "Example: " + COMMAND_WORD + " T1234567Z"; | ||
|
||
private final Predicate<Appointment> predicate; | ||
/** | ||
* Finds and lists all persons in address book whose attributes match the predicate. | ||
* Keyword matching is case insensitive. | ||
*/ | ||
public FindAppointmentCommand(Predicate<Appointment> predicate) { | ||
requireNonNull(predicate); | ||
this.predicate = predicate; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) { | ||
requireNonNull(model); | ||
model.updateFilteredAppointmentList(predicate); | ||
List<Appointment> searchList = new ArrayList<>(); | ||
searchList.addAll(model.getFilteredAppointmentList()); | ||
return new CommandResult( | ||
String.format(Messages.MESSAGE_APPOINTMENTS_FOUND_OVERVIEW, searchList.size())); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof FindAppointmentCommand)) { | ||
return false; | ||
} | ||
|
||
FindAppointmentCommand otherFindAppointmentCommand = (FindAppointmentCommand) other; | ||
return predicate.equals(otherFindAppointmentCommand.predicate); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("predicate", predicate) | ||
.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
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
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
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
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
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
40 changes: 40 additions & 0 deletions
40
src/main/java/seedu/address/logic/parser/FindAppointmentCommandParser.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,40 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import java.util.logging.Logger; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import seedu.address.commons.core.LogsCenter; | ||
import seedu.address.logic.commands.FindAppointmentCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.appointment.AppointmentIcPredicate; | ||
|
||
/** | ||
* Parses input arguments and creates a new FindCommand object | ||
*/ | ||
public class FindAppointmentCommandParser implements Parser<FindAppointmentCommand> { | ||
private static final Logger logger = LogsCenter.getLogger(FindAppointmentCommandParser.class); | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the FindCommand | ||
* and returns a FindCommand object for execution. | ||
* | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public FindAppointmentCommand parse(String args) throws ParseException { | ||
String trimmedArgs = args.trim(); | ||
Pattern nricPattern = Pattern.compile("^[ST]\\d{7}[A-Z]$"); | ||
Matcher nricMatcher = nricPattern.matcher(trimmedArgs); | ||
if (trimmedArgs.isEmpty() || !nricMatcher.matches()) { | ||
logger.warning("Can't parse find command - invalid input"); | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindAppointmentCommand.MESSAGE_USAGE)); | ||
} | ||
logger.info("Successfully parsed Find Command"); | ||
AppointmentIcPredicate icPredicate = new AppointmentIcPredicate(trimmedArgs); | ||
return new FindAppointmentCommand(icPredicate); | ||
} | ||
|
||
} |
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
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
46 changes: 46 additions & 0 deletions
46
src/main/java/seedu/address/model/appointment/AppointmentIcPredicate.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,46 @@ | ||
package seedu.address.model.appointment; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
|
||
/** | ||
* Checks if an {@code appointment} has a patient or doctor, matching | ||
* the query Ic {@code keywords}. | ||
*/ | ||
public class AppointmentIcPredicate implements Predicate<Appointment> { | ||
private final String keywords; | ||
|
||
public AppointmentIcPredicate(String keywords) { | ||
this.keywords = keywords; | ||
} | ||
|
||
@Override | ||
public boolean test(Appointment appointment) { | ||
if (keywords.equalsIgnoreCase(appointment.getPatient().toString())) { | ||
return true; | ||
} | ||
return keywords.equalsIgnoreCase(appointment.getDoctor().toString()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof AppointmentIcPredicate)) { | ||
return false; | ||
} | ||
|
||
AppointmentIcPredicate otherAppointmentIcPredicate = (AppointmentIcPredicate) other; | ||
return keywords.equals(otherAppointmentIcPredicate.keywords); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).add("keywords", keywords).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
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
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
31 changes: 31 additions & 0 deletions
31
src/test/java/seedu/address/logic/parser/FindAppointmentCommandParserTest.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,31 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; | ||
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.logic.commands.FindAppointmentCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.appointment.AppointmentIcPredicate; | ||
|
||
public class FindAppointmentCommandParserTest { | ||
private FindAppointmentCommandParser parser = new FindAppointmentCommandParser(); | ||
|
||
private String testInput = "T1111111G"; | ||
|
||
@Test | ||
public void parse_emptyArg_throwsParseException() { | ||
assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
FindAppointmentCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
@Test | ||
public void parse_validArgs_returnsFindCommand() throws ParseException { | ||
// no leading and trailing whitespaces | ||
FindAppointmentCommand expectedFindAppointmentCommand = | ||
new FindAppointmentCommand(new AppointmentIcPredicate(testInput)); | ||
assertParseSuccess(parser, "T1111111G", expectedFindAppointmentCommand); | ||
} | ||
} |