Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/main/java/io/zipcoder/Music.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,24 @@ public Music(String[] playList){
this.playList = playList;
}

public Integer selection(Integer startIndex, String selection){
return null;
public Integer selection(Integer startIndex, String selection) {
int distance = 0;
int selectionIndex = 0;

for (int i = startIndex; i < playList.length+1; i++) {
distance++;
if (i >= playList.length) {
i = 0;
}
if (playList[i].equals(selection)) {
selectionIndex = i;
}
if (distance >= playList.length) break;
}

int dist1 = Math.abs(selectionIndex - startIndex);
int dist2 = startIndex + playList.length - selectionIndex;
distance = Math.min(dist1, dist2);
return distance;
}
}
24 changes: 24 additions & 0 deletions src/test/java/io/zipcoder/MusicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,28 @@ public void selectionTest2(){
Integer actual = music.selection(startingIndex, selection);
Assert.assertEquals(expected, actual);
}

@Test
public void selectionTest3(){

String[] playlist = {"dancinginthedark","rio","liveoak","liveoak"};
Music music = new Music(playlist);
Integer startingIndex = 0;
String selection = "rio";
Integer expected = 1;
Integer actual = music.selection(startingIndex, selection);
Assert.assertEquals(expected, actual);
}

@Test
public void selectionTest4(){

String[] playlist = {"wheniseeyouagain","borntorun","nothingelsematters","cecelia"};
Music music = new Music(playlist);
Integer startingIndex = 1;
String selection = "nothingelsematters";
Integer expected = 1;
Integer actual = music.selection(startingIndex, selection);
Assert.assertEquals(expected, actual);
}
}