Skip to content

Commit

Permalink
Updated FruitGuy : better readability code
Browse files Browse the repository at this point in the history
  • Loading branch information
SandeepLakka committed Aug 29, 2022
1 parent 7da04b1 commit 9e25477
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions src/test/java/io/github/sandeeplakka/codewars/kyu7/FruitGuy.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Locale;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -44,12 +45,6 @@ public void testAllRottenArray() {
assertArrayEquals(expected, removeRotten(rotten));
}

public static String[] removeRotten(String[] fruitBasket) {
if (fruitBasket == null) return new String[]{};
return Arrays.stream(fruitBasket).map(FruitGuy::stockFreshItems).toArray(String[]::new);

}

@Test
public void testNullArray() {
assertNotNull(removeRotten(null));
Expand All @@ -71,17 +66,14 @@ public void testGoodRottenArray() {

}

public static String[] removeRotten(String[] fruitBasket) {
if (fruitBasket == null) return new String[0];
return Arrays.stream(fruitBasket).map(FruitGuy::stockFreshItems).toArray(String[]::new);

}

private static String stockFreshItems(String fruit) {
if (fruit.startsWith("rotten")) {
char[] replacedArr = fruit.replaceFirst("rotten", "").toCharArray();
if (Character.isUpperCase(replacedArr[0])) {
replacedArr[0] = Character.toLowerCase(replacedArr[0]);
return new String(replacedArr);
}
return new String(replacedArr);
} else {
return fruit;
}
return fruit.replaceFirst("rotten", "").toLowerCase(Locale.ROOT);
}

}

0 comments on commit 9e25477

Please sign in to comment.