Skip to content

Commit

Permalink
Merge pull request #117 from bennyLCK/branch-b-FeatureFlaws
Browse files Browse the repository at this point in the history
Fix case-sensitivity bug for sort commands
  • Loading branch information
bennyLCK authored Apr 4, 2024
2 parents f740fa3 + ef780de commit 9ed866f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public class SortArticleCommandParser implements Parser<SortArticleCommand> {
};

/**
* Checks if the given prefix is allowed in choosing an attribute for sorting.
* Checks if the given prefix is allowed in choosing an attribute for sorting that is case-insensitive.
*/
public static boolean isAllowedPrefix(String prefix) {
return AllowedPrefixes.contains(prefix);
return AllowedPrefixes.contains(prefix.toLowerCase()) || AllowedPrefixes.contains(prefix.toUpperCase());
}

/**
Expand All @@ -33,7 +33,7 @@ public static boolean isAllowedPrefix(String prefix) {
*/
public SortArticleCommand parse(String args) throws ParseException {
String prefix = args.trim();
if (prefix.isEmpty() || !PREFIX_DATE.getPrefix().equals(prefix)) {
if (prefix.isEmpty() || !PREFIX_DATE.getPrefix().equalsIgnoreCase(prefix)) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortArticleCommand.MESSAGE_USAGE));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public class SortCommandParser implements Parser<SortCommand> {
};

/**
* Checks if the given prefix is allowed in choosing an attribute for sorting.
* Checks if the given prefix is allowed in choosing an attribute for sorting that is case-insensitive.
*/
public static boolean isAllowedPrefix(String prefix) {
return AllowedPrefixes.contains(prefix);
return AllowedPrefixes.contains(prefix.toLowerCase()) || AllowedPrefixes.contains(prefix.toUpperCase());
}

/**
Expand All @@ -32,12 +32,14 @@ public static boolean isAllowedPrefix(String prefix) {
* @throws ParseException if the user input does not conform the expected format
*/
public SortCommand parse(String args) throws ParseException {

String prefix = args.trim();
if (prefix.isEmpty() || !PREFIX_NAME.getPrefix().equals(prefix)) {
if (prefix.isEmpty() || !PREFIX_NAME.getPrefix().equalsIgnoreCase(prefix)) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE));
}

return new SortCommand(prefix);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void setArticles(List<Article> articles) {
*/
public void sortArticles(String prefix) {
requireNonNull(prefix);
if (PREFIX_DATE.getPrefix().equals(prefix)) {
if (PREFIX_DATE.getPrefix().equalsIgnoreCase(prefix)) {
// Sort by publication date and display most recent articles first.
internalList.sort(Comparator.comparing(Article::getPublicationDate, Comparator.reverseOrder()));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void setPersons(List<Person> persons) {
*/
public void sortPersons(String prefix) {
requireNonNull(prefix);
if (PREFIX_NAME.getPrefix().equals(prefix)) {
if (PREFIX_NAME.getPrefix().equalsIgnoreCase(prefix)) {
internalList.sort(Comparator.comparing(Person::getName));
} else {
throw new IllegalArgumentException("Invalid prefix supplied.");
Expand Down

0 comments on commit 9ed866f

Please sign in to comment.