diff --git a/.idea/misc.xml b/.idea/misc.xml index e8942bd..c5f67dd 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -7,7 +7,7 @@ - + \ No newline at end of file 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..645c526 100644 --- a/src/main/java/io/zipcoder/Music.java +++ b/src/main/java/io/zipcoder/Music.java @@ -1,5 +1,7 @@ package io.zipcoder; +import java.util.List; + public class Music { private String[] playList; @@ -8,7 +10,43 @@ public Music(String[] playList){ this.playList = playList; } - public Integer selection(Integer startIndex, String selection){ - return null; + public Integer selection(Integer startIndex, String selection) { + return pressCounts(startIndex, selection); + } + + public Integer pressCounts (Integer startIndex, String selection) { + int pressNext = pressForwards(startIndex, selection); + int pressPrevious = pressBackwards(startIndex ,selection); + if (pressNext > pressPrevious) { + return pressPrevious; + } + return pressNext; + } + + public Integer pressForwards(Integer startIndex, String selection) { + int skipCount = 0; + int length = playList.length -1; + while (!playList[startIndex].equals(selection)) { + skipCount ++; + if (startIndex ==0) { + startIndex = length; + } else { + startIndex --; + } + } return skipCount; + } + + + public int pressBackwards (Integer startIndex, String selection) { + int skipCount = 0; + int length = playList.length -1; + while (!playList[startIndex].equals(selection)) { + skipCount ++; + if (startIndex ==length) { + startIndex = 0; + } else { + startIndex ++; + } + } return skipCount; } } diff --git a/target/classes/io/zipcoder/Music.class b/target/classes/io/zipcoder/Music.class new file mode 100644 index 0000000..983aa21 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..3a5845d Binary files /dev/null and b/target/test-classes/io/zipcoder/MusicTest.class differ