From 02d21e9dcd1126268e5b98ce5dfd8c91b77b789e Mon Sep 17 00:00:00 2001 From: alfaloo Date: Mon, 15 Apr 2024 14:31:00 +0800 Subject: [PATCH 1/2] Add logging and assertions --- .../seedu/address/logic/commands/AddDoctorCommand.java | 4 ++-- .../seedu/address/logic/commands/AddPatientCommand.java | 7 +++++++ .../java/seedu/address/logic/commands/DeleteCommand.java | 1 + .../seedu/address/logic/commands/QueryDoctorCommand.java | 9 +++++++++ .../address/model/appointment/AppointmentDateTime.java | 1 + src/main/java/seedu/address/model/person/DoB.java | 1 + src/main/java/seedu/address/model/person/Nric.java | 1 + src/main/java/seedu/address/model/person/Phone.java | 1 + 8 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/AddDoctorCommand.java b/src/main/java/seedu/address/logic/commands/AddDoctorCommand.java index 1561a9e2c0a..35db815f331 100644 --- a/src/main/java/seedu/address/logic/commands/AddDoctorCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddDoctorCommand.java @@ -9,7 +9,6 @@ import java.util.logging.Level; import java.util.logging.Logger; -import seedu.address.Main; import seedu.address.commons.core.LogsCenter; import seedu.address.commons.util.ToStringBuilder; import seedu.address.logic.Messages; @@ -38,7 +37,7 @@ public class AddDoctorCommand extends Command { public static final String MESSAGE_SUCCESS = "New doctor added: %1$s"; public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book"; - private static Logger logger = LogsCenter.getLogger(Main.class); + private static Logger logger = LogsCenter.getLogger(AddDoctorCommand.class); private final Doctor toAdd; @@ -60,6 +59,7 @@ public CommandResult execute(Model model) throws CommandException { } model.addPerson(toAdd); + logger.log(Level.INFO, "Doctor successfully added (when executing command: adddoctor)"); return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd))); } diff --git a/src/main/java/seedu/address/logic/commands/AddPatientCommand.java b/src/main/java/seedu/address/logic/commands/AddPatientCommand.java index f483f6eed9f..cfae9130358 100644 --- a/src/main/java/seedu/address/logic/commands/AddPatientCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddPatientCommand.java @@ -6,6 +6,10 @@ import static seedu.address.logic.parser.CliSyntax.PREFIX_NRIC; import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; +import java.util.logging.Level; +import java.util.logging.Logger; + +import seedu.address.commons.core.LogsCenter; import seedu.address.commons.util.ToStringBuilder; import seedu.address.logic.Messages; import seedu.address.logic.commands.exceptions.CommandException; @@ -33,6 +37,7 @@ public class AddPatientCommand extends Command { public static final String MESSAGE_SUCCESS = "New patient added: %1$s"; public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book"; + private static Logger logger = LogsCenter.getLogger(AddPatientCommand.class); private final Patient toAdd; @@ -49,10 +54,12 @@ public CommandResult execute(Model model) throws CommandException { requireNonNull(model); if (model.hasPerson(toAdd)) { + logger.log(Level.INFO, "Duplicate person detected! (when executing command: addpatient)"); throw new CommandException(MESSAGE_DUPLICATE_PERSON); } model.addPerson(toAdd); + logger.log(Level.INFO, "Patient successfully added (when executing command: addpatient)"); return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd))); } diff --git a/src/main/java/seedu/address/logic/commands/DeleteCommand.java b/src/main/java/seedu/address/logic/commands/DeleteCommand.java index db8f04ef350..64416d8ab8f 100644 --- a/src/main/java/seedu/address/logic/commands/DeleteCommand.java +++ b/src/main/java/seedu/address/logic/commands/DeleteCommand.java @@ -51,6 +51,7 @@ public CommandResult execute(Model model) throws CommandException { Person personToDelete = lastShownList.get(targetIndex.getZeroBased()); model.deletePerson(personToDelete); + logger.log(Level.INFO, "Person succesfully deleted. (when executing command: delete)"); String message = (personToDelete.getType() == Type.PATIENT ? MESSAGE_DELETE_PATIENT_SUCCESS : MESSAGE_DELETE_DOCTOR_SUCCESS); diff --git a/src/main/java/seedu/address/logic/commands/QueryDoctorCommand.java b/src/main/java/seedu/address/logic/commands/QueryDoctorCommand.java index 35f48151b1f..d9cc497e120 100644 --- a/src/main/java/seedu/address/logic/commands/QueryDoctorCommand.java +++ b/src/main/java/seedu/address/logic/commands/QueryDoctorCommand.java @@ -2,6 +2,10 @@ import static java.util.Objects.requireNonNull; +import java.util.logging.Level; +import java.util.logging.Logger; + +import seedu.address.commons.core.LogsCenter; import seedu.address.commons.util.ToStringBuilder; import seedu.address.logic.Messages; import seedu.address.model.Model; @@ -22,6 +26,8 @@ public class QueryDoctorCommand extends Command { + "Parameters: KEYWORD [MORE_KEYWORDS]...\n" + "Example: " + COMMAND_WORD + " alice bob charlie"; + private static Logger logger = LogsCenter.getLogger(QueryDoctorCommand.class); + private final DoctorContainsKeywordsPredicate predicate; public QueryDoctorCommand(DoctorContainsKeywordsPredicate predicate) { @@ -31,7 +37,10 @@ public QueryDoctorCommand(DoctorContainsKeywordsPredicate predicate) { @Override public CommandResult execute(Model model) { requireNonNull(model); + logger.log(Level.INFO, "Executing QueryDoctorCommand"); model.updateFilteredPersonList(predicate); + int numberOfDoctors = model.getFilteredPersonList().size(); + logger.log(Level.INFO, "Number of Doctor found: " + numberOfDoctors); return new CommandResult( String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size())); } diff --git a/src/main/java/seedu/address/model/appointment/AppointmentDateTime.java b/src/main/java/seedu/address/model/appointment/AppointmentDateTime.java index d4371ea1cd6..872e0822eb0 100644 --- a/src/main/java/seedu/address/model/appointment/AppointmentDateTime.java +++ b/src/main/java/seedu/address/model/appointment/AppointmentDateTime.java @@ -24,6 +24,7 @@ public class AppointmentDateTime { * @param dateStr input string to be stored */ public AppointmentDateTime(String dateStr) { + assert dateStr.length() == 16 : "Appointment date-time string is of incorrect length"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); requireNonNull(dateStr); checkArgument(isValidDate(dateStr), MESSAGE_CONSTRAINTS); diff --git a/src/main/java/seedu/address/model/person/DoB.java b/src/main/java/seedu/address/model/person/DoB.java index 372d90b1d74..5166b909b5c 100644 --- a/src/main/java/seedu/address/model/person/DoB.java +++ b/src/main/java/seedu/address/model/person/DoB.java @@ -31,6 +31,7 @@ public class DoB { */ public DoB(String dob) { requireNonNull(dob); + assert dob.length() == 10 : "Person dob string is of incorrect length"; checkArgument(isValidDoB(dob), MESSAGE_CONSTRAINTS); dateOfBirth = LocalDate.parse(dob); } diff --git a/src/main/java/seedu/address/model/person/Nric.java b/src/main/java/seedu/address/model/person/Nric.java index 2c9d033574e..9a659ae5ccf 100644 --- a/src/main/java/seedu/address/model/person/Nric.java +++ b/src/main/java/seedu/address/model/person/Nric.java @@ -27,6 +27,7 @@ public class Nric { */ public Nric(String nric) { requireNonNull(nric); + assert nric.length() == 9 : "Person nric string is of incorrect length"; checkArgument(isValidNric(nric), MESSAGE_CONSTRAINTS); this.nric = nric; } diff --git a/src/main/java/seedu/address/model/person/Phone.java b/src/main/java/seedu/address/model/person/Phone.java index 4fc030787e8..cd0dd9ae1be 100644 --- a/src/main/java/seedu/address/model/person/Phone.java +++ b/src/main/java/seedu/address/model/person/Phone.java @@ -22,6 +22,7 @@ public class Phone { */ public Phone(String phone) { requireNonNull(phone); + assert phone.length() == 8 : "Person phone string is of incorrect length"; checkArgument(isValidPhone(phone), MESSAGE_CONSTRAINTS); value = phone; } From ceb6b093f30a2cf9435ead9f62da5f5394ea354d Mon Sep 17 00:00:00 2001 From: alfaloo Date: Mon, 15 Apr 2024 14:33:03 +0800 Subject: [PATCH 2/2] Fix test cases --- src/test/java/seedu/address/model/person/DoBTest.java | 2 +- src/test/java/seedu/address/model/person/NricTest.java | 2 +- src/test/java/seedu/address/model/person/PhoneTest.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/seedu/address/model/person/DoBTest.java b/src/test/java/seedu/address/model/person/DoBTest.java index 476cdf82152..3bcec763e39 100644 --- a/src/test/java/seedu/address/model/person/DoBTest.java +++ b/src/test/java/seedu/address/model/person/DoBTest.java @@ -16,7 +16,7 @@ public void constructor_null_throwsNullPointerException() { @Test public void constructor_invalidDoB_throwsIllegalArgumentException() { String invalidDoB = ""; - assertThrows(IllegalArgumentException.class, () -> new DoB(invalidDoB)); + assertThrows(AssertionError.class, () -> new DoB(invalidDoB)); } @Test diff --git a/src/test/java/seedu/address/model/person/NricTest.java b/src/test/java/seedu/address/model/person/NricTest.java index 63b40b9f8e4..947146bdd15 100644 --- a/src/test/java/seedu/address/model/person/NricTest.java +++ b/src/test/java/seedu/address/model/person/NricTest.java @@ -16,7 +16,7 @@ public void constructor_null_throwsNullPointerException() { @Test public void constructor_invalidNric_throwsIllegalArgumentException() { String invalidNric = "A0234"; - assertThrows(IllegalArgumentException.class, () -> new Nric(invalidNric)); + assertThrows(AssertionError.class, () -> new Nric(invalidNric)); } @Test diff --git a/src/test/java/seedu/address/model/person/PhoneTest.java b/src/test/java/seedu/address/model/person/PhoneTest.java index b29e00e6c2f..8e40277a74e 100644 --- a/src/test/java/seedu/address/model/person/PhoneTest.java +++ b/src/test/java/seedu/address/model/person/PhoneTest.java @@ -16,7 +16,7 @@ public void constructor_null_throwsNullPointerException() { @Test public void constructor_invalidPhone_throwsIllegalArgumentException() { String invalidPhone = ""; - assertThrows(IllegalArgumentException.class, () -> new Phone(invalidPhone)); + assertThrows(AssertionError.class, () -> new Phone(invalidPhone)); } @Test