forked from nus-cs2103-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb180bc
commit 771696d
Showing
10 changed files
with
228 additions
and
6 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
104 changes: 104 additions & 0 deletions
104
src/main/java/seedu/address/logic/commands/LeaveCommand.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,104 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_MONTH; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; | ||
|
||
import java.util.List; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
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.Leave; | ||
import seedu.address.model.person.Person; | ||
|
||
/** | ||
* Adds leave months for an employee. | ||
*/ | ||
public class LeaveCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "leave"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds leave months for an employee.\n" | ||
+ "Parameters: INDEX (must be a positive integer) " + PREFIX_MONTH + "MONTHS\n" | ||
+ "Format: MONTHS must be integers separated by commas without spaces. " | ||
+ "1 - Jan, 2 - Feb, ..., 12 - Dec.\n" | ||
+ "Example: " + COMMAND_WORD + " 1 " | ||
+ PREFIX_MONTH + "3,-4 to add leave in March and remove leave in April."; | ||
|
||
public static final String MESSAGE_AMBIGUOUS = "Please check your MONTHS. Ambiguous leave(s) assignment\n"; | ||
public static final String MESSAGE_INVALID_MONTH = "Please check your MONTHS. Invalid month provided.\n"; | ||
public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Leave(s) successfully updated for employee: %1$s"; | ||
public static final String MESSAGE_NOT_A_NUMBER = "Please check your MONTHS. Some is not a number.\n"; | ||
public static final String MESSAGE_NOT_EDITED = "The employee's leave(s) does not change from previous state: %1$s"; | ||
public static final String MESSAGE_SPACES_DETECTED = "Spaces detected in your MONTHS.\n"; | ||
|
||
private final Index index; | ||
private final String change; | ||
|
||
/** | ||
* Constructs a LeaveCommand to update the {@code Leave} of a {@code Person} | ||
* with the given index. | ||
* @param index | ||
* @param change | ||
*/ | ||
public LeaveCommand(Index index, String change) { | ||
requireNonNull(index); | ||
requireNonNull(change); | ||
|
||
this.index = index; | ||
this.change = change; | ||
} | ||
|
||
@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 oldPerson = lastShownList.get(index.getZeroBased()); | ||
Leave oldLeave = oldPerson.getLeave(); | ||
Leave newLeave = oldLeave.update(change); | ||
|
||
if (oldLeave.equals(newLeave)) { | ||
throw new CommandException(String.format(MESSAGE_NOT_EDITED, oldLeave.toString())); | ||
} | ||
|
||
Person newPerson = new Person(oldPerson.getName(), oldPerson.getPhone(), oldPerson.getEmail(), | ||
oldPerson.getAddress(), oldPerson.getSalary(), oldPerson.getClaimBudget(), | ||
oldPerson.getDepartment(), oldPerson.getDob(), newLeave); | ||
|
||
model.setPerson(oldPerson, newPerson); | ||
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); | ||
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(newPerson))); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof EditCommand)) { | ||
return false; | ||
} | ||
|
||
LeaveCommand otherCommand = (LeaveCommand) other; | ||
return index.equals(otherCommand.index) | ||
&& change.equals(otherCommand.change); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("index", index) | ||
.add("leave", change) | ||
.toString(); | ||
} | ||
} |
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
74 changes: 74 additions & 0 deletions
74
src/main/java/seedu/address/logic/parser/LeaveCommandParser.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,74 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_MONTH; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.LeaveCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.person.Leave; | ||
|
||
/** | ||
* Parses input arguments and creates a new LeaveCommand object | ||
*/ | ||
public class LeaveCommandParser implements Parser<LeaveCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the AddCommand | ||
* and returns an AddCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public LeaveCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
System.out.println(args); | ||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_MONTH); | ||
|
||
Index index; | ||
try { | ||
index = ParserUtil.parseIndex(argMultimap.getPreamble()); | ||
} catch (ParseException pe) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, LeaveCommand.MESSAGE_USAGE), pe); | ||
} | ||
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_MONTH); | ||
if (argMultimap.getValue(PREFIX_MONTH).isPresent()) { | ||
return new LeaveCommand(index, parseLeave(argMultimap.getValue(PREFIX_MONTH).get())); | ||
} else { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, LeaveCommand.MESSAGE_USAGE)); | ||
} | ||
} | ||
|
||
private String parseLeave(String arg) throws ParseException { | ||
arg = arg.trim(); | ||
int len = arg.split("\\s+").length; | ||
if (len > 1) { | ||
throw new ParseException(LeaveCommand.MESSAGE_SPACES_DETECTED + LeaveCommand.MESSAGE_USAGE); | ||
} | ||
StringBuilder months = new StringBuilder(Leave.NO_LEAVE); | ||
String[] args = arg.split(","); | ||
for (int i = 0; i < args.length; i++) { | ||
if (args[i].length() > 3) { | ||
throw new ParseException(LeaveCommand.MESSAGE_INVALID_MONTH + LeaveCommand.MESSAGE_USAGE); | ||
} | ||
try { | ||
if (args[i].charAt(0) == '-') { | ||
Integer month = Integer.valueOf(args[i].substring(1)); | ||
if (months.charAt(month - 1) == '+') { | ||
throw new ParseException(LeaveCommand.MESSAGE_AMBIGUOUS + LeaveCommand.MESSAGE_USAGE); | ||
} | ||
months.setCharAt(month - 1, '-'); | ||
} else { | ||
Integer month = Integer.valueOf(args[i]); | ||
if (months.charAt(month - 1) == '-') { | ||
throw new ParseException(LeaveCommand.MESSAGE_AMBIGUOUS + LeaveCommand.MESSAGE_USAGE); | ||
} | ||
months.setCharAt(month - 1, '+'); | ||
} | ||
} catch (NumberFormatException e) { | ||
throw new ParseException(LeaveCommand.MESSAGE_NOT_A_NUMBER + LeaveCommand.MESSAGE_USAGE); | ||
} | ||
} | ||
return months.toString(); | ||
} | ||
|
||
} |
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