From 0ad96f165b37609a562d98b69d6b4f0b07bbbed9 Mon Sep 17 00:00:00 2001 From: Kappaccinoh Date: Mon, 15 Apr 2024 15:08:33 +0800 Subject: [PATCH] add defensive coding techniques --- .../QueryDoctorAppointmentCommand.java | 20 ++++++++++++++----- .../QueryPatientAppointmentCommand.java | 15 +++++++++++--- .../logic/commands/QueryPatientCommand.java | 20 ++++++++++++++----- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/QueryDoctorAppointmentCommand.java b/src/main/java/seedu/address/logic/commands/QueryDoctorAppointmentCommand.java index 54265b9f6f6..30f16912e6a 100644 --- a/src/main/java/seedu/address/logic/commands/QueryDoctorAppointmentCommand.java +++ b/src/main/java/seedu/address/logic/commands/QueryDoctorAppointmentCommand.java @@ -2,6 +2,7 @@ import static java.util.Objects.requireNonNull; +import java.util.logging.Level; import java.util.logging.Logger; import seedu.address.commons.util.ToStringBuilder; @@ -11,7 +12,7 @@ /** * Represents a command for querying appointments for a specific doctor. - * The command searches for appointments of doctors whose NRICs/names contain any of the specified keywords + * The command searches for appointments of doctors whose NRICs contain any of the specified keywords * (case-insensitive) and displays them as a list with index numbers. */ public class QueryDoctorAppointmentCommand extends Command { @@ -28,17 +29,27 @@ public class QueryDoctorAppointmentCommand extends Command { private final AppointmentContainsDoctorPredicate predicate; + /** + * Constructs a QueryDoctorAppointmentCommand with the given predicate. + * + * @param predicate The predicate to be used for querying doctor appointments. + * @throws NullPointerException if the predicate is null. + */ public QueryDoctorAppointmentCommand(AppointmentContainsDoctorPredicate predicate) { + requireNonNull(predicate, "Predicate cannot be null in QueryDoctorAppointmentCommand constructor."); this.predicate = predicate; } @Override public CommandResult execute(Model model) { - requireNonNull(model); - logger.info("Executing QueryDoctorAppointmentCommand"); + requireNonNull(model, "Model cannot be null in execute method of QueryDoctorAppointmentCommand."); + + logger.log(Level.INFO, "Executing QueryDoctorAppointmentCommand"); + model.updateFilteredAppointmentList(predicate); int numberOfAppointments = model.getFilteredAppointmentList().size(); - logger.info("Number of appointments found: " + numberOfAppointments); + logger.log(Level.INFO, "Number of appointments found: " + numberOfAppointments); + return new CommandResult( String.format(Messages.MESSAGE_APPOINTMENTS_LISTED_OVERVIEW, numberOfAppointments)); } @@ -49,7 +60,6 @@ public boolean equals(Object other) { return true; } - // instanceof handles nulls if (!(other instanceof QueryDoctorAppointmentCommand)) { return false; } diff --git a/src/main/java/seedu/address/logic/commands/QueryPatientAppointmentCommand.java b/src/main/java/seedu/address/logic/commands/QueryPatientAppointmentCommand.java index 615f7d9476f..d111fcff09d 100644 --- a/src/main/java/seedu/address/logic/commands/QueryPatientAppointmentCommand.java +++ b/src/main/java/seedu/address/logic/commands/QueryPatientAppointmentCommand.java @@ -12,7 +12,7 @@ /** * Represents a command for querying appointments for a specific patient. - * The command searches for appointments of patients whose NRICs/names contain any of the specified keywords + * The command searches for appointments of patients whose NRICs contain any of the specified keywords * (case-insensitive) and displays them as a list with index numbers. */ public class QueryPatientAppointmentCommand extends Command { @@ -28,17 +28,27 @@ public class QueryPatientAppointmentCommand extends Command { private final AppointmentContainsPatientPredicate predicate; + /** + * Constructs a QueryPatientAppointmentCommand with the given predicate. + * + * @param predicate The predicate to be used for querying patient appointments. + * @throws NullPointerException if the predicate is null. + */ public QueryPatientAppointmentCommand(AppointmentContainsPatientPredicate predicate) { + requireNonNull(predicate, "Predicate cannot be null in QueryPatientAppointmentCommand constructor."); this.predicate = predicate; } @Override public CommandResult execute(Model model) { - requireNonNull(model); + requireNonNull(model, "Model cannot be null in execute method of QueryPatientAppointmentCommand."); + logger.log(Level.INFO, "Executing QueryPatientAppointmentCommand"); + model.updateFilteredAppointmentList(predicate); int numberOfAppointments = model.getFilteredAppointmentList().size(); logger.log(Level.INFO, "Number of appointments found: " + numberOfAppointments); + return new CommandResult( String.format(Messages.MESSAGE_APPOINTMENTS_LISTED_OVERVIEW, numberOfAppointments)); } @@ -49,7 +59,6 @@ public boolean equals(Object other) { return true; } - // instanceof handles nulls if (!(other instanceof QueryPatientAppointmentCommand)) { return false; } diff --git a/src/main/java/seedu/address/logic/commands/QueryPatientCommand.java b/src/main/java/seedu/address/logic/commands/QueryPatientCommand.java index 5cb6fb4d4b2..83d613cad53 100644 --- a/src/main/java/seedu/address/logic/commands/QueryPatientCommand.java +++ b/src/main/java/seedu/address/logic/commands/QueryPatientCommand.java @@ -11,9 +11,10 @@ import seedu.address.model.person.PatientContainsKeywordsPredicate; /** - * Queries and returns all patients whose name matches the input string. - * Keyword matching is case insensitive. - * Query more than one name at a time is supported + * Queries and returns all patients whose name, NRIC, DoB and phone number matches + * or substring matches the input string. + * Keyword matching is case-insensitive. + * Query more than one name, nric, date of birth and phone number at a time is supported */ public class QueryPatientCommand extends Command { @@ -28,17 +29,27 @@ public class QueryPatientCommand extends Command { private final PatientContainsKeywordsPredicate predicate; + /** + * Constructs a QueryPatientCommand with the given predicate. + * + * @param predicate The predicate to be used for querying patients. + * @throws NullPointerException if the predicate is null. + */ public QueryPatientCommand(PatientContainsKeywordsPredicate predicate) { + requireNonNull(predicate, "Predicate cannot be null in QueryPatientCommand constructor."); this.predicate = predicate; } @Override public CommandResult execute(Model model) { - requireNonNull(model); + requireNonNull(model, "Model cannot be null in execute method of QueryPatientCommand."); + logger.log(Level.INFO, "Executing QueryPatientCommand"); + model.updateFilteredPersonList(predicate); int numberOfPatients = model.getFilteredPersonList().size(); logger.log(Level.INFO, "Number of patients found: " + numberOfPatients); + return new CommandResult( String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, numberOfPatients)); } @@ -49,7 +60,6 @@ public boolean equals(Object other) { return true; } - // instanceof handles nulls if (!(other instanceof QueryPatientCommand)) { return false; }