Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
nixonwidjaja committed Oct 18, 2023
1 parent 7ef9bd4 commit 63bebb6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
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
14 changes: 13 additions & 1 deletion src/main/java/seedu/address/model/person/Leave.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
* 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";
private static final String[] MONTHS = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

public final String leave;

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

0 comments on commit 63bebb6

Please sign in to comment.