Created
December 19, 2016 16:37
-
-
Save swdgit/d8b2bfd8bb5880492db90440a66a3d88 to your computer and use it in GitHub Desktop.
Java - reddit dailyprogrammer Twelve Days of Xmas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.MessageFormat; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class TwelveDaysOfXmas { | |
private static final List<String> GIFTS = new ArrayList<>(Arrays.asList("a Partridge in a Pear Tree", | |
"two Turtle Doves", | |
"three French Hens", | |
"four Calling Birds", | |
"five Golden Rings", | |
"six Geese a Laying", | |
"seven Swans a Swimming", | |
"eight Maids a Milking", | |
"nine Ladies Dancing", | |
"ten Lords a Leaping", | |
"eleven Pipers Piping", | |
"twelve Drummers Drumming")); | |
private static final List<String> DAYS = new ArrayList<>(Arrays.asList("first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth")); | |
public static void main(String... args) { | |
String gift = ""; | |
int index = 0; | |
for (String day : DAYS) { | |
System.out.println(MessageFormat.format("On the {0} day of Christmas \n my true love sent to me: ", day)); | |
index = DAYS.indexOf(day); | |
for (int i = index; i >= 0; i--) { | |
gift = GIFTS.get(i); | |
if (index > 0 && i == 0) { | |
gift = "and " + gift; | |
} | |
System.out.println(gift); | |
} | |
System.out.println(""); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment