Skip to content

Commit

Permalink
Merge pull request #167 from tsulim/162-modify-code-mark-unmark
Browse files Browse the repository at this point in the history
Modify FavCommand and UnfavCommand to reduce code duplication
  • Loading branch information
Pluiexo authored Apr 3, 2024
2 parents 02d7d64 + 8cfa081 commit 42c00f5
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 67 deletions.
21 changes: 16 additions & 5 deletions docs/diagrams/FavSequenceDiagram.puml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ participant ":LogicManager" as LogicManager LOGIC_COLOR
participant ":StaffConnectParser" as StaffConnectParser LOGIC_COLOR
participant ":FavCommandParser" as FavCommandParser LOGIC_COLOR
participant "<<class>>\nParserUtil" as ParserUtil LOGIC_COLOR
participant "d:FavCommand" as FavCommand LOGIC_COLOR
participant "f:FavCommand" as FavCommand LOGIC_COLOR
participant "r:CommandResult" as CommandResult LOGIC_COLOR
participant FavCommand as FavCommand1 <<class>> LOGIC_COLOR
participant "<<class>>\nPersonUtil" as PersonUtil LOGIC_COLOR
end box

box Model MODEL_COLOR_T1
Expand Down Expand Up @@ -40,19 +42,28 @@ create FavCommand
FavCommandParser -> FavCommand++
return

return m
return f
destroy FavCommandParser
return m
return f

LogicManager -> FavCommand : execute(model)
activate FavCommand

FavCommand -> Model : getFilteredPersonList()
FavCommand -> Model : getSortedFilteredPersonList()
activate Model
return

FavCommand -> FavCommand1 : createFavPerson(personToFav)
activate FavCommand1

FavCommand1 -> PersonUtil : createPersonWithFavouriteStatus(Person selectedPerson, Favourite favourite)
activate PersonUtil

create Person
FavCommand -> Person++
PersonUtil -> Person++
return createdPerson

return favPerson
return favPerson

FavCommand -> Model : setPerson(personToFav, favPerson)
Expand Down
17 changes: 14 additions & 3 deletions docs/diagrams/UnfavSequenceDiagram.puml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ participant ":LogicManager" as LogicManager LOGIC_COLOR
participant ":StaffConnectParser" as StaffConnectParser LOGIC_COLOR
participant ":UnfavCommandParser" as UnfavCommandParser LOGIC_COLOR
participant "<<class>>\nParserUtil" as ParserUtil LOGIC_COLOR
participant "d:UnfavCommand" as UnfavCommand LOGIC_COLOR
participant "u:UnfavCommand" as UnfavCommand LOGIC_COLOR
participant "r:CommandResult" as CommandResult LOGIC_COLOR
participant UnfavCommand as UnfavCommand1 <<class>> LOGIC_COLOR
participant "<<class>>\nPersonUtil" as PersonUtil LOGIC_COLOR
end box

box Model MODEL_COLOR_T1
Expand Down Expand Up @@ -47,12 +49,21 @@ return u
LogicManager -> UnfavCommand : execute(model)
activate UnfavCommand

UnfavCommand -> Model : getFilteredPersonList()
UnfavCommand -> Model : getSortedFilteredPersonList()
activate Model
return

UnfavCommand -> UnfavCommand1 : createUnfavPerson(personToUnfav)
activate UnfavCommand1

UnfavCommand1 -> PersonUtil : createPersonWithFavouriteStatus(Person selectedPerson, Favourite favourite)
activate PersonUtil

create Person
UnfavCommand -> Person++
PersonUtil -> Person++
return createdPerson

return unfavPerson
return unfavPerson

UnfavCommand -> Model : setPerson(personToUnfav, unfavPerson)
Expand Down
Binary file modified docs/images/FavSequenceDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/UnfavSequenceDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 2 additions & 21 deletions src/main/java/staffconnect/logic/commands/FavCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,17 @@

import static java.util.Objects.requireNonNull;
import static staffconnect.model.Model.PREDICATE_SHOW_ALL_PERSONS;
import static staffconnect.model.person.PersonUtil.createPersonWithFavouriteStatus;

import java.util.List;
import java.util.Set;

import staffconnect.commons.core.index.Index;
import staffconnect.commons.util.ToStringBuilder;
import staffconnect.logic.Messages;
import staffconnect.logic.commands.exceptions.CommandException;
import staffconnect.model.Model;
import staffconnect.model.availability.Availability;
import staffconnect.model.person.Email;
import staffconnect.model.person.Faculty;
import staffconnect.model.person.Favourite;
import staffconnect.model.person.Module;
import staffconnect.model.person.Name;
import staffconnect.model.person.Person;
import staffconnect.model.person.Phone;
import staffconnect.model.person.Venue;
import staffconnect.model.tag.Tag;

/**
* Sets a person as favourite in the staff book.
Expand Down Expand Up @@ -76,19 +68,8 @@ public CommandResult execute(Model model) throws CommandException {
private static Person createFavPerson(Person personToFav) {
assert personToFav != null;

Name name = personToFav.getName();
Phone phone = personToFav.getPhone();
Email email = personToFav.getEmail();
Module module = personToFav.getModule();
Faculty faculty = personToFav.getFaculty();
Venue venue = personToFav.getVenue();
Set<Tag> tags = personToFav.getTags();
Set<Availability> availabilities = personToFav.getAvailabilities();
Favourite updatedFavourite = new Favourite(true);

Person favPerson =
new Person(name, phone, email, module, faculty, venue, tags, availabilities, updatedFavourite);
favPerson.setMeetings(personToFav.getMeetings());
Person favPerson = createPersonWithFavouriteStatus(personToFav, updatedFavourite);
return favPerson;
}

Expand Down
27 changes: 4 additions & 23 deletions src/main/java/staffconnect/logic/commands/UnfavCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,17 @@

import static java.util.Objects.requireNonNull;
import static staffconnect.model.Model.PREDICATE_SHOW_ALL_PERSONS;
import static staffconnect.model.person.PersonUtil.createPersonWithFavouriteStatus;

import java.util.List;
import java.util.Set;

import staffconnect.commons.core.index.Index;
import staffconnect.commons.util.ToStringBuilder;
import staffconnect.logic.Messages;
import staffconnect.logic.commands.exceptions.CommandException;
import staffconnect.model.Model;
import staffconnect.model.availability.Availability;
import staffconnect.model.person.Email;
import staffconnect.model.person.Faculty;
import staffconnect.model.person.Favourite;
import staffconnect.model.person.Module;
import staffconnect.model.person.Name;
import staffconnect.model.person.Person;
import staffconnect.model.person.Phone;
import staffconnect.model.person.Venue;
import staffconnect.model.tag.Tag;

/**
* Removes a person as favourite in the staff book.
Expand Down Expand Up @@ -62,7 +54,7 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(MESSAGE_DUPLICATE_UNFAV);
}

Person unfavPerson = createUnmarkedPerson(personToUnfav);
Person unfavPerson = createUnfavPerson(personToUnfav);

model.setPerson(personToUnfav, unfavPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
Expand All @@ -72,22 +64,11 @@ public CommandResult execute(Model model) throws CommandException {
/**
* Creates and returns a {@code Person} with the details of {@code personToUnfav}.
*/
private static Person createUnmarkedPerson(Person personToUnfav) {
private static Person createUnfavPerson(Person personToUnfav) {
assert personToUnfav != null;

Name name = personToUnfav.getName();
Phone phone = personToUnfav.getPhone();
Email email = personToUnfav.getEmail();
Module module = personToUnfav.getModule();
Faculty faculty = personToUnfav.getFaculty();
Venue venue = personToUnfav.getVenue();
Set<Tag> tags = personToUnfav.getTags();
Set<Availability> availabilities = personToUnfav.getAvailabilities();
Favourite updatedFavourite = new Favourite(false);

Person unfavPerson =
new Person(name, phone, email, module, faculty, venue, tags, availabilities, updatedFavourite);
unfavPerson.setMeetings(personToUnfav.getMeetings());
Person unfavPerson = createPersonWithFavouriteStatus(personToUnfav, updatedFavourite);
return unfavPerson;
}

Expand Down
48 changes: 34 additions & 14 deletions src/main/java/staffconnect/model/person/PersonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,39 @@ public class PersonUtil {
* Copies and returns a new Person with the data of {@code personToCopy}.
*/
public static Person copyPerson(Person personToCopy) {
Name name = personToCopy.getName();
Phone phone = personToCopy.getPhone();
Email email = personToCopy.getEmail();
Module module = personToCopy.getModule();
Faculty faculty = personToCopy.getFaculty();
Venue venue = personToCopy.getVenue();
Set<Tag> tags = personToCopy.getTags();
Set<Availability> availabilities = personToCopy.getAvailabilities();
List<Meeting> meetings = personToCopy.getMeetings();
Favourite favourite = personToCopy.getFavourite();

Person personToCreate = new Person(name, phone, email, module, faculty, venue, tags, availabilities, favourite);
personToCreate.setMeetings(meetings);
return personToCreate;
Name copiedName = personToCopy.getName();
Phone copiedPhone = personToCopy.getPhone();
Email copiedEmail = personToCopy.getEmail();
Module copiedModule = personToCopy.getModule();
Faculty copiedFaculty = personToCopy.getFaculty();
Venue copiedVenue = personToCopy.getVenue();
Set<Tag> copiedTags = personToCopy.getTags();
Set<Availability> copiedAvailabilities = personToCopy.getAvailabilities();
List<Meeting> copiedMeetings = personToCopy.getMeetings();
Favourite copiedFavourite = personToCopy.getFavourite();

Person copiedPerson = new Person(copiedName, copiedPhone, copiedEmail, copiedModule, copiedFaculty, copiedVenue,
copiedTags, copiedAvailabilities, copiedFavourite);
copiedPerson.setMeetings(copiedMeetings);
return copiedPerson;
}

/**
* Creates and returns a new Person with the data of {@code selectedPerson} and given {@code favourite}.
*/
public static Person createPersonWithFavouriteStatus(Person selectedPerson, Favourite favourite) {
Name name = selectedPerson.getName();
Phone phone = selectedPerson.getPhone();
Email email = selectedPerson.getEmail();
Module module = selectedPerson.getModule();
Faculty faculty = selectedPerson.getFaculty();
Venue venue = selectedPerson.getVenue();
Set<Tag> tags = selectedPerson.getTags();
Set<Availability> availabilities = selectedPerson.getAvailabilities();

Person createdPerson =
new Person(name, phone, email, module, faculty, venue, tags, availabilities, favourite);
createdPerson.setMeetings(selectedPerson.getMeetings());
return createdPerson;
}
}
2 changes: 1 addition & 1 deletion src/test/java/staffconnect/model/ModelManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void indexOf_personInStaffBook_equalsZero() {
}

@Test
public void getFilteredPersonList_modifyList_throwsUnsupportedOperationException() {
public void getSortedFilteredPersonList_modifyList_throwsUnsupportedOperationException() {
assertThrows(UnsupportedOperationException.class, () -> modelManager.getSortedFilteredPersonList().remove(0));
}

Expand Down

0 comments on commit 42c00f5

Please sign in to comment.