Skip to content

Commit

Permalink
Merge pull request #53 from nniiggeell/Refactor-Student-Classes
Browse files Browse the repository at this point in the history
Refactor student classes
  • Loading branch information
g4ryy authored Oct 11, 2021
2 parents 6eac8a7 + 15250f1 commit 76455b1
Show file tree
Hide file tree
Showing 17 changed files with 220 additions and 53 deletions.
12 changes: 6 additions & 6 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,22 @@ Shows a list of all persons stored in NewAddressBook.

Format: `list`

### Editing a person : `edit`
### Editing a student : `editStudent`

Edits an existing person in NewAddressBook.
Edits an existing student in NewAddressBook.

Format: `edit INDEX [n/NAME] [p/PHONE] [e/EMAIL] [a/ADDRESS] [t/TAG]…​`
Format: `editStudent INDEX [n/NAME] [p/PHONE] [e/EMAIL] [a/ADDRESS] [t/TAG]…​`

* Edits the person at the specified `INDEX`. The index refers to the index number shown in the displayed person list. The index **must be a positive integer** 1, 2, 3, …​
* Edits the student at the specified `INDEX`. The index refers to the index number shown in the displayed person list. The index **must be a positive integer** 1, 2, 3, …​
* At least one of the optional fields must be provided.
* Existing values will be updated to the input values.
* When editing tags, the existing tags of the person will be removed i.e adding of tags is not cumulative.
* You can remove all the person’s tags by typing `t/` without
specifying any tags after it.

Examples:
* `edit 1 p/91234567 e/[email protected]` Edits the phone number and email address of the 1st person to be `91234567` and `[email protected]` respectively.
* `edit 2 n/Betsy Crower t/` Edits the name of the 2nd person to be `Betsy Crower` and clears all existing tags.
* `editStudent 1 p/91234567 e/[email protected]` Edits the phone number and email address of the 1st person to be `91234567` and `[email protected]` respectively.
* `editStudent 2 n/Betsy Crower t/` Edits the name of the 2nd person to be `Betsy Crower` and clears all existing tags.

### Locating persons by name: `find`

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package seedu.address.logic.commands;

import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package seedu.address.logic.commands.student;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMERGENCY_CONTACT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_FORM_CLASS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GENDER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_INVOLVEMENT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Student;

/**
* Adds a student to the address book.
*/
public class AddStudentCommand extends Command {
public static final String COMMAND_WORD = "student";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a student to the address book. "
+ "Parameters: "
+ PREFIX_NAME + "NAME "
+ PREFIX_PHONE + "PHONE "
+ PREFIX_EMAIL + "EMAIL "
+ PREFIX_ADDRESS + "ADDRESS "
+ PREFIX_GENDER + "GENDER "
+ PREFIX_FORM_CLASS + "FORM CLASS "
+ PREFIX_INVOLVEMENT + "INVOLVEMENT "
+ "[" + PREFIX_TAG + "TAG]... "
+ PREFIX_EMERGENCY_CONTACT + "EMERGENCY_CONTACT \n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "John Doe "
+ PREFIX_PHONE + "98765432 "
+ PREFIX_EMAIL + "[email protected] "
+ PREFIX_ADDRESS + "311, Clementi Ave 2, #02-25 "
+ PREFIX_GENDER + "M "
+ PREFIX_INVOLVEMENT + "Math class "
+ PREFIX_TAG + "naughty "
+ PREFIX_EMERGENCY_CONTACT + "999 "
+ PREFIX_FORM_CLASS + "3E1";

public static final String MESSAGE_SUCCESS = "New student added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This student already exists in the address book";

private final Student toAdd;

/**
* Creates an AddStudentCommand to add the specified {@code Student}
*/
public AddStudentCommand(Student student) {
requireNonNull(student);
toAdd = student;
}

@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, toAdd));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof seedu.address.logic.commands.student.AddStudentCommand // instanceof handles nulls
&& toAdd.equals(((seedu.address.logic.commands.student.AddStudentCommand) other).toAdd));
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.logic.commands;
package seedu.address.logic.commands.student;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
Expand All @@ -16,6 +16,8 @@

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.descriptors.EditStudentDescriptor;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/seedu/address/logic/parser/AddressBookParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CopyCommand;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.EditStudentCommand;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.StudentCommand;
import seedu.address.logic.commands.student.AddStudentCommand;
import seedu.address.logic.commands.student.EditStudentCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.logic.parser.student.AddStudentCommandParser;
import seedu.address.logic.parser.student.EditStudentCommandParser;

/**
* Parses user input.
Expand Down Expand Up @@ -45,8 +47,8 @@ public Command parseCommand(String userInput) throws ParseException {
final String arguments = matcher.group("arguments");
switch (commandWord) {

case StudentCommand.COMMAND_WORD:
return new StudentCommandParser().parse(arguments);
case AddStudentCommand.COMMAND_WORD:
return new AddStudentCommandParser().parse(arguments);

case EditStudentCommand.COMMAND_WORD:
return new EditStudentCommandParser().parse(arguments);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package seedu.address.logic.parser.student;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMERGENCY_CONTACT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_FORM_CLASS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GENDER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_INVOLVEMENT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import java.util.Set;
import java.util.stream.Stream;

import seedu.address.logic.commands.student.AddStudentCommand;
import seedu.address.logic.parser.ArgumentMultimap;
import seedu.address.logic.parser.ArgumentTokenizer;
import seedu.address.logic.parser.Parser;
import seedu.address.logic.parser.ParserUtil;
import seedu.address.logic.parser.Prefix;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.FormClass;
import seedu.address.model.person.Gender;
import seedu.address.model.person.Involvement;
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
import seedu.address.model.person.Student;
import seedu.address.model.tag.Tag;

public class AddStudentCommandParser implements Parser<AddStudentCommand> {
/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}

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

// put all the non-optional arguments here
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_EMERGENCY_CONTACT, PREFIX_INVOLVEMENT, PREFIX_FORM_CLASS, PREFIX_GENDER)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddStudentCommand.MESSAGE_USAGE));
}

Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
Involvement involvement = ParserUtil.parseInvolvement(argMultimap.getValue(PREFIX_INVOLVEMENT).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
Phone emergencyContact = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_EMERGENCY_CONTACT).get());
FormClass formClass = ParserUtil.parseFormClass(argMultimap.getValue(PREFIX_FORM_CLASS).get());
Gender gender = ParserUtil.parseGender(argMultimap.getValue(PREFIX_GENDER).get());

Student student = new Student(name, phone, email, address, involvement, tagList, emergencyContact, formClass,
gender);

return new AddStudentCommand(student);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.logic.parser;
package seedu.address.logic.parser.student;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
Expand All @@ -19,8 +19,12 @@

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditStudentCommand;
import seedu.address.logic.commands.descriptors.EditStudentDescriptor;
import seedu.address.logic.commands.student.EditStudentCommand;
import seedu.address.logic.parser.ArgumentMultimap;
import seedu.address.logic.parser.ArgumentTokenizer;
import seedu.address.logic.parser.Parser;
import seedu.address.logic.parser.ParserUtil;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.tag.Tag;

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ public boolean isSamePerson(Person otherPerson) {
}

return otherPerson != null
&& otherPerson.getName().equals(getName())
&& otherPerson.getAddress().equals(getAddress());
&& otherPerson.getName().equals(getName());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.StudentCommand;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.commands.student.AddStudentCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
Expand Down Expand Up @@ -83,7 +83,7 @@ public void execute_storageThrowsIoException_throwsCommandException() {
logic = new LogicManager(model, storage);

// Execute student command
String studentCommand = StudentCommand.COMMAND_WORD + NAME_DESC_AMY + PHONE_DESC_AMY + EMAIL_DESC_AMY
String studentCommand = AddStudentCommand.COMMAND_WORD + NAME_DESC_AMY + PHONE_DESC_AMY + EMAIL_DESC_AMY
+ ADDRESS_DESC_AMY + INVOLVEMENT_DESC_AMY + EMERGENCY_CONTACT_DESC_AMY + FORM_CLASS_DESC_AMY
+ GENDER_DESC_AMY;
Student expectedPerson = new StudentBuilder(AMY).withTags().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.student.AddStudentCommand;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
Expand All @@ -16,7 +17,7 @@
/**
* Contains integration tests (interaction with the Model) for {@code StudentCommand}.
*/
public class StudentCommandIntegrationTest {
public class AddStudentCommandIntegrationTest {

private Model model;

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

assertCommandSuccess(new StudentCommand(validStudent), model,
String.format(StudentCommand.MESSAGE_SUCCESS, validStudent), expectedModel);
assertCommandSuccess(new AddStudentCommand(validStudent), model,
String.format(AddStudentCommand.MESSAGE_SUCCESS, validStudent), expectedModel);
}

@Test
public void execute_duplicatePerson_throwsCommandException() {
Student studentInList = (Student) model.getAddressBook().getPersonList().get(0);
assertCommandFailure(new StudentCommand(studentInList), model, StudentCommand.MESSAGE_DUPLICATE_PERSON);
assertCommandFailure(new AddStudentCommand(studentInList), model, AddStudentCommand.MESSAGE_DUPLICATE_PERSON);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.commands.student.AddStudentCommand;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
Expand All @@ -24,46 +25,46 @@
import seedu.address.model.person.Student;
import seedu.address.testutil.StudentBuilder;

public class StudentCommandTest {
public class AddStudentCommandTest {

@Test
public void constructor_nullStudent_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new StudentCommand(null));
assertThrows(NullPointerException.class, () -> new AddStudentCommand(null));
}

@Test
public void execute_studentAcceptedByModel_addSuccessful() throws Exception {
ModelStubAcceptingPersonAdded modelStub = new ModelStubAcceptingPersonAdded();
Student validStudent = new StudentBuilder().build();

CommandResult commandResult = new StudentCommand(validStudent).execute(modelStub);
CommandResult commandResult = new AddStudentCommand(validStudent).execute(modelStub);

assertEquals(String.format(StudentCommand.MESSAGE_SUCCESS, validStudent), commandResult.getFeedbackToUser());
assertEquals(String.format(AddStudentCommand.MESSAGE_SUCCESS, validStudent), commandResult.getFeedbackToUser());
assertEquals(Arrays.asList(validStudent), modelStub.personsAdded);
}

@Test
public void execute_duplicateStudent_throwsCommandException() {
Student validStudent = new StudentBuilder().build();
StudentCommand studentCommand = new StudentCommand(validStudent);
AddStudentCommand addStudentCommand = new AddStudentCommand(validStudent);
ModelStub modelStub = new ModelStubWithPerson(validStudent);

assertThrows(CommandException.class,
StudentCommand.MESSAGE_DUPLICATE_PERSON, () -> studentCommand.execute(modelStub));
AddStudentCommand.MESSAGE_DUPLICATE_PERSON, () -> addStudentCommand.execute(modelStub));
}

@Test
public void equals() {
Student alice = new StudentBuilder().withName("Alice").build();
Student bob = new StudentBuilder().withName("Bob").build();
StudentCommand addAliceCommand = new StudentCommand(alice);
StudentCommand addBobCommand = new StudentCommand(bob);
AddStudentCommand addAliceCommand = new AddStudentCommand(alice);
AddStudentCommand addBobCommand = new AddStudentCommand(bob);

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

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

// different types -> returns false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.descriptors.EditStudentDescriptor;
import seedu.address.logic.commands.student.EditStudentCommand;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
Expand Down
Loading

0 comments on commit 76455b1

Please sign in to comment.