Skip to content

Commit

Permalink
Refactor to remove id prefix and add delete module functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jessica2828 committed Oct 23, 2024
1 parent 1378b56 commit acf004f
Show file tree
Hide file tree
Showing 28 changed files with 359 additions and 128 deletions.
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class Messages {
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_DUPLICATE_FIELDS =
"Multiple values specified for the following single-valued field(s): ";
public static final String MESSAGE_DUPLICATE_STUDENTID =
"Multiple student IDs specified. Only one student ID is allowed.";

/**
* Returns an error message indicating the duplicate prefixes.
Expand All @@ -32,6 +34,13 @@ public static String getErrorMessageForDuplicatePrefixes(Prefix... duplicatePref
return MESSAGE_DUPLICATE_FIELDS + String.join(" ", duplicateFields);
}

/**
* Returns an error message indicating multiple student IDs were provided.
*/
public static String getErrorMessageForDuplicateID() {
return MESSAGE_DUPLICATE_STUDENTID;
}

/**
* Formats the {@code person} for display to the user.
*/
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
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_STUDENTID;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import seedu.address.commons.util.ToStringBuilder;
Expand All @@ -24,15 +23,15 @@ public class AddCommand extends Command {

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a person to the address book. "
+ "Parameters: "
+ PREFIX_STUDENTID + "ID "
+ "ID "
+ PREFIX_NAME + "NAME "
+ PREFIX_PHONE + "PHONE "
+ PREFIX_EMAIL + "EMAIL "
+ PREFIX_ADDRESS + "ADDRESS "
+ PREFIX_COURSE + "COURSE "
+ PREFIX_TAG + "TAG\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_STUDENTID + "12345678 "
+ "12345678 "
+ PREFIX_NAME + "John Doe "
+ PREFIX_PHONE + "98765432 "
+ PREFIX_EMAIL + "[email protected] "
Expand Down
45 changes: 37 additions & 8 deletions src/main/java/seedu/address/logic/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENTID;

import java.util.List;

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.Module;
import seedu.address.model.person.Person;
import seedu.address.model.person.StudentId;

Expand All @@ -20,25 +20,42 @@ public class DeleteCommand extends Command {
public static final String COMMAND_WORD = "delete";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes the student identified by the Student ID used in the displayed person list.\n"
+ ": Deletes the student identified by the Student ID used in the displayed person list,\n"
+ "or deletes a module from the person's module list.\n"
+ "Parameters: "
+ PREFIX_STUDENTID + "ID\n"
+ "ID\n"
+ "or: "
+ "ID MODULE_KEYWORD"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_STUDENTID + "12345678";
+ "12345678"
+ "or: " + COMMAND_WORD + " "
+ "12345678 m/CS2103T";

public static final String MESSAGE_DELETE_PERSON_SUCCESS = "Deleted Student: %1$s";
public static final String MESSAGE_DELETE_MODULE_SUCCESS = "Deleted Module: %1$s";

public static final String MESSAGE_PERSON_NOT_FOUND = "No student is found with Student ID: %1$s";
public static final String MESSAGE_MODULE_NOT_FOUND = "No module is found for this student: %1$s";
private final StudentId studentId;
private final Module module;

/**
* Creates a DeleteCommand to delete the person identified by the specified {@code StudentId}.
*
* @param studentId The student ID of the person to be deleted.
* @throws NullPointerException if the {@code studentId} is null.
*/
public DeleteCommand(StudentId studentId) {
requireNonNull(studentId);
this.studentId = studentId;
this.module = null;
}

/**
* Creates a DeleteCommand to delete the module by the specified {@code Module}.
*/
public DeleteCommand(StudentId studentId, Module module) {
requireNonNull(studentId);
requireNonNull(module);
this.studentId = studentId;
this.module = module;
}

/**
Expand All @@ -65,6 +82,14 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(String.format(MESSAGE_PERSON_NOT_FOUND, studentId));
}

if (module != null) {
if (!toDelete.hasModule(module)) {
throw new CommandException(String.format(MESSAGE_MODULE_NOT_FOUND, toDelete.getStudentId()));
}
model.deleteModule(toDelete, module);
return new CommandResult(String.format(MESSAGE_DELETE_MODULE_SUCCESS, module.toString()));
}

model.deletePerson(toDelete);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(toDelete)));
}
Expand All @@ -81,13 +106,17 @@ public boolean equals(Object other) {
}

DeleteCommand otherDeleteCommand = (DeleteCommand) other;
return studentId.equals(otherDeleteCommand.studentId);
return studentId.equals(otherDeleteCommand.studentId)
&& (module == null
? otherDeleteCommand.module == null
: module.equals(otherDeleteCommand.module));
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("targetStudentId", studentId)
.add("module", module)
.toString();
}
}
5 changes: 2 additions & 3 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
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_STUDENTID;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

Expand Down Expand Up @@ -41,7 +40,7 @@ public class EditCommand extends Command {
+ "by the studentId assigned to the corresponding student. "
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: STUDENTID (must be a valid and existing 8-digit Student ID) "
+ "[" + PREFIX_STUDENTID + "STUDENTID] "
+ "[" + "STUDENTID] "
+ "[" + PREFIX_NAME + "NAME] "
+ "[" + PREFIX_PHONE + "PHONE] "
+ "[" + PREFIX_EMAIL + "EMAIL] "
Expand Down Expand Up @@ -171,7 +170,7 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) {
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(studentId, name, phone, email, address, course, tag, modules);
return CollectionUtil.isAnyNonNull(name, phone, email, address, course, tag, modules);
}

public void setStudentId(StudentId studentId) {
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/seedu/address/logic/commands/GradeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRADE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENTID;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.ArrayList;
Expand All @@ -26,12 +25,12 @@ public class GradeCommand extends Command {

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Assigns a course-specific grade to a student. "
+ "Parameters: "
+ PREFIX_STUDENTID + "ID "
+ "ID "
+ PREFIX_MODULE + "MODULE "
+ PREFIX_GRADE + "GRADE "
+ "\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_STUDENTID + "12345678 "
+ "12345678 "
+ PREFIX_MODULE + "CS2103T "
+ PREFIX_GRADE + "A+ ";

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/seedu/address/logic/commands/ModuleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENTID;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.ArrayList;
Expand All @@ -24,11 +23,11 @@ public class ModuleCommand extends Command {

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a module to a student. "
+ "Parameters: "
+ PREFIX_STUDENTID + "ID "
+ "ID "
+ PREFIX_MODULE + "MODULE "
+ "\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_STUDENTID + "12345678 "
+ "12345678 "
+ PREFIX_MODULE + "CS2103T ";

public static final String MESSAGE_SUCCESS = "New module added for Student %1$s";
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
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_STUDENTID;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import java.util.stream.Stream;
Expand Down Expand Up @@ -34,18 +33,20 @@ public class AddCommandParser implements Parser<AddCommand> {
*/
public AddCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_STUDENTID, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_ADDRESS, PREFIX_COURSE, PREFIX_TAG);
String preamble = argMultimap.getPreamble();

if (!arePrefixesPresent(argMultimap, PREFIX_STUDENTID, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_COURSE)
|| !argMultimap.getPreamble().isEmpty()) {
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_COURSE)) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_STUDENTID, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_ADDRESS, PREFIX_COURSE);
StudentId studentId = ParserUtil.parseStudentId(argMultimap.getValue(PREFIX_STUDENTID).get());
argMultimap.verifyNoDuplicateStudentId(args);
StudentId studentId = ParserUtil.parseStudentId(preamble);
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());
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/seedu/address/logic/parser/ArgumentMultimap.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,33 @@ public void verifyNoDuplicatePrefixesFor(Prefix... prefixes) throws ParseExcepti
throw new ParseException(Messages.getErrorMessageForDuplicatePrefixes(duplicatedPrefixes));
}
}

/**
* Verifies that there is no more than one StudentId present in the preamble.
* If multiple StudentIds are found, this method throws a {@code ParseException}.
*
* @throws ParseException if multiple StudentIds are present in the arguments.
*/
public void verifyNoDuplicateStudentId(String input) throws ParseException {
String trimmedInput = input.trim();
String[] tokens = trimmedInput.split("\\s+");
int count = 0;
for (String token : tokens) {
if (!token.startsWith("m/") && !token.startsWith("g/")
&& !token.startsWith("n/") && !token.startsWith("p/")
&& !token.startsWith("e/") && !token.startsWith("a/")
&& !token.startsWith("c/") && !token.startsWith("t/")) {

boolean isStudentId = token.matches("^[!?&@]*\\d{7,8}$")
|| token.matches("^[A-Za-z]\\d{7}[A-Za-z]?$");

if (isStudentId) {
count++;
}
}
}
if (count > 1) {
throw new ParseException(Messages.getErrorMessageForDuplicateID());
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
public class CliSyntax {

/* Prefix definitions */
public static final Prefix PREFIX_STUDENTID = new Prefix("id/");
public static final Prefix PREFIX_NAME = new Prefix("n/");
public static final Prefix PREFIX_PHONE = new Prefix("p/");
public static final Prefix PREFIX_EMAIL = new Prefix("e/");
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
public static final Prefix PREFIX_COURSE = new Prefix("c/");
public static final Prefix PREFIX_TAG = new Prefix("t/");

public static final Prefix PREFIX_MODULE = new Prefix("m/");
public static final Prefix PREFIX_GRADE = new Prefix("g/");
}
27 changes: 16 additions & 11 deletions src/main/java/seedu/address/logic/parser/DeleteCommandParser.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULE;

import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Module;
import seedu.address.model.person.StudentId;

/**
Expand All @@ -18,19 +20,22 @@ public class DeleteCommandParser implements Parser<DeleteCommand> {
*/
public DeleteCommand parse(String args) throws ParseException {
String trimmedArg = args.trim();
try {
if (!trimmedArg.startsWith("id/")) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
}
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_MODULE);
argMultimap.verifyNoDuplicateStudentId(args);

String studentIdString = trimmedArg.substring(3).trim();
StudentId studentId = ParserUtil.parseStudentId(studentIdString);
return new DeleteCommand(studentId);

} catch (IllegalArgumentException e) {
if (trimmedArg.isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE), e);
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
}

String studentIdString = argMultimap.getPreamble(); // Assuming preamble is used for ID
StudentId studentId = ParserUtil.parseStudentId(studentIdString);

if (argMultimap.getValue(PREFIX_MODULE).isPresent()) {
String moduleName = argMultimap.getValue(PREFIX_MODULE).get();
Module module = ParserUtil.parseModule(moduleName);
return new DeleteCommand(studentId, module);
}
return new DeleteCommand(studentId);
}
}
17 changes: 8 additions & 9 deletions src/main/java/seedu/address/logic/parser/EditCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULE;
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_STUDENTID;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import seedu.address.logic.commands.EditCommand;
Expand All @@ -30,25 +29,25 @@ public class EditCommandParser implements Parser<EditCommand> {
public EditCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_STUDENTID, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_ADDRESS, PREFIX_COURSE, PREFIX_TAG, PREFIX_MODULE);

ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS,
PREFIX_COURSE, PREFIX_TAG, PREFIX_MODULE);
StudentId studentId;

try {
studentId = ParserUtil.parseStudentId(argMultimap.getPreamble());
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE), pe);
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_STUDENTID, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_ADDRESS, PREFIX_COURSE);
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS,
PREFIX_COURSE);
argMultimap.verifyNoDuplicateStudentId(args);

EditPersonDescriptor editPersonDescriptor = new EditPersonDescriptor();

if (argMultimap.getValue(PREFIX_STUDENTID).isPresent()) {
editPersonDescriptor.setStudentId(ParserUtil.parseStudentId(argMultimap.getValue(PREFIX_STUDENTID).get()));
if (!argMultimap.getPreamble().isEmpty()) {
editPersonDescriptor.setStudentId(ParserUtil.parseStudentId(argMultimap.getPreamble()));
}

if (argMultimap.getValue(PREFIX_NAME).isPresent()) {
editPersonDescriptor.setName(ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get()));
}
Expand Down
Loading

0 comments on commit acf004f

Please sign in to comment.