diff --git a/PlaylistChallenge.iml b/PlaylistChallenge.iml index 54ddd53..e0c2965 100644 --- a/PlaylistChallenge.iml +++ b/PlaylistChallenge.iml @@ -13,5 +13,7 @@ + + \ 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..73a2ef3 100644 --- a/src/main/java/io/zipcoder/Music.java +++ b/src/main/java/io/zipcoder/Music.java @@ -1,14 +1,61 @@ package io.zipcoder; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + public class Music { private String[] playList; - public Music(String[] playList){ - this.playList = playList; - } + public Music(String[] playList) + {this.playList = playList;} + + public Integer selection(Integer startIndex, String selection) + { + //Use an ArrayList + List songList = Arrays.asList(this.playList); + + /*The formula for forward takes the index of the selection + and minus the startIndex then Math.abs*/ + int indexOfSelection = songList.indexOf(selection); + int indexDifference = indexOfSelection - startIndex; + int forwardClicks = Math.abs(indexDifference); + + /*The formula for backwardClicks adds the size of the array plus the startIndex + * and subtracts the last index of the array. In essance it finds the distance without + * physically looping backwards */ + int listSize = songList.size(); + int sizePlusStart = listSize + startIndex; + int lastIndexOfSelection = songList.lastIndexOf(selection); + int backwardClicks = Math.abs(sizePlusStart) - lastIndexOfSelection; + + + //then compares the two values for backwardClicks and forwardClicks and returns the minimum + return Math.min(forwardClicks, backwardClicks); + + }} - public Integer selection(Integer startIndex, String selection){ - return null; - } -} +//// public Integer selectionn(Integer startIndex, String selection) +//// { +//// //Use an ArrayList +//// ArrayList songList = new ArrayList(Arrays.asList(this.playList)); +//// +//// /*The formula for forward takes the index of the selection +//// and minus the startIndex then Math.abs*/ +//// +//// int forwardClicks = Math.abs(songList.indexOf(selection) - startIndex); +//// +//// /*The formula for backwardClicks adds the size of the array plus the startIndex +//// * and subtracts the last index of the array. In essance it finds the distance without +//// * physically looping backwards */ +//// +//// int backwardClicks = Math.abs(songList.size() + startIndex) - (songList.lastIndexOf(selection)); +//// +//// +//// //then compares the two values for backwardClicks and forwardClicks and returns the minimum +//// +//// return Math.min(forwardClicks, backwardClicks); +//// +//// } +//} diff --git a/src/test/java/io/zipcoder/MusicTest.java b/src/test/java/io/zipcoder/MusicTest.java index 03ede61..56c758c 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,26 @@ public void selectionTest2(){ Integer actual = music.selection(startingIndex, selection); Assert.assertEquals(expected, actual); } -} + + @Test + public void selectionTest3() { + String[] playList = {"Changes", "I Will Always Love You", "Trap Queen", "Changes", "Can't Be Touched"}; + Music music = new Music(playList); + Integer startingIndex = 1; + String selection = "Changes"; + Integer expected = 1; + Integer actual = music.selection(startingIndex, selection); + Assert.assertEquals(expected, actual); } + + @Test + public void selectionTest4() + { + String [] playList = {"Changes", "I Will Always Love You", "Trap Queen", "You Came To Do This", "I Miss You", "Changes"}; + Music music = new Music(playList); + Integer startingIndex = 2; + String selection = "Changes"; + Integer expected = 2; + Integer actual = music.selection(startingIndex, selection); + Assert.assertEquals(expected, actual); + } +} \ No newline at end of file diff --git a/target/classes/io/zipcoder/Music.class b/target/classes/io/zipcoder/Music.class new file mode 100644 index 0000000..9a1f80a 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..0bd632d Binary files /dev/null and b/target/test-classes/io/zipcoder/MusicTest.class differ