diff --git a/.idea/$PRODUCT_WORKSPACE_FILE$ b/.idea/$PRODUCT_WORKSPACE_FILE$ new file mode 100644 index 0000000..9fba8c6 --- /dev/null +++ b/.idea/$PRODUCT_WORKSPACE_FILE$ @@ -0,0 +1,30 @@ + + + + + + + 1.8.0_201 + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..2bb69ef --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..3e62a0b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..5103107 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/PlaylistChallenge.iml b/PlaylistChallenge.iml new file mode 100644 index 0000000..0ddf51c --- /dev/null +++ b/PlaylistChallenge.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/io/zipcoder/Music.java b/src/main/java/io/zipcoder/Music.java index 180c65c..4f72e27 100644 --- a/src/main/java/io/zipcoder/Music.java +++ b/src/main/java/io/zipcoder/Music.java @@ -9,6 +9,39 @@ public Music(String[] playList){ } public Integer selection(Integer startIndex, String selection){ - return null; + int clickUp = 0; + int clickDown = 0; + + //move forward through the array + for(int i = startIndex ; i <= playList.length; i++){ + + if(playList[i].equals(selection)){ + break; + } + clickUp++; + /*if we get to the end of the playlist and don't find our song, we'll have to + start at the other end and keep on counting, bewaring of an infinite loop + */ + if (i == (playList.length - 1)) { + i = -1; + } + } + + //move backwards through the array + for(int j = startIndex; j >= 0 ; j--){ + + if(playList[j].equals(selection)){ + break; + } + clickDown++; + if (j == 0) { + j = playList.length; + } + } + + //return whichever counter is smaller + //Math.min was auto-suggested from an if statement + //it will return the smaller number + return Math.min(clickUp, clickDown); } } diff --git a/src/test/java/io/zipcoder/MusicTest.java b/src/test/java/io/zipcoder/MusicTest.java index 03ede61..9a9ce71 100644 --- a/src/test/java/io/zipcoder/MusicTest.java +++ b/src/test/java/io/zipcoder/MusicTest.java @@ -6,9 +6,9 @@ public class MusicTest { @Test - public void selectionTest1(){ + public void selectionTest1() { - String[] playlist = {"wheniseeyouagain","borntorun","nothingelsematters","cecelia"}; + String[] playlist = {"wheniseeyouagain", "borntorun", "nothingelsematters", "cecelia"}; Music music = new Music(playlist); Integer startingIndex = 1; String selection = "cecelia"; @@ -18,9 +18,9 @@ public void selectionTest1(){ } @Test - public void selectionTest2(){ + public void selectionTest2() { - String[] playlist = {"dancinginthedark","rio","liveoak","liveoak"}; + String[] playlist = {"dancinginthedark", "rio", "liveoak", "liveoak"}; Music music = new Music(playlist); Integer startingIndex = 0; String selection = "liveoak"; @@ -28,4 +28,46 @@ public void selectionTest2(){ Integer actual = music.selection(startingIndex, selection); Assert.assertEquals(expected, actual); } + + @Test + public void selectionTest3() { + + String[] playlist = {"heyjude", "inmylife", "letitbe", "ijustseenaface", + "isawherstandingthere", "tickettoride", "help", "paperbackwriter", "harddaysnight"}; + Music music = new Music(playlist); + Integer startingIndex = 2; + String selection = "tickettoride"; + Integer expected = 3; + Integer actual = music.selection(startingIndex, selection); + Assert.assertEquals(expected, actual); + } + + //test to wrap around the bottom of the array + @Test + public void selectionTest4() { + + String[] playlist = {"bohemianrhapsody", "killerqueen", "fatbottomedgirls", "underpressure", + "iwantobreakfree", "somebodytolove", "flashgordontheme", "crazylittlethingcalledlove", "wearethechampions"}; + Music music = new Music(playlist); + Integer startingIndex = 1; + String selection = "crazylittlethingcalledlove"; + Integer expected = 3; + Integer actual = music.selection(startingIndex, selection); + Assert.assertEquals(expected, actual); + } + + + //test to wrap around the top of the array + @Test + public void selectionTest5() { + String[] playlist = {"tinydancer", "downtletthesungodownonme", "yoursong", "saturday", + "bennyandthejets", "crocodilerock"}; + Music music = new Music(playlist); + Integer startingIndex = 5; + String selection = "tinydancer"; + Integer expected = 1; + Integer actual = music.selection(startingIndex, selection); + Assert.assertEquals(expected, actual); + } } + diff --git a/target/classes/io/zipcoder/Music.class b/target/classes/io/zipcoder/Music.class new file mode 100644 index 0000000..23c2df4 Binary files /dev/null and b/target/classes/io/zipcoder/Music.class differ diff --git a/target/test-classes/io/zipcoder/MusicTest.class b/target/test-classes/io/zipcoder/MusicTest.class new file mode 100644 index 0000000..7e4446c Binary files /dev/null and b/target/test-classes/io/zipcoder/MusicTest.class differ