Skip to content

Commit

Permalink
Merge pull request #184 from KJunWei/edit-bug-for-addassignments
Browse files Browse the repository at this point in the history
Edit bug for addassignments
  • Loading branch information
cxyterence authored Nov 7, 2022
2 parents d24de2b + ef202fa commit c580408
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/tutorials/TracingCode.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Now let’s set the breakpoint. First, double-click the item to reach the corres

## Tracing the execution path

Recall from the User Guide that the `edit` command has the format: `edit INDEX [n/NAME] [p/PHONE] [e/EMAIL] [a/ADDRESS] [t/TAG]…​` For this tutorial we will be issuing the command `edit 1 n/Alice Yeoh`.
Recall from the User Guide that the `edit` command has the format: `edit INDEX [n/NAME] [p/PHONE] [e/EMAIL] [a/ADDRESS]…​` For this tutorial we will be issuing the command `edit 1 n/Alice Yeoh`.

<div markdown="span" class="alert alert-primary">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public boolean test(Person person) {
}
Student currPosition = (Student) personToEdit.getPosition();
Student editedPosition = new Student(currPosition.getAttendance(),
currPosition.getOverallGrade(),
"0/0",
currPosition.setAssignments(assignments), filePath1);


Expand Down
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ 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 TAB.";
public static final String MESSAGE_DUPLICATE_PHONE = "This person's Phone already exists in TAB.";
public static final String MESSAGE_DUPLICATE_EMAIL = "This person's Email already exists in TAB.";

private final Index index;
private final EditPersonDescriptor editPersonDescriptor;
Expand Down Expand Up @@ -82,6 +84,14 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

if (!personToEdit.hasSameEmail(editedPerson) && model.hasEmail(editedPerson)) {
throw new CommandException(MESSAGE_DUPLICATE_EMAIL);
}

if (!personToEdit.hasSamePhone(editedPerson) && model.hasPhone(editedPerson)) {
throw new CommandException(MESSAGE_DUPLICATE_PHONE);
}

model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, editedPerson));
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ public boolean hasPerson(Person person) {
return persons.contains(person);
}

/**
* Returns true if a person with the same email as {@code person} exists in the address book.
*/
public boolean hasEmail(Person person) {
requireNonNull(person);
return persons.containsEmail(person);
}

/**
* Returns true if a person with the same phone as {@code person} exists in the address book.
*/
public boolean hasPhone(Person person) {
requireNonNull(person);
return persons.containsPhone(person);
}
/**
* Adds a person to the address book.
* The person must not already exist in the address book.
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ public interface Model {
*/
boolean hasPerson(Person person);

/**
* Returns true if a person with the same email as {@code person} exists in the address book.
*/
boolean hasEmail(Person person);

/**
* Returns true if a person with the same phone as {@code person} exists in the address book.
*/
boolean hasPhone(Person person);

/**
* Deletes the given person.
* The person must exist in the address book.
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ public boolean hasPerson(Person person) {
return addressBook.hasPerson(person);
}

@Override
public boolean hasPhone(Person person) {
requireNonNull(person);
return addressBook.hasPhone(person);
}

@Override
public boolean hasEmail(Person person) {
requireNonNull(person);
return addressBook.hasEmail(person);
}

@Override
public void deletePerson(Person target) {
addressBook.removePerson(target);
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,21 @@ public boolean isSamePerson(Person otherPerson) {
}

return otherPerson != null
&& (otherPerson.getName().equals(getName())
|| otherPerson.getEmail().equals(getEmail())
|| otherPerson.getPhone().equals(getPhone()));
&& otherPerson.getName().equals(getName());
}

/**
* Returns true if both persons have the same phone.
*/
public boolean hasSamePhone(Person otherPerson) {
return phone.equals(otherPerson.getPhone());
}

/**
* Returns true if both persons have the same email.
*/
public boolean hasSameEmail(Person otherPerson) {
return email.equals(otherPerson.getEmail());
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/model/person/UniquePersonList.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ public boolean contains(Person toCheck) {
return internalList.stream().anyMatch(toCheck::isSamePerson);
}

/**
* Returns true if the list contains an equivalent email as the given argument.
*/
public boolean containsEmail(Person toCheck) {
requireNonNull(toCheck);
return internalList.stream().anyMatch(toCheck::hasSameEmail);
}

/**
* Returns true if the list contains an equivalent phone as the given argument.
*/
public boolean containsPhone(Person toCheck) {
requireNonNull(toCheck);
return internalList.stream().anyMatch(toCheck::hasSamePhone);
}

/**
* Adds a person to the list.
* The person must not already exist in the list.
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/seedu/address/logic/commands/AddCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ public boolean hasPerson(Person person) {
throw new AssertionError("This method should not be called.");
}

@Override
public boolean hasEmail(Person person) {
throw new AssertionError("This method should not be called.");
}

@Override
public boolean hasPhone(Person person) {
throw new AssertionError("This method should not be called.");
}

@Override
public void deletePerson(Person target) {
throw new AssertionError("This method should not be called.");
Expand Down
13 changes: 4 additions & 9 deletions src/test/java/seedu/address/model/person/PersonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,13 @@ public void isSamePerson() {
.withAddress(VALID_ADDRESS_BOB).withTags(VALID_TAG_HUSBAND).build();
assertTrue(ALICE.isSamePerson(editedAlice));

// different name, all other attributes same -> returns true
// different name, all other attributes same -> returns false
editedAlice = new PersonBuilder(ALICE).withName(VALID_NAME_BOB).build();
assertTrue(ALICE.isSamePerson(editedAlice));
assertFalse(ALICE.isSamePerson(editedAlice));

// name differs in case, all other attributes same -> returns true
// name differs in case, all other attributes same -> returns false
Person editedBob = new PersonBuilder(BOB).withName(VALID_NAME_BOB.toLowerCase()).build();
assertTrue(BOB.isSamePerson(editedBob));

// name has trailing spaces, all other attributes same -> returns true
String nameWithTrailingSpaces = VALID_NAME_BOB + " ";
editedBob = new PersonBuilder(BOB).withName(nameWithTrailingSpaces).build();
assertTrue(BOB.isSamePerson(editedBob));
assertFalse(BOB.isSamePerson(editedBob));
}

@Test
Expand Down

0 comments on commit c580408

Please sign in to comment.