Skip to content

Commit

Permalink
Merge pull request #122 from tituschewxj/fix-resultMessages
Browse files Browse the repository at this point in the history
fix: Result messages
  • Loading branch information
tituschewxj authored Apr 3, 2024
2 parents 398fb54 + 7b4928d commit dddb04f
Show file tree
Hide file tree
Showing 23 changed files with 253 additions and 97 deletions.
27 changes: 27 additions & 0 deletions src/main/java/seedu/address/commons/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,31 @@ public static boolean isNonZeroUnsignedInteger(String s) {
return false;
}
}

/**
* Returns true if the strings are equal after being trimmed.
*/
public static boolean areStrippedStringsEqual(String a, String b) {
if (a == null || b == null) {
return a == null && b == null;
}

return a.strip().equals(b.strip());
}

/**
* Strips whitespace and format specifiers from the {@code message}.
* <p>
* Format specifiers are assumed to be in the format of {@code %\\d*\\$?\\w}.
*
* @param text The text to strip.
* @return The message without the placeholders and whitespace.
*/
public static String stripMessageFormatSpecifiers(String text) {
if (text.contains("%")) {
return text.replaceAll("%\\d*\\$?\\w", "").trim();
} else {
return text.trim();
}
}
}
83 changes: 53 additions & 30 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,40 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import seedu.address.commons.util.StringUtil;
import seedu.address.logic.commands.AddPersonCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.DeletePersonCommand;
import seedu.address.logic.commands.EditPersonCommand;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListPersonCommand;
import seedu.address.logic.commands.MarkAttendanceCommand;
import seedu.address.logic.commands.SetCourseCommand;
import seedu.address.logic.commands.UnmarkAttendanceCommand;
import seedu.address.logic.parser.Prefix;
import seedu.address.model.person.Person;

/**
* Container for user visible messages.
* <p>
* Messages that are not command-specific are listed here.
* <p>
* Messages that are command-specific should be in their respective classes.
*/
public class Messages {

public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_MISSING_NUSNET = "No such student with the NUSNET_ID found in contacts!";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The student index provided is invalid";
public static final String MESSAGE_MISSING_NUSNET = "No such student with the NUSNet ID found in contacts!";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d students listed!";
public static final String MESSAGE_DUPLICATE_FIELDS =
"Multiple values specified for the following single-valued field(s): ";
public static final String MESSAGE_MARKED_ATTENDANCE_SUCCESS = "Marked attendance for student: ";
public static final String MESSAGE_MARK_EXISTING_ATTENDANCE_SUCCESS =
"Re-marked Attendance for student: ";
public static final String MESSAGE_UNMARKED_ATTENDANCE_SUCCESS = "Unmarked attendance for student: ";
public static final String MESSAGE_UNMARK_NONEXISITING_ATTENDANCE_SUCCESS =
"Attendance is unmarked for student: ";
"Multiple values specified for the following single-valued field(s): ";
public static final String MESSAGE_DUPLICATE_PERSON =
"This student already exists in the contact book";

/** Error messages are shown when a command cannot be executed. */
private static final String[] ERROR_MESSAGES = {
MESSAGE_UNKNOWN_COMMAND,
MESSAGE_INVALID_COMMAND_FORMAT,
Expand All @@ -38,12 +50,26 @@ public class Messages {
MESSAGE_DUPLICATE_FIELDS,
};

private static final String[] SUCCESS_MESSAGES = {
/** Neutral messages are shown when a command executes successfully, but no change occurs. */
private static final String[] NEUTRAL_MESSAGES = {
MESSAGE_PERSONS_LISTED_OVERVIEW,
MESSAGE_MARKED_ATTENDANCE_SUCCESS,
MESSAGE_MARK_EXISTING_ATTENDANCE_SUCCESS,
MESSAGE_UNMARKED_ATTENDANCE_SUCCESS,
MESSAGE_UNMARK_NONEXISITING_ATTENDANCE_SUCCESS,
EditPersonCommand.MESSAGE_NOT_EDITED,
ExitCommand.MESSAGE_EXIT_ACKNOWLEDGEMENT,
HelpCommand.SHOWING_HELP_MESSAGE,
ListPersonCommand.MESSAGE_SUCCESS,
MarkAttendanceCommand.MESSAGE_MARK_EXISTING_ATTENDANCE_SUCCESS,
UnmarkAttendanceCommand.MESSAGE_UNMARK_NONEXISITING_ATTENDANCE_SUCCESS
};

/** Success messages are shown when a command executes successfully and does a modification. */
private static final String[] SUCCESS_MESSAGES = {
AddPersonCommand.MESSAGE_SUCCESS,
ClearCommand.MESSAGE_SUCCESS,
DeletePersonCommand.MESSAGE_DELETE_PERSON_SUCCESS,
EditPersonCommand.MESSAGE_EDIT_PERSON_SUCCESS,
MarkAttendanceCommand.MESSAGE_MARKED_ATTENDANCE_SUCCESS,
SetCourseCommand.MESSAGE_SUCCESS,
UnmarkAttendanceCommand.MESSAGE_UNMARKED_ATTENDANCE_SUCCESS,
};

/**
Expand Down Expand Up @@ -83,35 +109,32 @@ public static String format(Person person) {
}

/**
* Returns true if the message is a message representing the failure of a command.
* Returns true if the {@code message} is a message representing the failure of a command.
* Otherwise, false is returned.
*
* @param message The message to check.
* @see StringUtil#stripMessageFormatSpecifiers(String)
*/
public static boolean isErrorMessage(String message) {
requireNonNull(message);
return Arrays
.stream(ERROR_MESSAGES)
.map(Messages::stripMessagePlaceholders)
.map(StringUtil::stripMessageFormatSpecifiers)
.anyMatch(message::contains);
}

/**
* Returns true if the message is a message representing the success of a command.
* Returns true if the {@code message} is a message representing the success of a command.
* Otherwise, false is returned.
*
* @param message The message to check.
* @see StringUtil#stripMessageFormatSpecifiers(String)
*/
public static boolean isSuccessMessage(String message) {
requireNonNull(message);
return Arrays
.stream(SUCCESS_MESSAGES)
.map(Messages::stripMessagePlaceholders)
.map(StringUtil::stripMessageFormatSpecifiers)
.anyMatch(message::contains);
}

/**
* Strips whitespace and {@code %1$s} placeholders from the message.
*/
private static String stripMessagePlaceholders(String message) {
if (message.contains("%1$s")) {
return String.format(message, "").trim();
} else {
return message.trim();
}
}
}
10 changes: 5 additions & 5 deletions src/main/java/seedu/address/logic/commands/AddPersonCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_DUPLICATE_PERSON;
import static seedu.address.logic.commands.util.CommandMessageUsageUtil.generateMessageUsage;
import static seedu.address.logic.commands.util.ParameterSyntax.PARAMETER_ADDRESS;
import static seedu.address.logic.commands.util.ParameterSyntax.PARAMETER_EMAIL;
Expand All @@ -24,7 +25,7 @@ public class AddPersonCommand extends Command {

public static final String MESSAGE_USAGE = generateMessageUsage(
COMMAND_WORD,
"Adds a person to the address book. ",
"Adds a student into the contact book. ",
PARAMETER_NAME,
PARAMETER_NUSNET,
PARAMETER_PHONE.asOptional(true),
Expand All @@ -33,10 +34,9 @@ public class AddPersonCommand extends Command {
PARAMETER_TAG.asMultiple(2)
);

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_DUPLICATE_NUSNET = "Another person with the same NUSNET already exists in the "
+ "contact book";
public static final String MESSAGE_SUCCESS = "New student added: %1$s";
public static final String MESSAGE_DUPLICATE_NUSNET = "Another student with the same NUSNet ID "
+ "already exists in the contact book";

private final Person toAdd;

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/seedu/address/logic/commands/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
public class ClearCommand extends Command {

public static final String COMMAND_WORD = "clear";
public static final String MESSAGE_SUCCESS = "Address book has been cleared!";

public static final String MESSAGE_SUCCESS = "Contact book has been cleared!";

@Override
public CommandResult execute(Model model) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public class DeletePersonCommand extends Command {

public static final String MESSAGE_USAGE = generateMessageUsage(
COMMAND_WORD,
"Deletes the student identified by NUSNET_ID.",
"Deletes the student identified by NUSNet ID.",
PARAMETER_NUSNET
);

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

private final NusNet targetNusNet;

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

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_DUPLICATE_PERSON;
import static seedu.address.logic.commands.util.CommandMessageUsageUtil.generateMessageUsage;
import static seedu.address.logic.commands.util.ParameterSyntax.PARAMETER_ADDRESS;
import static seedu.address.logic.commands.util.ParameterSyntax.PARAMETER_EMAIL;
Expand Down Expand Up @@ -42,8 +43,8 @@ public class EditPersonCommand extends Command {

public static final String MESSAGE_USAGE = generateMessageUsage(
COMMAND_WORD,
"Edits the details of the person identified "
+ "by the index number used in the displayed person list. "
"Edits the details of the student identified "
+ "by the index number used in the currently displayed student list. "
+ "Existing values will be overwritten by the input values.",
PARAMETER_INDEX,
PARAMETER_NAME.asOptional(false),
Expand All @@ -54,9 +55,8 @@ public class EditPersonCommand extends Command {
PARAMETER_TAG.asMultiple(0)
);

public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Edited Person: %1$s";
public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Edited student: %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.";

private final Index index;
private final EditPersonDescriptor editPersonDescriptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ public class FindPersonCommand extends Command {

public static final String MESSAGE_USAGE = generateMessageUsage(
COMMAND_WORD,
"Finds all persons whose names contain any of "
"Finds all students whose names contain any of "
+ "the specified keywords (case-insensitive) and displays them as a list with index numbers.",
"KEYWORD [MORE_KEYWORDS]...",
COMMAND_WORD + " alice bob charlie"

);

private final NameContainsKeywordsPredicate predicate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@
import seedu.address.model.weeknumber.WeekNumber;

/**
* Marks attendance of an existing person in the address book.
* Marks attendance of an existing student in the contact book.
*/
public class MarkAttendanceCommand extends Command {

public static final String COMMAND_WORD = "mark";

public static final String MESSAGE_USAGE = generateMessageUsage(
COMMAND_WORD,
"Marks the attendance of the person identified by their NusNet by adding the specified week to "
+ "their attendance set. ",
"Marks the attendance of the student identified by their NUSNet ID "
+ "by adding the specified week to their attendance set. ",
PARAMETER_NUSNET, PARAMETER_WEEK);

public static final String MESSAGE_MARKED_ATTENDANCE_SUCCESS = "Marked attendance for student: ";
public static final String MESSAGE_MARK_EXISTING_ATTENDANCE_SUCCESS =
"Attendance is already marked for student: ";

private final NusNet nusNet;
private final WeekNumber weekNumber;

Expand Down Expand Up @@ -59,7 +63,7 @@ public CommandResult execute(Model model) throws CommandException {

if (!updatedWeekAttendance.add(weekNumber)) {
String formattedMessage = String.format("%1$s%2$s, %3$s, Week %4$s",
Messages.MESSAGE_MARK_EXISTING_ATTENDANCE_SUCCESS,
MESSAGE_MARK_EXISTING_ATTENDANCE_SUCCESS,
personToMark.getName(), personToMark.getNusNet(), weekNumber);

return new CommandResult(formattedMessage);
Expand All @@ -70,7 +74,7 @@ public CommandResult execute(Model model) throws CommandException {

model.setPerson(personToMark, updatedPerson);

String formattedMessage = String.format("%1$s%2$s, %3$s, Week %4$s", Messages.MESSAGE_MARKED_ATTENDANCE_SUCCESS,
String formattedMessage = String.format("%1$s%2$s, %3$s, Week %4$s", MESSAGE_MARKED_ATTENDANCE_SUCCESS,
updatedPerson.getName(), updatedPerson.getNusNet(), weekNumber);

return new CommandResult(formattedMessage);
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/seedu/address/logic/commands/SetCourseCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.logic.commands;

import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.logic.commands.util.ParameterSyntax.PARAMETER_COURSE_CODE;
import static seedu.address.model.course.Course.isValidCode;

import seedu.address.logic.commands.exceptions.CommandException;
Expand All @@ -20,13 +21,11 @@
public class SetCourseCommand extends Command {
public static final String COMMAND_WORD = "setcrs";

public static final String MESSAGE_ARGUMENTS = "Course: %1$s";

public static final String MESSAGE_CONSTRAINTS =
"Course code should follow the format \"XX1234Y\", Y is optional";

public static final String MESSAGE_USAGE = CommandMessageUsageUtil.generateMessageUsage(COMMAND_WORD,
"Set Course name ", "course code", COMMAND_WORD + " CS2104");
public static final String MESSAGE_USAGE = CommandMessageUsageUtil.generateMessageUsage(
COMMAND_WORD,
"Sets the course name. ",
PARAMETER_COURSE_CODE);

public static final String MESSAGE_SUCCESS = "Successfully updated course name";

Expand All @@ -49,7 +48,7 @@ public SetCourseCommand(Course course) {
@Override
public CommandResult execute(Model model) throws CommandException {
if (!isValidCode(course.toString())) {
throw new CommandException(MESSAGE_CONSTRAINTS);
throw new CommandException(PARAMETER_COURSE_CODE.getParameterConstraint());

Check warning on line 51 in src/main/java/seedu/address/logic/commands/SetCourseCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/SetCourseCommand.java#L51

Added line #L51 was not covered by tests
}
model.changeCode(course.toString());
return new CommandResult(MESSAGE_SUCCESS);

Check warning on line 54 in src/main/java/seedu/address/logic/commands/SetCourseCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/SetCourseCommand.java#L53-L54

Added lines #L53 - L54 were not covered by tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,32 @@
import seedu.address.model.weeknumber.WeekNumber;

/**
*
* Unmarks attendance of an existing student of a certain week in the contact book.
* <li>The week should be between 1 and 13.
* <li>The NUSNet ID should be valid.
* <li>Attendance can be unmarked again, without any error, but no change would occur.
*/
public class UnmarkAttendanceCommand extends Command {

public static final String COMMAND_WORD = "unmark";

public static final String MESSAGE_USAGE = generateMessageUsage(
COMMAND_WORD,
"Unmarks the attendance of the person identified by their NusNet by removing the specified week to "
+ "their attendance set. ",
"Unmarks the attendance of the student identified by their NUSNet ID "
+ "by removing the specified week to their attendance set. ",
PARAMETER_NUSNET, PARAMETER_WEEK);

public static final String MESSAGE_UNMARKED_ATTENDANCE_SUCCESS = "Unmarked attendance for student: ";
public static final String MESSAGE_UNMARK_NONEXISITING_ATTENDANCE_SUCCESS =
"Attendance is already unmarked for student: ";
private final NusNet nusNet;
private final WeekNumber weekNumber;

/**
* @param nusNet of the person to mark attendance for
* @param weekNumber the week number to mark attendance for
* Creates an {@link UnmarkAttendanceCommand}.
*
* @param nusNet of the person to mark attendance for.
* @param weekNumber the week number to mark attendance for.
*/
public UnmarkAttendanceCommand(NusNet nusNet, WeekNumber weekNumber) {
requireNonNull(nusNet);
Expand All @@ -57,7 +65,7 @@ public CommandResult execute(Model model) throws CommandException {

if (!updatedWeekAttendance.remove(weekNumber)) {
String formattedMessage = String.format("%1$s%2$s, %3$s, Week %4$s",
Messages.MESSAGE_UNMARK_NONEXISITING_ATTENDANCE_SUCCESS,
MESSAGE_UNMARK_NONEXISITING_ATTENDANCE_SUCCESS,
personToUnmark.getName(), personToUnmark.getNusNet(), weekNumber);

return new CommandResult(formattedMessage);
Expand All @@ -70,7 +78,7 @@ public CommandResult execute(Model model) throws CommandException {
model.setPerson(personToUnmark, updatedPerson);

String formattedMessage = String.format("%1$s%2$s, %3$s, Week %4$s",
Messages.MESSAGE_UNMARKED_ATTENDANCE_SUCCESS,
MESSAGE_UNMARKED_ATTENDANCE_SUCCESS,
updatedPerson.getName(), updatedPerson.getNusNet(), weekNumber);

return new CommandResult(formattedMessage);
Expand Down
Loading

0 comments on commit dddb04f

Please sign in to comment.