Skip to content

Commit 6e419cb

Browse files
committed
twelve-days: inline constructVerse
1 parent a18cd41 commit 6e419cb

1 file changed

Lines changed: 13 additions & 17 deletions

File tree

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
public class TwelveDays {
2-
private static String[] days = new String[] {"first", "second", "third", "fourth", "fifth", "sixth", "seventh",
3-
"eighth", "ninth", "tenth", "eleventh", "twelfth"};
4-
private static String[] gifts = new String[] {
2+
private static String[] days = new String[]{"first", "second", "third", "fourth", "fifth", "sixth", "seventh",
3+
"eighth", "ninth", "tenth", "eleventh", "twelfth"};
4+
private static String[] gifts = new String[]{
55
"a Partridge in a Pear Tree.",
66
"two Turtle Doves, ",
77
"three French Hens, ",
@@ -17,30 +17,26 @@ public class TwelveDays {
1717
};
1818

1919
public String verse(int verseNumber) {
20-
return constructVerse(verseNumber);
20+
StringBuilder stringBuilder = new StringBuilder("On the " + days[verseNumber - 1]
21+
+ " day of Christmas my true love gave to me, ");
22+
for (int i = verseNumber; i > 0; i--) {
23+
if (verseNumber != 1 && i == 1) stringBuilder.append("and ");
24+
stringBuilder.append(gifts[i - 1]);
25+
}
26+
stringBuilder.append("\n");
27+
return stringBuilder.toString();
2128
}
2229

2330
public String verses(int verseNumberStart, int verseNumberEnd) {
24-
StringBuilder stringBuilder = new StringBuilder(constructVerse(verseNumberStart));
31+
StringBuilder stringBuilder = new StringBuilder(verse(verseNumberStart));
2532
for (int i = verseNumberStart + 1; i <= verseNumberEnd; i++) {
2633
stringBuilder.append("\n");
27-
stringBuilder.append(constructVerse(i));
34+
stringBuilder.append(verse(i));
2835
}
2936
return stringBuilder.toString();
3037
}
3138

3239
public String sing() {
3340
return verses(1, 12);
3441
}
35-
36-
private String constructVerse(int verseNumber) {
37-
StringBuilder stringBuilder = new StringBuilder("On the " + days[verseNumber - 1]
38-
+ " day of Christmas my true love gave to me, ");
39-
for (int i = verseNumber; i > 0; i--) {
40-
if (verseNumber != 1 && i == 1) stringBuilder.append("and ");
41-
stringBuilder.append(gifts[i - 1]);
42-
}
43-
stringBuilder.append("\n");
44-
return stringBuilder.toString();
45-
}
4642
}

0 commit comments

Comments
 (0)