Skip to content

Commit

Permalink
Add Interviewer type as a Person extension
Browse files Browse the repository at this point in the history
  • Loading branch information
yashpola committed Mar 10, 2024
1 parent 52b9a37 commit 8d2b733
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 95 deletions.
45 changes: 3 additions & 42 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;

/**
* Adds a person to the address book.
*/
public class AddCommand extends Command {
public abstract class AddCommand extends Command {

public static final String COMMAND_WORD = "add";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a person to the address book. "
+ "Parameters: "
public static final String MESSAGE_USAGE = "Parameters: "
+ PREFIX_NAME + "NAME "
+ PREFIX_PHONE + "PHONE "
+ PREFIX_EMAIL + "EMAIL "
Expand All @@ -35,7 +30,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";

private final Person toAdd;
protected final Person toAdd;

/**
* Creates an AddCommand to add the specified {@code Person}
Expand All @@ -44,38 +39,4 @@ public AddCommand(Person person) {
requireNonNull(person);
toAdd = person;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (model.hasPerson(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

model.addPerson(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd)));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof AddCommand)) {
return false;
}

AddCommand otherAddCommand = (AddCommand) other;
return toAdd.equals(otherAddCommand.toAdd);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("toAdd", toAdd)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;

/**
* Adds an interviewer to the talent tracker.
*/
public class AddInterviewerCommand extends AddCommand {

public static final String COMMAND_WORD = AddCommand.COMMAND_WORD + "_interviewer";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds an interviewer to the talent tracker. "
+ AddCommand.MESSAGE_USAGE;

public static final String MESSAGE_SUCCESS = "New interviewer added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This interviewer already exists in the talent tracker";

/**
* Creates an AddInterviewerCommand to add the specified {@code Person}
*/
public AddInterviewerCommand(Person person) {
super(person);
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (model.hasPerson(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

model.addPerson(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd)));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof AddInterviewerCommand)) {
return false;
}

AddCommand otherAddCommand = (AddCommand) other;
return toAdd.equals(otherAddCommand.toAdd);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("toAdd", toAdd)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,26 @@
import java.util.stream.Stream;

import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.AddInterviewerCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Email;
import seedu.address.model.person.Interviewer;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.person.Remark;
import seedu.address.model.tag.Tag;

/**
* Parses input arguments and creates a new AddCommand object
* Parses input arguments and creates a new AddInterviewerCommand object
*/
public class AddCommandParser implements Parser<AddCommand> {
public class AddInterviewerCommandParser implements Parser<AddInterviewerCommand> {

/**
* Parses the given {@code String} of arguments in the context of the AddCommand
* and returns an AddCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public AddCommand parse(String args) throws ParseException {
public AddInterviewerCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_TAG);

Expand All @@ -44,9 +45,9 @@ public AddCommand parse(String args) throws ParseException {
Remark remark = new Remark(""); // add command does not allow adding remarks straight away
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));

Person person = new Person(name, phone, email, remark, tagList);
Interviewer person = new Interviewer(name, phone, email, remark, tagList);

return new AddCommand(person);
return new AddInterviewerCommand(person);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.AddInterviewerCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCommand;
Expand Down Expand Up @@ -45,8 +45,8 @@ public Command parseCommand(String userInput) throws ParseException {
final String arguments = matcher.group("arguments");
switch (commandWord) {

case AddCommand.COMMAND_WORD:
return new AddCommandParser().parse(arguments);
case AddInterviewerCommand.COMMAND_WORD:
return new AddInterviewerCommandParser().parse(arguments);

case EditCommand.COMMAND_WORD:
return new EditCommandParser().parse(arguments);
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/seedu/address/model/person/Interviewer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package seedu.address.model.person;

import java.util.Set;

import seedu.address.model.tag.Tag;

/**
* Represents an Interviewer in the talent tracker.
* Guarantees: details are present and not null, field values are validated, immutable.
*/
public class Interviewer extends Person {
/**
* Every field must be present and not null.
*/
public Interviewer(Name name, Phone phone, Email email, Remark remark, Set<Tag> tags) {
super(name, phone, email, remark, tags);
}

@Override
public String toString() {
return "Interviewer: " + super.toString();
}
}
14 changes: 7 additions & 7 deletions src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.AddInterviewerCommand;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.exceptions.CommandException;
Expand Down Expand Up @@ -70,14 +70,14 @@ public void execute_validCommand_success() throws Exception {
}

@Test
public void execute_storageThrowsIoException_throwsCommandException() {
assertCommandFailureForExceptionFromStorage(DUMMY_IO_EXCEPTION, String.format(
public void execute_interviewer_storageThrowsIoException_throwsCommandException() {
assertInterviewerCommandFailureForExceptionFromStorage(DUMMY_IO_EXCEPTION, String.format(
LogicManager.FILE_OPS_ERROR_FORMAT, DUMMY_IO_EXCEPTION.getMessage()));
}

@Test
public void execute_storageThrowsAdException_throwsCommandException() {
assertCommandFailureForExceptionFromStorage(DUMMY_AD_EXCEPTION, String.format(
public void execute_interviewer_storageThrowsAdException_throwsCommandException() {
assertInterviewerCommandFailureForExceptionFromStorage(DUMMY_AD_EXCEPTION, String.format(
LogicManager.FILE_OPS_PERMISSION_ERROR_FORMAT, DUMMY_AD_EXCEPTION.getMessage()));
}

Expand Down Expand Up @@ -145,7 +145,7 @@ private void assertCommandFailure(String inputCommand, Class<? extends Throwable
* @param e the exception to be thrown by the Storage component
* @param expectedMessage the message expected inside exception thrown by the Logic component
*/
private void assertCommandFailureForExceptionFromStorage(IOException e, String expectedMessage) {
private void assertInterviewerCommandFailureForExceptionFromStorage(IOException e, String expectedMessage) {
Path prefPath = temporaryFolder.resolve("ExceptionUserPrefs.json");

// Inject LogicManager with an AddressBookStorage that throws the IOException e when saving
Expand All @@ -164,7 +164,7 @@ public void saveAddressBook(ReadOnlyAddressBook addressBook, Path filePath)
logic = new LogicManager(model, storage);

// Triggers the saveAddressBook method by executing an add command
String addCommand = AddCommand.COMMAND_WORD + NAME_DESC_AMY + PHONE_DESC_AMY
String addCommand = AddInterviewerCommand.COMMAND_WORD + NAME_DESC_AMY + PHONE_DESC_AMY
+ EMAIL_DESC_AMY;
Person expectedPerson = new PersonBuilder(AMY).withTags().build();
ModelManager expectedModel = new ModelManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Contains integration tests (interaction with the Model) for {@code AddCommand}.
*/
public class AddCommandIntegrationTest {
public class AddInterviewerCommandIntegrationTest {

private Model model;

Expand All @@ -33,16 +33,16 @@ public void execute_newPerson_success() {
Model expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs());
expectedModel.addPerson(validPerson);

assertCommandSuccess(new AddCommand(validPerson), model,
String.format(AddCommand.MESSAGE_SUCCESS, Messages.format(validPerson)),
assertCommandSuccess(new AddInterviewerCommand(validPerson), model,
String.format(AddInterviewerCommand.MESSAGE_SUCCESS, Messages.format(validPerson)),
expectedModel);
}

@Test
public void execute_duplicatePerson_throwsCommandException() {
Person personInList = model.getAddressBook().getPersonList().get(0);
assertCommandFailure(new AddCommand(personInList), model,
AddCommand.MESSAGE_DUPLICATE_PERSON);
assertCommandFailure(new AddInterviewerCommand(personInList), model,
AddInterviewerCommand.MESSAGE_DUPLICATE_PERSON);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,46 @@
import seedu.address.model.person.Person;
import seedu.address.testutil.PersonBuilder;

public class AddCommandTest {
public class AddInterviewerCommandTest {

@Test
public void constructor_nullPerson_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new AddCommand(null));
assertThrows(NullPointerException.class, () -> new AddInterviewerCommand(null));
}

@Test
public void execute_personAcceptedByModel_addSuccessful() throws Exception {
ModelStubAcceptingPersonAdded modelStub = new ModelStubAcceptingPersonAdded();
Person validPerson = new PersonBuilder().build();

CommandResult commandResult = new AddCommand(validPerson).execute(modelStub);
CommandResult commandResult = new AddInterviewerCommand(validPerson).execute(modelStub);

assertEquals(String.format(AddCommand.MESSAGE_SUCCESS, Messages.format(validPerson)),
assertEquals(String.format(AddInterviewerCommand.MESSAGE_SUCCESS, Messages.format(validPerson)),
commandResult.getFeedbackToUser());
assertEquals(Arrays.asList(validPerson), modelStub.personsAdded);
}

@Test
public void execute_duplicatePerson_throwsCommandException() {
Person validPerson = new PersonBuilder().build();
AddCommand addCommand = new AddCommand(validPerson);
AddInterviewerCommand addCommand = new AddInterviewerCommand(validPerson);
ModelStub modelStub = new ModelStubWithPerson(validPerson);

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

@Test
public void equals() {
Person alice = new PersonBuilder().withName("Alice").build();
Person bob = new PersonBuilder().withName("Bob").build();
AddCommand addAliceCommand = new AddCommand(alice);
AddCommand addBobCommand = new AddCommand(bob);
AddInterviewerCommand addAliceCommand = new AddInterviewerCommand(alice);
AddCommand addBobCommand = new AddInterviewerCommand(bob);

// same object -> returns true
assertTrue(addAliceCommand.equals(addAliceCommand));

// same values -> returns true
AddCommand addAliceCommandCopy = new AddCommand(alice);
AddCommand addAliceCommandCopy = new AddInterviewerCommand(alice);
assertTrue(addAliceCommand.equals(addAliceCommandCopy));

// different types -> returns false
Expand All @@ -79,8 +79,8 @@ public void equals() {

@Test
public void toStringMethod() {
AddCommand addCommand = new AddCommand(ALICE);
String expected = AddCommand.class.getCanonicalName() + "{toAdd=" + ALICE + "}";
AddCommand addCommand = new AddInterviewerCommand(ALICE);
String expected = AddInterviewerCommand.class.getCanonicalName() + "{toAdd=" + ALICE + "}";
assertEquals(expected, addCommand.toString());
}

Expand Down
Loading

0 comments on commit 8d2b733

Please sign in to comment.