Skip to content

Commit

Permalink
Merge branch 'List-students-of-class' of https://github.com/qinxutan/tp
Browse files Browse the repository at this point in the history
… into List-students-of-class

* 'List-students-of-class' of https://github.com/qinxutan/tp:
  Add command to sort students (AY2324S2-CS2103T-T09-4#117)
  Delete module (AY2324S2-CS2103T-T09-4#120)
  Add edit student command (AY2324S2-CS2103T-T09-4#116)
  Delete student from tutorial command (AY2324S2-CS2103T-T09-4#119)
  • Loading branch information
qinxutan committed Mar 27, 2024
2 parents c26c7a3 + 42ef420 commit bc66cf2
Show file tree
Hide file tree
Showing 38 changed files with 1,741 additions and 60 deletions.
42 changes: 28 additions & 14 deletions docs/diagrams/ModelClassDiagram.puml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ Class AddressBook
Class ModelManager
Class UserPrefs

Class ModuleCode
Class ModuleContainsKeywordPredicate
Class ModuleTutorialPair
Class TutorialClass
Class TutorialContainsKeywordPredicate
Class Tag

Class UniquePersonList
Class Person
Class Address
Class Email
Class Name
Class Phone
Class Tag

Class Email
Class StudentId
Class NameContainsKeywordPredicate
Class EmailContainsKeywordPredicate
Class StudentIdContainsKeywordPredicate
Class I #FFFFFF
}

Expand All @@ -35,20 +42,27 @@ ModelManager -left-> "1" AddressBook
ModelManager -right-> "1" UserPrefs
UserPrefs .up.|> ReadOnlyUserPrefs

ModelManager *-left> "~* filtered" ModuleCode
AddressBook -left> "~* all" ModuleCode
ModuleCode -up> TutorialClass
ModuleTutorialPair -> ModuleCode
ModuleTutorialPair -down> TutorialClass
ModuleContainsKeywordPredicate -..down> ModuleCode
TutorialContainsKeywordPredicate -..up> TutorialClass
TutorialClass -> Person

AddressBook *--> "1" UniquePersonList
UniquePersonList --> "~* all" Person
Person *--> Name
Person *--> Phone
Person *--> Email
Person *--> Address
Person *--> "*" Tag

Person *--> "1" Name
Person *--> "1" Email
Person *--> "1" StudentId
Person *--> Tag
Person -[hidden]up--> I
UniquePersonList -[hidden]right-> I

Name -[hidden]right-> Phone
Phone -[hidden]right-> Address
Address -[hidden]right-> Email
NameContainsKeywordPredicate -..> Name
StudentIdContainsKeywordPredicate -left..> StudentId
EmailContainsKeywordPredicate -right..> Email

ModelManager --> "~* filtered" Person
@enduml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.logic.messages.ModuleMessages.MESSAGE_DELETE_MODULE_SUCCESS;
import static seedu.address.logic.messages.ModuleMessages.MESSAGE_MODULE_NOT_FOUND;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULECODE;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.module.ModuleCode;

/**
* A class used to handle the deletion of tutorial classes.
*/
public class DeleteModuleCommand extends Command {

public static final String COMMAND_WORD = "/delete_module";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes a module with the module code specified\n"
+ "Parameters:" + PREFIX_MODULECODE + "MODULE_CODE\n"
+ "Example: " + COMMAND_WORD + " " + PREFIX_MODULECODE + " CS2103T";

private final ModuleCode module;

/**
* Constructs a DeleteClassCommand to delete the specified {@code TutorialClass}
* from the specified {@code ModuleCode}.
* @param module The module code of the tutorial class to be deleted.
*/
public DeleteModuleCommand(ModuleCode module) {
requireAllNonNull(module);
this.module = module;
}

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

ModuleCode existingModule = model.findModuleFromList(module);
if (existingModule == null) {
String moduleNotFoundMessage = String.format(MESSAGE_MODULE_NOT_FOUND, module);
throw new CommandException(moduleNotFoundMessage);
} else {
model.deleteModule(existingModule);
}
return new CommandResult(generateSuccessMessage(module));
}

/**
* Generates a command execution success message based on whether the tutorial class is successfully deleted.
* @param module The module code of the tutorial class.
* @return The success message.
*/
private String generateSuccessMessage(ModuleCode module) {
return String.format(MESSAGE_DELETE_MODULE_SUCCESS, module.toString());
}

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

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

DeleteModuleCommand e = (DeleteModuleCommand) other;
return module.equals(e.module);
}
}
22 changes: 18 additions & 4 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_INDEX;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENTID;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
Expand Down Expand Up @@ -31,23 +32,26 @@
*/
public class EditCommand extends Command {

public static final String COMMAND_WORD = "edit";
public static final String COMMAND_WORD = "/edit_student";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the person identified "
+ "by the index number used in the displayed person list. "
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: INDEX (must be a positive integer) "
+ "Parameters: "
+ PREFIX_INDEX + "INDEX (must be a positive integer) "
+ "[" + PREFIX_NAME + "NAME] "
+ "[" + PREFIX_STUDENTID + "PHONE] "
+ "[" + PREFIX_STUDENTID + "STUDENT ID] "
+ "[" + PREFIX_EMAIL + "EMAIL] "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_STUDENTID + "91234567 "
+ PREFIX_STUDENTID + "A1234567Z "
+ PREFIX_EMAIL + "[email protected]";

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_DUPLICATE_EMAIL = "This email already exists in the address book.";
public static final String MESSAGE_DUPLICATE_STUDENTID = "This student id already exists in the address book.";

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

Optional<Email> emailToBeEdited = editPersonDescriptor.getEmail();
if (emailToBeEdited.isPresent() && model.hasPersonWithEmail(emailToBeEdited.get())) {
throw new CommandException(MESSAGE_DUPLICATE_EMAIL);
}

Optional<StudentId> studentIdToBeEdited = editPersonDescriptor.getStudentId();
if (studentIdToBeEdited.isPresent() && model.hasPersonWithStudentId(studentIdToBeEdited.get())) {
throw new CommandException(MESSAGE_DUPLICATE_STUDENTID);
}

model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)));
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/seedu/address/logic/commands/SortStudentCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package seedu.address.logic.commands;

import static seedu.address.logic.parser.CliSyntax.PREFIX_SORT_BY;

import java.util.Comparator;

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;

/**
* Sorts students information lexicographically based on given parameter
*/
public class SortStudentCommand extends Command {

public static final String COMMAND_WORD = "/sort_student";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Sorts students based on specified parameter.\n"
+ "Parameters: "
+ PREFIX_SORT_BY + "name " + "or "
+ PREFIX_SORT_BY + "id " + "or "
+ PREFIX_SORT_BY + "email\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_SORT_BY + "name";

public static final String MESSAGE_SORT_STUDENT_SUCCESS = "Students have been successfully sorted";
public static final String MESSAGE_INVALID_PARAMETER = "Not a valid parameter for sorting.";
public static final String MESSAGE_EMPTY_PARAMETER = "Please provide a parameter.";

private final String sortBy;
public SortStudentCommand(String sortBy) {
this.sortBy = sortBy;
}

@Override
public CommandResult execute(Model model) throws CommandException {
Comparator<Person> personComparator;
switch (sortBy) {
case "name":
personComparator = Comparator.comparing(p -> p.getName().toString());
break;
case "id":
personComparator = Comparator.comparing(p -> p.getStudentId().toString());
break;
case "email":
personComparator = Comparator.comparing(p -> p.getEmail().toString());
break;
case "":
throw new CommandException(String.format(Messages.MESSAGE_INVALID_COMMAND_FORMAT, MESSAGE_USAGE));
default:
throw new CommandException(MESSAGE_INVALID_PARAMETER);
}

model.getSortedPersonList(personComparator);
return new CommandResult(MESSAGE_SORT_STUDENT_SUCCESS);
}

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

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

SortStudentCommand otherSortStudentCommand = (SortStudentCommand) other;
return sortBy.equals(otherSortStudentCommand.sortBy);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("sortBy", sortBy)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public boolean equals(Object other) {
return true;
}

if (!(other instanceof AddStudentToClassByIdCommand)) {
if (!(other instanceof AddStudentToClassByIndexCommand)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package seedu.address.logic.commands.deletestudentfromclasscommands;

import static java.util.Objects.requireNonNull;

import java.util.function.Predicate;

import seedu.address.logic.Messages;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.messages.PersonMessages;
import seedu.address.logic.messages.TutorialClassMessages;
import seedu.address.model.Model;
import seedu.address.model.module.ModuleCode;
import seedu.address.model.module.ModuleTutorialPair;
import seedu.address.model.module.TutorialClass;
import seedu.address.model.person.Email;
import seedu.address.model.person.Person;

/**
* Deletes a student from a specified tutorial class, by identifying
* the student via their email.
*/
public class DeleteStudentFromClassByEmailCommand extends DeleteStudentFromClassCommand {
private final Predicate<Person> predicate;

private final Email email;

/**
* Deletes a student from a class by email.
* @param email
* @param module
* @param tutorialClass
*/
public DeleteStudentFromClassByEmailCommand(Email email, ModuleCode module, TutorialClass tutorialClass) {
super(module, tutorialClass);
this.email = email;
this.predicate = person -> person.getEmail().equals(email);
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
ModuleTutorialPair moduleAndTutorialClass = getModuleAndTutorialClass(model);
TutorialClass tutorialClass = moduleAndTutorialClass.getTutorialClass();
ModuleCode module = moduleAndTutorialClass.getModule();
Person personToDelete;

personToDelete = model.searchPersonByPredicate(predicate);
if (personToDelete == null) {
throw new CommandException(String.format(PersonMessages.MESSAGE_PERSON_EMAIL_NOT_FOUND, email));
}
if (!(tutorialClass.hasStudent(personToDelete))) {
throw new CommandException(
String.format(TutorialClassMessages.MESSAGE_STUDENT_NOT_FOUND_IN_CLASS,
Messages.format(personToDelete), tutorialClass));
} else {
model.deletePersonFromTutorialClass(personToDelete, module, tutorialClass);
return new CommandResult(
String.format(TutorialClassMessages.MESSAGE_DELETE_STUDENT_FROM_CLASS_SUCCESS,
Messages.format(personToDelete), module, tutorialClass));
}
}

/**
* Returns true if both DeleteStudentFromClassByEmailCommand have the same email.
*/
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

if (!(other instanceof DeleteStudentFromClassByEmailCommand)) {
return false;
}

DeleteStudentFromClassByEmailCommand otherDeleteCommand = (DeleteStudentFromClassByEmailCommand) other;
return email.equals(otherDeleteCommand.email);
}
}
Loading

0 comments on commit bc66cf2

Please sign in to comment.