Skip to content

Commit

Permalink
Add leave command
Browse files Browse the repository at this point in the history
  • Loading branch information
nixonwidjaja committed Oct 12, 2023
1 parent fb180bc commit 771696d
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ 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_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The employee index provided is invalid";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d employees listed!";
public static final String MESSAGE_DUPLICATE_FIELDS = "Multiple values specified for the "
+ "following single-valued field(s): ";
public static final String MESSAGE_OVER_CLAIM = "Claim is REJECTED as the amount is greater than "
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class AddCommand extends Command {
+ PREFIX_DOB + "1999-07-23";

public static final String MESSAGE_SUCCESS = "New employee added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book";
public static final String MESSAGE_DUPLICATE_PERSON = "The employee already exists in the address book";

private final Person toAdd;

Expand Down
104 changes: 104 additions & 0 deletions src/main/java/seedu/address/logic/commands/LeaveCommand.java
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();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CLAIM_BUDGET;
Expand Down Expand Up @@ -34,6 +35,7 @@ public class AddCommandParser implements Parser<AddCommand> {
* @throws ParseException if the user input does not conform the expected format
*/
public AddCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS,
PREFIX_SALARY, PREFIX_CLAIM_BUDGET, PREFIX_DEPARTMENT, PREFIX_DOB);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.LeaveCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.parser.exceptions.ParseException;

Expand Down Expand Up @@ -81,6 +82,9 @@ public Command parseCommand(String userInput) throws ParseException {
case ClaimCommand.COMMAND_WORD:
return new ClaimCommandParser().parse(arguments);

case LeaveCommand.COMMAND_WORD:
return new LeaveCommandParser().parse(arguments);

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
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 @@ -14,6 +14,7 @@ public class CliSyntax {
public static final Prefix PREFIX_CLAIM_BUDGET = new Prefix("b/");
public static final Prefix PREFIX_DEPARTMENT = new Prefix("d/");
public static final Prefix PREFIX_DOB = new Prefix("dob/");
public static final Prefix PREFIX_MONTH = new Prefix("m/");
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_CLAIM_AMOUNT = new Prefix("$/");
}
74 changes: 74 additions & 0 deletions src/main/java/seedu/address/logic/parser/LeaveCommandParser.java
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();
}

}
39 changes: 36 additions & 3 deletions src/main/java/seedu/address/model/person/Leave.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
public class Leave {
public static final int LEAVE_LENGTH = 12;
public static final String MESSAGE_CONSTRAINTS = "Invalid leave format";
public static final String NO_LEAVE = "000000000000";
private static final String[] MONTHS = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

public final String leave;

/**
* Constructs an empty {@code Leave}.
*/
public Leave() {
this.leave = "000000000000";
this.leave = NO_LEAVE;
}

/**
Expand All @@ -40,16 +43,46 @@ public static boolean isValidLeave(String test) {
return false;
}
for (int i = 0; i < LEAVE_LENGTH; i++) {
if (test.charAt(i) != '0' || test.charAt(i) != '1') {
if (test.charAt(i) != '0' && test.charAt(i) != '1') {
return false;
}
}
return true;
}

/**
* Updates current leave.
* @param change
* @return
*/
public Leave update(String change) {
StringBuilder newLeave = new StringBuilder(leave);
for (int i = 0; i < LEAVE_LENGTH; i++) {
if (change.charAt(i) == '+') {
newLeave.setCharAt(i, '1');
}
if (change.charAt(i) == '-') {
newLeave.setCharAt(i, '0');
}
}
return new Leave(newLeave.toString());
}

@Override
public String toString() {
return leave;
if (leave.equals(NO_LEAVE)) {
return "-";
}
String leaves = "";
for (int i = 0; i < LEAVE_LENGTH; i++) {
if (leave.charAt(i) == '1') {
if (leaves.length() > 0) {
leaves += ", ";
}
leaves += MONTHS[i];
}
}
return leaves;
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/ui/PersonCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class PersonCard extends UiPart<Region> {
private Label department;
@FXML
private Label dob;
@FXML
private Label leave;

/**
* Creates a {@code PersonCode} with the given {@code Person} and index to display.
Expand All @@ -59,5 +61,6 @@ public PersonCard(Person person, int displayedIndex) {
claimBudget.setText("Claim budget: " + person.getClaimBudget().toString());
department.setText("Department: " + person.getDepartment().toString());
dob.setText("DOB: " + person.getDob().toString());
leave.setText("Leave: " + person.getLeave().toString());
}
}
1 change: 1 addition & 0 deletions src/main/resources/view/PersonListCard.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<Label fx:id="claimBudget" styleClass="cell_small_label" text="\$claimBudget" />
<Label fx:id="department" styleClass="cell_small_label" text="\$department" />
<Label fx:id="dob" styleClass="cell_small_label" text="\$dob" />
<Label fx:id="leave" styleClass="cell_small_label" text="\$leave" />
</VBox>
</GridPane>
</HBox>

0 comments on commit 771696d

Please sign in to comment.