Skip to content

Commit

Permalink
Merge pull request #237 from Alteqa/branch-addlogging
Browse files Browse the repository at this point in the history
Add logging and assertions
  • Loading branch information
Kappaccinoh authored Apr 15, 2024
2 parents 8b6a652 + b023015 commit f7fc148
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

import seedu.address.Main;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
import seedu.address.commons.util.ToStringBuilder;
Expand Down Expand Up @@ -37,6 +41,7 @@ public class EditAppointmentCommand extends Command {
public static final String MESSAGE_EDIT_APPOINTMENT_SUCCESS = "Edited Appointment: %1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_DUPLICATE_APPOINTMENT = "This appointment already exists in the address book.";
private static Logger logger = LogsCenter.getLogger(Main.class);

private final Index index;
private final EditAppointmentDescriptor editAppointmentDescriptor;
Expand All @@ -59,18 +64,21 @@ public CommandResult execute(Model model) throws CommandException {
List<Appointment> lastShownList = model.getFilteredAppointmentList();

if (index.getZeroBased() >= lastShownList.size()) {
logger.log(Level.INFO, "Specified index is not valid! (when executing command: editappt)");
throw new CommandException(Messages.MESSAGE_INVALID_APPOINTMENT_DISPLAYED_INDEX);
}

Appointment appointmentToEdit = lastShownList.get(index.getZeroBased());

Appointment editedAppointment = createEditedAppointment(appointmentToEdit, editAppointmentDescriptor);
if (appointmentToEdit.isSameAppointment(editedAppointment) && model.hasAppointment(editedAppointment)) {
logger.log(Level.INFO, "Duplicate appointment detected! (when executing command: editappt)");
throw new CommandException(MESSAGE_DUPLICATE_APPOINTMENT);
}

model.setAppointment(appointmentToEdit, editedAppointment);
model.updateFilteredAppointmentList(PREDICATE_SHOW_ALL_APPOINTMENTS);
logger.log(Level.INFO, "Edit appointment success! (when executing command: editappt)");
return new CommandResult(String.format(MESSAGE_EDIT_APPOINTMENT_SUCCESS, Messages.format(editedAppointment)));
}

Expand Down
12 changes: 11 additions & 1 deletion src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

import seedu.address.Main;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
import seedu.address.commons.util.ToStringBuilder;
Expand Down Expand Up @@ -53,6 +57,7 @@ public class EditCommand extends Command {
public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Edited Person: %1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book.";
private static Logger logger = LogsCenter.getLogger(Main.class);

private final Index index;
private final EditPersonDescriptor editPersonDescriptor;
Expand All @@ -75,13 +80,16 @@ public CommandResult execute(Model model) throws CommandException {
List<Person> lastShownList = model.getFilteredPersonList();

if (index.getZeroBased() >= lastShownList.size()) {
logger.log(Level.WARNING, "Index not within valid parameters! (when executing command: edit)");
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

Person personToEdit = lastShownList.get(index.getZeroBased());
Person editedPerson = createEditedPerson(personToEdit, editPersonDescriptor);
assert editedPerson != null;

if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) {
logger.log(Level.WARNING, "Duplicate person detected! (when executing command: edit)");
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

Expand All @@ -95,6 +103,7 @@ public CommandResult execute(Model model) throws CommandException {
for (Appointment appt : model.getFilteredAppointmentList()) {
if (appt.getDoctorNric().equals(personToEdit.getNric())) {
appt.setDoctorNric(editedPerson.getNric());
assert appt.getDoctorNric() == editedPerson.getNric();
}
}

Expand All @@ -105,11 +114,12 @@ public CommandResult execute(Model model) throws CommandException {
for (Appointment appt : model.getFilteredAppointmentList()) {
if (appt.getPatientNric().equals(personToEdit.getNric())) {
appt.setPatientNric(editedPerson.getNric());
assert appt.getPatientNric() == editedPerson.getNric();
}
}

model.updateFilteredAppointmentList(PREDICATE_SHOW_ALL_APPOINTMENTS);

logger.log(Level.INFO, "Edit person success! (when executing command: edit)");
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)));
}

Expand Down

0 comments on commit f7fc148

Please sign in to comment.