From f7fad2ce176bb8957a99b04522f0989eb7660959 Mon Sep 17 00:00:00 2001 From: remuslum <57741494+remuslum@users.noreply.github.com> Date: Wed, 8 Nov 2023 04:44:29 +0800 Subject: [PATCH] Fixed birthdayCommand bugs --- .../logic/commands/BirthdayCommand.java | 6 ++++-- .../logic/parser/BirthdayCommandParser.java | 19 ++++++++----------- .../person/MatchingBirthdayPredicate.java | 13 +++++++++++++ .../seedu/address/model/person/Month.java | 11 +++++++++-- .../logic/commands/BirthdayCommandTest.java | 4 ++-- .../parser/BirthdayCommandParserTest.java | 10 ++++++++++ .../person/MatchingBirthdayPredicateTest.java | 10 ++++++++++ .../seedu/address/model/person/MonthTest.java | 6 ++++++ 8 files changed, 62 insertions(+), 17 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/BirthdayCommand.java b/src/main/java/seedu/address/logic/commands/BirthdayCommand.java index 59c85c738e1..9fd11e4b671 100644 --- a/src/main/java/seedu/address/logic/commands/BirthdayCommand.java +++ b/src/main/java/seedu/address/logic/commands/BirthdayCommand.java @@ -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; @@ -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 diff --git a/src/main/java/seedu/address/logic/parser/BirthdayCommandParser.java b/src/main/java/seedu/address/logic/parser/BirthdayCommandParser.java index 6187a214c90..f9c3c7a2eb1 100644 --- a/src/main/java/seedu/address/logic/parser/BirthdayCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/BirthdayCommandParser.java @@ -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 monthList = parseBirthday(argMultimap.getValue(PREFIX_MONTH).get()); MatchingBirthdayPredicate predicate = new MatchingBirthdayPredicate(monthList); return new BirthdayCommand(predicate); @@ -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 parseBirthday(String arg) throws ParseException { - arg = arg.trim(); + private List parseBirthday(String args) throws ParseException { + args = args.trim(); List 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; } diff --git a/src/main/java/seedu/address/model/person/MatchingBirthdayPredicate.java b/src/main/java/seedu/address/model/person/MatchingBirthdayPredicate.java index 043e37cc49d..637a482efb0 100644 --- a/src/main/java/seedu/address/model/person/MatchingBirthdayPredicate.java +++ b/src/main/java/seedu/address/model/person/MatchingBirthdayPredicate.java @@ -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(); + } } diff --git a/src/main/java/seedu/address/model/person/Month.java b/src/main/java/seedu/address/model/person/Month.java index 3495f384077..7cd671d35b8 100644 --- a/src/main/java/seedu/address/model/person/Month.java +++ b/src/main/java/seedu/address/model/person/Month.java @@ -1,6 +1,5 @@ package seedu.address.model.person; -import static java.util.Objects.requireNonNull; import static seedu.address.commons.util.AppUtil.checkArgument; /** @@ -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]; } /** @@ -84,6 +87,10 @@ public int getMonthValue() { return month; } + public String getMonthName() { + return monthName; + } + @Override public boolean equals(Object other) { if (other == this) { diff --git a/src/test/java/seedu/address/logic/commands/BirthdayCommandTest.java b/src/test/java/seedu/address/logic/commands/BirthdayCommandTest.java index 1892bdd3c1c..542154992e8 100644 --- a/src/test/java/seedu/address/logic/commands/BirthdayCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/BirthdayCommandTest.java @@ -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 diff --git a/src/test/java/seedu/address/logic/parser/BirthdayCommandParserTest.java b/src/test/java/seedu/address/logic/parser/BirthdayCommandParserTest.java index 62bd176c173..076b7078b9d 100644 --- a/src/test/java/seedu/address/logic/parser/BirthdayCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/BirthdayCommandParserTest.java @@ -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 monthList = new ArrayList<>(); diff --git a/src/test/java/seedu/address/model/person/MatchingBirthdayPredicateTest.java b/src/test/java/seedu/address/model/person/MatchingBirthdayPredicateTest.java index 361ddbab2c4..51495edcb33 100644 --- a/src/test/java/seedu/address/model/person/MatchingBirthdayPredicateTest.java +++ b/src/test/java/seedu/address/model/person/MatchingBirthdayPredicateTest.java @@ -73,4 +73,14 @@ public void equalList_failure() { MatchingBirthdayPredicate predicate2 = new MatchingBirthdayPredicate(monthList2); assertNotEquals(predicate1, predicate2); } + + @Test + public void stringRep_success() { + List monthList1 = new ArrayList<>(); + monthList1.add(new Month(2)); + monthList1.add(new Month(3)); + MatchingBirthdayPredicate predicate1 = new MatchingBirthdayPredicate(monthList1); + + assertEquals(predicate1.toString(), "Feb,Mar"); + } } diff --git a/src/test/java/seedu/address/model/person/MonthTest.java b/src/test/java/seedu/address/model/person/MonthTest.java index f52eb3b6996..65e91465f76 100644 --- a/src/test/java/seedu/address/model/person/MonthTest.java +++ b/src/test/java/seedu/address/model/person/MonthTest.java @@ -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"); + } }