Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix leave bugs #75

Merged
merged 5 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main/java/seedu/address/logic/commands/LeaveCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ public CommandResult execute(Model model) throws CommandException {

Person oldPerson = lastShownList.get(index.getZeroBased());
Leave oldLeave = oldPerson.getLeave();
Leave newLeave = oldLeave.update(change);
Leave newLeave;
try {
newLeave = oldLeave.update(change);
} catch (IllegalArgumentException e) {
throw new CommandException(e.getMessage());
}

if (oldLeave.equals(newLeave)) {
throw new CommandException(String.format(MESSAGE_NOT_EDITED, oldLeave.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,18 @@ private String parseLeave(String arg) throws ParseException {
try {
if (args[i].charAt(0) == '-') {
Integer month = Integer.valueOf(args[i].substring(1));
if (month < 1 || month > 12) {
throw new ParseException(LeaveCommand.MESSAGE_INVALID_MONTH + LeaveCommand.MESSAGE_USAGE);
}
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 (month < 1 || month > 12) {
throw new ParseException(LeaveCommand.MESSAGE_INVALID_MONTH + LeaveCommand.MESSAGE_USAGE);
}
if (months.charAt(month - 1) == '-') {
throw new ParseException(LeaveCommand.MESSAGE_AMBIGUOUS + LeaveCommand.MESSAGE_USAGE);
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/seedu/address/model/person/Leave.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* Represents a Person's leave.
*/
public class Leave {
public static final String ILLEGAL_MONTH = "Cannot remove leave(s). "
+ "The employee does not have leave(s) on %1$s.";
public static final int LEAVE_LENGTH = 12;
public static final String MESSAGE_CONSTRAINTS = "Invalid leave format";
public static final String NO_LEAVE = "000000000000";
Expand Down Expand Up @@ -57,14 +59,24 @@ public static boolean isValidLeave(String test) {
*/
public Leave update(String change) {
StringBuilder newLeave = new StringBuilder(leave);
String illegalMonths = "";
for (int i = 0; i < LEAVE_LENGTH; i++) {
if (change.charAt(i) == '+') {
newLeave.setCharAt(i, '1');
}
if (change.charAt(i) == '-') {
if (newLeave.charAt(i) == '0') {
if (illegalMonths.length() > 0) {
illegalMonths += ", ";
}
illegalMonths += MONTHS[i];
}
newLeave.setCharAt(i, '0');
}
}
if (!illegalMonths.equals("")) {
throw new IllegalArgumentException(String.format(ILLEGAL_MONTH, illegalMonths));
}
return new Leave(newLeave.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public void execute_failure() {
Index outOfBoundIndex = Index.fromOneBased(model.getFilteredPersonList().size() + 1);
LeaveCommand command = new LeaveCommand(outOfBoundIndex, "0000+0000000");
assertCommandFailure(command, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
assertCommandFailure(new LeaveCommand(INDEX_FIRST_PERSON, "0000---00000"), model,
String.format(Leave.ILLEGAL_MONTH, "May, Jul"));

Person person = model.getFilteredPersonList().get(INDEX_SECOND_PERSON.getZeroBased());
assertCommandFailure(leaveSecondCommand, model,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,13 @@ public void parse_invalidArgs_throwsParseException() {
assertParseFailure(parser, " 1 m/-3,3", LeaveCommand.MESSAGE_AMBIGUOUS + LeaveCommand.MESSAGE_USAGE);

assertParseFailure(parser, " 1 m/z", LeaveCommand.MESSAGE_NOT_A_NUMBER + LeaveCommand.MESSAGE_USAGE);

assertParseFailure(parser, " 1 m/0", LeaveCommand.MESSAGE_INVALID_MONTH + LeaveCommand.MESSAGE_USAGE);

assertParseFailure(parser, " 1 m/-0", LeaveCommand.MESSAGE_INVALID_MONTH + LeaveCommand.MESSAGE_USAGE);

assertParseFailure(parser, " 1 m/13", LeaveCommand.MESSAGE_INVALID_MONTH + LeaveCommand.MESSAGE_USAGE);

assertParseFailure(parser, " 1 m/-13", LeaveCommand.MESSAGE_INVALID_MONTH + LeaveCommand.MESSAGE_USAGE);
}
}
Loading