Skip to content

Commit

Permalink
Merge pull request #40 from comicalromance/mark-unmark
Browse files Browse the repository at this point in the history
Add Mark Unmark Commands
  • Loading branch information
comicalromance authored Oct 10, 2022
2 parents 606f688 + da741f4 commit b718fc3
Show file tree
Hide file tree
Showing 29 changed files with 868 additions and 20 deletions.
2 changes: 1 addition & 1 deletion docs/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ header_pages:

markdown: kramdown

repository: "https://github.com/AY2223S1-CS2103T-W13-1/tp"
repository: "AY2223S1-CS2103T-W13-1/tp"
github_icon: "images/github-icon.png"

plugins:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class AddTagCommand extends Command {
/**
* Constructs AddTagCommand that will add specified tag to all persons in the
* displayed list.
* @param index of the person in the filtered person list to add the tag
*
* @param tags of the person to be added
*/
public AddTagCommand(Set<Tag> tags) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import seedu.address.model.person.Person;

/**
* Deletes a person identified using it's displayed index from the address book.
* Deletes a person identified using its displayed index from the address book.
*/
public class DeleteCommand extends Command {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
personData.setEmail(editPersonDescriptor.getEmail().orElse(personToEdit.getEmail()));
personData.setAddress(editPersonDescriptor.getAddress().orElse(personToEdit.getAddress()));
personData.setTags(editPersonDescriptor.getTags().orElse(personToEdit.getTags()));
personData.setAttendances(personToEdit.getAttendances());

return new Person(personData);
}
Expand Down
94 changes: 94 additions & 0 deletions src/main/java/seedu/address/logic/commands/MarkCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package seedu.address.logic.commands;

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

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Attendance;
import seedu.address.model.person.Person;
import seedu.address.model.person.PersonData;

/**
* Marks a person identified using its displayed index from the address book as having attended a class or tutorial.
*/
public class MarkCommand extends Command {

public static final String COMMAND_WORD = "mark";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Marks the attendance for person identified by the index number used in the displayed"
+ " person list.\n Marks attendance for the class or tutorial specified in the parameter.\n"
+ "Parameters: INDEX (must be positive integer) OPTION (must be absent/present) "
+ PREFIX_CLASS + " [CLASS]\n"
+ "Example: " + COMMAND_WORD + " 1 present " + PREFIX_CLASS + " T01";

public static final String MESSAGE_MARK_SUCCESS = "Marked Person as %1$s: %2$s";

private final Index index;
private final Attendance attendance;

/**
* @param index Index of the person in the filtered person list to edit the attendance
* @param attendance Attendance of the person to be added
*/
public MarkCommand(Index index, Attendance attendance) {
this.index = index;
this.attendance = attendance;
}

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

List<Person> lastShownList = model.getFilteredPersonList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

Person personToEdit = lastShownList.get(index.getZeroBased());
PersonData personData = new PersonData();
personData.setName(personToEdit.getName());
personData.setPhone(personToEdit.getPhone());
personData.setEmail(personToEdit.getEmail());
personData.setAddress(personToEdit.getAddress());
personData.setTags(personToEdit.getTags());

Set<Attendance> newAttendance = new HashSet<>(personToEdit.getAttendances());
newAttendance.add(attendance);
personData.setAttendances(newAttendance);
Person editedPerson = new Person(personData);

model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);

return new CommandResult(
String.format(MESSAGE_MARK_SUCCESS, attendance.getAttendance(), editedPerson));
}

@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}

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

// state check
MarkCommand e = (MarkCommand) other;
return index.equals(e.index)
&& attendance.equals(e.attendance);
}
}
96 changes: 96 additions & 0 deletions src/main/java/seedu/address/logic/commands/UnmarkCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package seedu.address.logic.commands;

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

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Attendance;
import seedu.address.model.person.Person;
import seedu.address.model.person.PersonData;

/**
* Unmarks the specified attendance record from the person identified using its displayed index.
*/
public class UnmarkCommand extends Command {

public static final String COMMAND_WORD = "unmark";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Unmarks the attendance for person identified by the index number used in the displayed"
+ " person list.\n Removes attendance record for the class or tutorial specified in the parameter.\n"
+ "Parameters: INDEX (must be positive integer) "
+ PREFIX_CLASS + " [CLASS]\n"
+ "Example: " + COMMAND_WORD + " 1 " + PREFIX_CLASS + " T01";

public static final String MESSAGE_UNMARK_SUCCESS = "Removed Class %1$s from Person: %2$s";
public static final String MESSAGE_UNMARK_NOTFOUND = "Class %1$s not found in Person: %2$s";

private final Index index;
private final Attendance attendance;

/**
* @param index Index of the person in the filtered person list to remove the attendance
* @param attendance Attendance of the person to be removed
*/
public UnmarkCommand(Index index, Attendance attendance) {
this.index = index;
this.attendance = attendance;
}

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

List<Person> lastShownList = model.getFilteredPersonList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

Person personToEdit = lastShownList.get(index.getZeroBased());
PersonData personData = new PersonData();
personData.setName(personToEdit.getName());
personData.setPhone(personToEdit.getPhone());
personData.setEmail(personToEdit.getEmail());
personData.setAddress(personToEdit.getAddress());
personData.setTags(personToEdit.getTags());

Set<Attendance> newAttendance = new HashSet<>(personToEdit.getAttendances());
boolean isRemoved = newAttendance.remove(attendance);
personData.setAttendances(newAttendance);
Person editedPerson = new Person(personData);

model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);

return new CommandResult(
String.format(isRemoved ? MESSAGE_UNMARK_SUCCESS : MESSAGE_UNMARK_NOTFOUND,
attendance.className, editedPerson));
}

@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}

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

// state check
UnmarkCommand e = (UnmarkCommand) other;
return index.equals(e.index)
&& attendance.equals(e.attendance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.MarkCommand;
import seedu.address.logic.commands.UnmarkCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -73,6 +75,12 @@ public Command parseCommand(String userInput) throws ParseException {
case HelpCommand.COMMAND_WORD:
return new HelpCommand();

case MarkCommand.COMMAND_WORD:
return new MarkCommandParser().parse(arguments);

case UnmarkCommand.COMMAND_WORD:
return new UnmarkCommandParser().parse(arguments);

case FilterCommand.COMMAND_WORD:
return new FilterCommandParser().parse(arguments);

Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public class CliSyntax {
public static final Prefix PREFIX_EMAIL = new Prefix("e/");
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_CLASS = new Prefix("c/");

}
61 changes: 61 additions & 0 deletions src/main/java/seedu/address/logic/parser/MarkCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package seedu.address.logic.parser;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CLASS;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.logic.commands.MarkCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Attendance;

/**
* Parses input arguments and creates a new MarkCommand object
*/
public class MarkCommandParser implements Parser<MarkCommand> {

public static final String MESSAGE_INVALID_OPTION = "Option must either be 'present' or 'absent'!";

/**
* Parses the given {@code String} of arguments in the context of the MarkCommand
* and returns a MarkCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public MarkCommand parse(String args) throws ParseException {

ParseException parseException = new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
MarkCommand.MESSAGE_USAGE));
requireNonNull(args);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args,
PREFIX_CLASS);

Index index;
String[] preamble = argMultimap.getPreamble().split("\\s+");
if (preamble.length != 2) {
throw parseException;
}

try {
index = ParserUtil.parseIndex(preamble[0]);
} catch (IllegalValueException ive) {
throw parseException;
}

boolean attended = parseOption(preamble[1]);
String className = ParserUtil.parseClassName(argMultimap.getValue(PREFIX_CLASS).orElse(""));
Attendance attendance = new Attendance(className, attended);

return new MarkCommand(index, attendance);
}

private boolean parseOption(String option) throws ParseException {
if (option.equals("present")) {
return true;
} else if (option.equals("absent")) {
return false;
} else {
throw new ParseException(MESSAGE_INVALID_OPTION);
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import seedu.address.commons.util.StringUtil;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Address;
import seedu.address.model.person.Attendance;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
Expand Down Expand Up @@ -121,4 +122,19 @@ public static Set<Tag> parseTags(Collection<String> tags) throws ParseException
}
return tagSet;
}

/**
* Parses a {@code String className} checking for any errors.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code className} is invalid.
*/
public static String parseClassName(String className) throws ParseException {
requireNonNull(className);
String trimmedClass = className.trim();
if (!Attendance.isValidClassName(trimmedClass)) {
throw new ParseException(Attendance.MESSAGE_CONSTRAINTS);
}
return trimmedClass;
}
}
41 changes: 41 additions & 0 deletions src/main/java/seedu/address/logic/parser/UnmarkCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package seedu.address.logic.parser;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CLASS;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.logic.commands.UnmarkCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Attendance;

/**
* Parses input arguments and creates a new UnmarkCommand object
*/
public class UnmarkCommandParser implements Parser<UnmarkCommand> {

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

Index index;
try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
} catch (IllegalValueException ive) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
UnmarkCommand.MESSAGE_USAGE));
}

String className = ParserUtil.parseClassName(argMultimap.getValue(PREFIX_CLASS).orElse(""));
Attendance attendance = new Attendance(className, true);

return new UnmarkCommand(index, attendance);
}
}
Loading

0 comments on commit b718fc3

Please sign in to comment.