Skip to content

Commit

Permalink
Merge pull request #173 from remuslum/birthday_bugs_fixes
Browse files Browse the repository at this point in the history
Fixed birthdayCommand bugs
  • Loading branch information
nixonwidjaja authored Nov 11, 2023
2 parents 2a548eb + f7fad2c commit 26dc807
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class BirthdayCommand extends Command {
+ ": Gives a list of all employees who have birthdays in the given month.\n"
+ "Parameters (Optional): "
+ PREFIX_MONTH + "MONTH";
public static final String MESSAGE_SUCCESS = Messages.MESSAGE_LIST_SUCCESS;
public static final String MESSAGE_SUCCESS = Messages.MESSAGE_LIST_SUCCESS + " in the month of ";
public static final String MESSAGE_FAILURE = Messages.MESSAGE_BIRTHDAY_FAILURE;
private final MatchingBirthdayPredicate predicate;

Expand All @@ -38,7 +38,9 @@ public CommandResult execute(Model model, String commandText) {
if (model.getFilteredPersonList().isEmpty()) {
return new CommandResult(MESSAGE_FAILURE);
}
return new CommandResult(String.format(MESSAGE_SUCCESS, model.getFilteredPersonList().size()));

return new CommandResult(String.format(MESSAGE_SUCCESS, model.getFilteredPersonList().size())
+ predicate.toString());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public BirthdayCommand parse(String args) throws ParseException {
if (!arePrefixesPresent(argMultimap, PREFIX_MONTH)) {
throw new ParseException(Messages.MESSAGE_INVALID_MONTH_PREFIX);
} else {
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_MONTH);
List<Month> monthList = parseBirthday(argMultimap.getValue(PREFIX_MONTH).get());
MatchingBirthdayPredicate predicate = new MatchingBirthdayPredicate(monthList);
return new BirthdayCommand(predicate);
Expand All @@ -46,20 +47,16 @@ public BirthdayCommand parse(String args) throws ParseException {

/**
* Parses a list of months given
* @param arg Months as strings
* @param args Months as strings
* @return a list of Months
*/
public List<Month> parseBirthday(String arg) throws ParseException {
arg = arg.trim();
private List<Month> parseBirthday(String args) throws ParseException {
args = args.trim();
List<Month> monthList = new ArrayList<>();
String[] args = arg.split(",");
for (String s : args) {
try {
int month = Integer.parseInt(s);
monthList.add(new Month(month));
} catch (Exception e) {
throw new ParseException(Month.INVALID_MONTH);
}
String[] months = args.split(",");
for (String month : months) {
int monthValue = ParserUtil.parseMonth(month);
monthList.add(new Month(monthValue));
}
return monthList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,17 @@ public boolean equals(Object other) {
public boolean test(Person person) {
return this.month.contains(person.getDob().getMonth());
}

@Override
public String toString() {
StringBuilder message = new StringBuilder();
for (int i = 0; i < month.size(); i++) {
if (i == month.size() - 1) {
message.append(month.get(i).getMonthName());
} else {
message.append(month.get(i).getMonthName()).append(",");
}
}
return message.toString();
}
}
11 changes: 9 additions & 2 deletions src/main/java/seedu/address/model/person/Month.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package seedu.address.model.person;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

/**
Expand All @@ -21,15 +20,19 @@ public class Month {
public static final String INVALID_MONTH = "Invalid month(s) provided.";

public final int month;
public final String monthName;
public final String[] monthNames = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

/**
* Constructs an {@code Month}
* @param monthValue the integer representation of the intended month.
*/
public Month(int monthValue) {
requireNonNull(monthValue);
checkArgument(monthValue > 0 && monthValue < 13, INVALID_MONTH);
month = monthValue;
monthName = monthNames[monthValue - 1];
}

/**
Expand Down Expand Up @@ -84,6 +87,10 @@ public int getMonthValue() {
return month;
}

public String getMonthName() {
return monthName;
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public void execute_birthday_success() {
MatchingBirthdayPredicate matchingBirthdayPredicate = new MatchingBirthdayPredicate(monthList);
expectedFilteredModel.updateFilteredPersonList(matchingBirthdayPredicate);
assertCommandSuccess(new BirthdayCommand(matchingBirthdayPredicate), model,
String.format(MESSAGE_SUCCESS, expectedFilteredModel.getFilteredPersonList().size()),
expectedFilteredModel);
String.format(MESSAGE_SUCCESS, expectedFilteredModel.getFilteredPersonList().size())
+ "Feb", expectedFilteredModel);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public void parse_validMultipleArgs_returnsBirthdayCommand() throws ParseExcepti
assertEquals(parser.parse(" m/3,4"), successfulMatch);
}

@Test
public void parse_invalidMultipleArgs_exceptionThrown() {
assertThrows(ParseException.class, () -> parser.parse(" m/1,-1"));
}

@Test
public void duplicatePrefix_exceptionThrown() {
assertThrows(ParseException.class, () -> parser.parse(" m/1 m/2"));
}

@Test
public void parse_noArgs_returnsBirthdayCommand() throws ParseException {
List<Month> monthList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,14 @@ public void equalList_failure() {
MatchingBirthdayPredicate predicate2 = new MatchingBirthdayPredicate(monthList2);
assertNotEquals(predicate1, predicate2);
}

@Test
public void stringRep_success() {
List<Month> monthList1 = new ArrayList<>();
monthList1.add(new Month(2));
monthList1.add(new Month(3));
MatchingBirthdayPredicate predicate1 = new MatchingBirthdayPredicate(monthList1);

assertEquals(predicate1.toString(), "Feb,Mar");
}
}
6 changes: 6 additions & 0 deletions src/test/java/seedu/address/model/person/MonthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,10 @@ public void execute_invalidMonth() {
public void execute_validMonth() {
assertTrue(Month.isValidMonth(VALID_MONTH));
}

@Test
public void execute_getMonthName() {
Month testMonth = new Month(1);
assertEquals(testMonth.getMonthName(), "Jan");
}
}

0 comments on commit 26dc807

Please sign in to comment.