Skip to content

Commit

Permalink
Merge pull request #150 from Kb-Tay/branch-Bug-CompleteCommand
Browse files Browse the repository at this point in the history
Fix bugs in Complete Command
  • Loading branch information
jylow authored Nov 2, 2023
2 parents 2d64fbb + 1633588 commit ade49f7
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 16 deletions.
19 changes: 14 additions & 5 deletions src/main/java/seedu/address/logic/commands/CompleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public class CompleteCommand extends Command {
public static final String MESSAGE_COMPLETE_SUCCESS = "Appointments Completed!";
public static final String MESSAGE_INVALID_DATE_FORMAT = "Input Date should be in format of dd-MM-yyyy";
public static final String MESSAGE_INVALID_DATE = "Please input a valid Date";
public static final String MESSAGE_PERSON_NO_APPOINTMENT = "No Appointment Found:"
+ " Selected Person currently has no appointment scheduled";
public static final String MESSAGE_DATE_NO_APPOINTMENT = "No Appointment Found:"
+ " No Appointments found with the current date";
private final CompleteDescriptor completeDescriptor;
public CompleteCommand(CompleteDescriptor completeDescriptor) {
this.completeDescriptor = completeDescriptor;
Expand All @@ -51,22 +55,27 @@ public CommandResult execute(Model model) throws CommandException {
model.setPerson(personToEdit, edittedPerson);
};

Consumer<LocalDate> editByDate = date -> {
model.clearAppointments(date);
};

Optional<Index> index = completeDescriptor.getIndex();
if (index.isPresent()) {
if (index.get().getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

Person person = lastShownList.get(index.get().getZeroBased());
if (person.hasNullAppointment()) {
throw new CommandException(MESSAGE_PERSON_NO_APPOINTMENT);
}
}

// if Index present, edit Model by Person Index
completeDescriptor.getIndex().ifPresent(editByIndex);

// if Date present, edit Model by Date;
completeDescriptor.getDate().ifPresent(editByDate);
if (completeDescriptor.getDate().isPresent()) {
if (!model.clearAppointments(completeDescriptor.getDate().orElse(null))) {
throw new CommandException(MESSAGE_DATE_NO_APPOINTMENT);
}
}

model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(MESSAGE_COMPLETE_SUCCESS);
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/seedu/address/logic/commands/SortCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import seedu.address.model.person.Person;




/**
* Sorts all persons in the list by lexicographical order barring capitalisation.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public String gatherEmails(GatherEmailPrompt prompt) {
return persons.gatherEmails(prompt);
};

public void clearAppointments(LocalDate date) {
persons.clearAppointments(date);
public boolean clearAppointments(LocalDate date) {
return persons.clearAppointments(date);
}

//// util methods
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,5 @@ public interface Model {
*/
String gatherEmails(GatherEmailPrompt prompt);

void clearAppointments(LocalDate date);
boolean clearAppointments(LocalDate date);
}
10 changes: 7 additions & 3 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,13 @@ public void setAppointmentList() {
}

@Override
public void clearAppointments(LocalDate date) {
addressBook.clearAppointments(date);
setAppointmentList();
public boolean clearAppointments(LocalDate date) {
boolean isCleared = addressBook.clearAppointments(date);
if (isCleared) {
setAppointmentList();
}

return isCleared;
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ public Person clearAppointment() {
tags, NullAppointment.getNullAppointment());
}

public boolean hasNullAppointment() {
return appointment.equals(NullAppointment.getNullAppointment());
}

/**
* Returns true if both persons have the same identity and data fields.
* This defines a stronger notion of equality between two persons.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,16 @@ public String gatherEmails(GatherEmailPrompt prompt) {
/**
* Clears all appointments of {@code Person} in {@code persons} that match the given {@code LocalDate date}.
*/
public void clearAppointments(LocalDate date) {
public boolean clearAppointments(LocalDate date) {
boolean isCleared = false;
for (Person person: internalList) {
if (person.isSameAppointmentDate(date)) {
setPerson(person, person.clearAppointment());
isCleared = true;
}
}

return isCleared;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public String gatherEmails(GatherEmailPrompt prompt) {
}

@Override
public void clearAppointments(LocalDate date) {
public boolean clearAppointments(LocalDate date) {
throw new AssertionError("This method should not be called.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.logic.commands.CompleteCommand.MESSAGE_COMPLETE_SUCCESS;
import static seedu.address.logic.commands.CompleteCommand.MESSAGE_DATE_NO_APPOINTMENT;
import static seedu.address.logic.commands.CompleteCommand.MESSAGE_PERSON_NO_APPOINTMENT;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;
import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON;
import static seedu.address.testutil.TypicalPersons.BENSON;
Expand Down Expand Up @@ -45,8 +48,8 @@ public void execute_dateSpecified_success() {
Person editedPerson2 = new PersonBuilder(CARL).withNullAppointment().build();
String expectedMessage = MESSAGE_COMPLETE_SUCCESS;
CompleteDescriptor completeDescriptor = new CompleteDescriptor();
CompleteCommand completeCommand = new CompleteCommand(completeDescriptor);
completeDescriptor.setDate(LocalDate.of(2023, 05, 01));
CompleteCommand completeCommand = new CompleteCommand(completeDescriptor);

Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs());
expectedModel.setPerson(model.getFilteredPersonList().get(1), editedPerson1);
Expand All @@ -55,6 +58,26 @@ public void execute_dateSpecified_success() {
assertCommandSuccess(completeCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_noMatchingDate_failure() {
Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs());
CompleteDescriptor completeDescriptor = new CompleteDescriptor();
completeDescriptor.setDate(LocalDate.of(2023, 02, 10)); //no matching appointment date
CompleteCommand completeCommand = new CompleteCommand(completeDescriptor);

assertCommandFailure(completeCommand, expectedModel, MESSAGE_DATE_NO_APPOINTMENT);
}

@Test
public void execute_personNoAppointment_failure() {
Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs());
CompleteDescriptor completeDescriptor = new CompleteDescriptor();
completeDescriptor.setIndex(INDEX_FIRST_PERSON); //person has no appointment
CompleteCommand completeCommand = new CompleteCommand(completeDescriptor);

assertCommandFailure(completeCommand, expectedModel, MESSAGE_PERSON_NO_APPOINTMENT);
}

@Test
public void equals() {
CompleteDescriptor completeDescriptor1 = new CompleteDescriptor();
Expand Down

0 comments on commit ade49f7

Please sign in to comment.