Skip to content

Commit

Permalink
Removed Ability to Assign Role for Add, Assign and Edit Commands with…
Browse files Browse the repository at this point in the history
…out CCA (#204)

* Fix JavaDocs

* Fix Assign without CCA

* Change AddCommandTest
  • Loading branch information
chuahjiajie authored Apr 14, 2024
1 parent ceb78f4 commit 302804c
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class AddCommand extends Command {

public static final String MESSAGE_SUCCESS = "New person added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book";
public static final String MESSAGE_NO_CCA = "Cannot add a person with a role but without a CCA.";

private final Person toAdd;

Expand All @@ -62,6 +63,10 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

if (toAdd.getCcas().isEmpty() && !toAdd.getRoles().isEmpty()) {
throw new CommandException(MESSAGE_NO_CCA);
}

model.addPerson(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd)));
}
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/seedu/address/logic/commands/AssignCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ public class AssignCommand extends Command {
public static final String MESSAGE_ASSIGN_PERSON_SUCCESS = "Assigned Person: %1$s";
public static final String MESSAGE_NOT_ASSIGNED = "Role should be provided here.";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book.";
public static final String MESSAGE_NO_CCA = "Cannot assign role to person without a CCA.";

private final Index index;
private final AssignCommand.AssignPersonDescriptor assignPersonDescriptor;

/**
* @param index of the person to assign
* @param index of the person to assign
* @param assignPersonDescriptor details of the role to assign the person with
*/
public AssignCommand(Index index, AssignCommand.AssignPersonDescriptor assignPersonDescriptor) {
Expand All @@ -74,6 +75,11 @@ public CommandResult execute(Model model) throws CommandException {
}

Person personToAssign = lastShownList.get(index.getZeroBased());

if (personToAssign.getCcas().isEmpty() && !personToAssign.getRoles().isEmpty()) {
throw new CommandException(MESSAGE_NO_CCA);
}

Person assignedPerson = createAssignedPerson(personToAssign, assignPersonDescriptor);

if (!personToAssign.isSamePerson(assignedPerson) && model.hasPerson(assignedPerson)) {
Expand All @@ -87,7 +93,8 @@ public CommandResult execute(Model model) throws CommandException {

/**
* Creates and returns an assigned person with details of the role
* @param personToAssign person who will be assigned
*
* @param personToAssign person who will be assigned
* @param assignPersonDescriptor details of the role to assign the person with
* @return Person who is assigned with a role
*/
Expand Down Expand Up @@ -116,7 +123,8 @@ private static Person createAssignedPerson(Person personToAssign,
public static class AssignPersonDescriptor {
private Set<Role> role;

public AssignPersonDescriptor() {}
public AssignPersonDescriptor() {
}

/**
* Copy constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


/**
* Owes the details of the person identified using the displayed index from the address book.
* Charges a person with an amount of money by CCA and optionally, by role.
*/
public class ChargeCommand extends Command {
public static final String COMMAND_WORD = "charge";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


/**
* Owes the details of the person identified using the displayed index from the address book.
* Deletes a CCA and all persons that is associated with the CCA.
*/
public class DeleteCcaCommand extends Command {
public static final String COMMAND_WORD = "cca_delete";
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ 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.";
public static final String MESSAGE_NO_CCA = "Cannot assign role to person without a CCA.";

private final Index index;
private final EditPersonDescriptor editPersonDescriptor;

/**
* @param index of the person in the filtered person list to edit
* @param index of the person in the filtered person list to edit
* @param editPersonDescriptor details to edit the person with
*/
public EditCommand(Index index, EditPersonDescriptor editPersonDescriptor) {
Expand All @@ -89,6 +90,11 @@ public CommandResult execute(Model model) throws CommandException {
}

Person personToEdit = lastShownList.get(index.getZeroBased());

if (personToEdit.getCcas().isEmpty() && !personToEdit.getRoles().isEmpty()) {
throw new CommandException(MESSAGE_NO_CCA);
}

Person editedPerson = createEditedPerson(personToEdit, editPersonDescriptor);

if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) {
Expand All @@ -114,8 +120,8 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
Set<Role> updatedRoles = editPersonDescriptor.getRoles().orElse(personToEdit.getRoles());
Set<Cca> updatedCcas = editPersonDescriptor.getCcas().orElse(personToEdit.getCcas());
Optional<Metadata> updatedMetadata = editPersonDescriptor
.getMetadata()
.or(() -> personToEdit.getMetadata());
.getMetadata()
.or(() -> personToEdit.getMetadata());
Amount updatedAmount = personToEdit.getAmount();
Attendance updatedAttendance = personToEdit.getAtt();
Sessions updatedSessions = personToEdit.getSess();
Expand Down Expand Up @@ -162,7 +168,8 @@ public static class EditPersonDescriptor {

private Metadata metadata;

public EditPersonDescriptor() {}
public EditPersonDescriptor() {
}

/**
* Copy constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

/**
* Filters all persons in address book by their cca (case-sensitive).
* Serves the purpose of grouping people together..
* Serves the purpose of grouping people together.
*/
public class FilterCommand extends Command {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/**
* Finds and lists all persons in address book whose name contains any of the argument keywords.
* Keyword matching is case insensitive.
* Keyword matching is case-insensitive.
*/
public class FindCommand extends Command {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import seedu.address.model.roles.Role;

/**
* Assigns role to the existing person in the CCA Manager
* Sets the attendance details of an existing person in the CCA Manager
*/
public class SetAttCommand extends Command {

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 @@ -54,6 +54,16 @@ public void execute_duplicatePerson_throwsCommandException() {
assertThrows(CommandException.class, AddCommand.MESSAGE_DUPLICATE_PERSON, () -> addCommand.execute(modelStub));
}

@Test
public void execute_roleWithoutCca_throwsCommandException() {
Person validPerson = new PersonBuilder().withName("Bub").build();
Person invalidPerson = new PersonBuilder().withRoles("Treasurer").build();
AddCommand addCommand = new AddCommand(invalidPerson);
ModelStub modelStub = new ModelStubWithPerson(validPerson);

assertThrows(CommandException.class, AddCommand.MESSAGE_NO_CCA, () -> addCommand.execute(modelStub));
}

@Test
public void equals() {
Person alice = new PersonBuilder().withName("Alice").build();
Expand Down

0 comments on commit 302804c

Please sign in to comment.