forked from nus-cs2103-AY2223S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from comicalromance/mark-unmark
Add Mark Unmark Commands
- Loading branch information
Showing
29 changed files
with
868 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/java/seedu/address/logic/commands/MarkCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
96
src/main/java/seedu/address/logic/commands/UnmarkCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/seedu/address/logic/parser/MarkCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/seedu/address/logic/parser/UnmarkCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.