From e7ab02e977160624b5407893b7cf8791ca181326 Mon Sep 17 00:00:00 2001 From: ahsonali Date: Thu, 22 Feb 2018 18:25:18 -0500 Subject: [PATCH] Done, got help from Katrina and Leon --- PlaylistChallenge.iml | 2 + src/main/java/io/zipcoder/Music.java | 61 ++++++++++++++++-- src/test/java/io/zipcoder/MusicTest.java | 32 +++++++-- target/classes/io/zipcoder/Music.class | Bin 0 -> 1336 bytes .../test-classes/io/zipcoder/MusicTest.class | Bin 0 -> 2002 bytes 5 files changed, 83 insertions(+), 12 deletions(-) create mode 100644 target/classes/io/zipcoder/Music.class create mode 100644 target/test-classes/io/zipcoder/MusicTest.class 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 0000000000000000000000000000000000000000..9a1f80aaffb6b8ebda03d431b9f7f9e1b962a5b5 GIT binary patch literal 1336 zcmZux*;dm~6y3M9O&h{cAUM|vuuN7_>i~j?sMRv8RTnS5n6@ci2q`N`QTQrd%K?1w z1N&&oTNyO}Qso^&^71(I{pmBGBgaPB|x@eP6a zQln-I^p&J*uQqoow)e)WIP?sZ8db~Luso^kmNOo_mp+q~WFvndcdLBsV$X`0={;g}C+Vygy=~eCLQcXq= z8-7!@#Xuh(8t6fT0wGkBYhReTV1@Gy$D%2Ox6;(8@#wO6+F)bo?Bo{T2 zD~1qlPtmQ_)u&SY-&*1-zfgl;tUydPoVOV7edyB3tL!wqln2AFq z3(4stXoCL;rl$Pw3Mqw55t%kq1eobsK{u18eu(rT^iSmMf>JbwG|voDRFPrk^UQq} z7cq`Ym_e4SbQ#OI9xC3Aw45kj$61^UD_Rd<#>ny#7x=o1Tnl8#B2LBvIkHHQH;*g4 qCCPq+Ru)>A;~H-%W-^&4OFhfXGs2sWQED@oA>th)ui#77@!TJutSM#y literal 0 HcmV?d00001 diff --git a/target/test-classes/io/zipcoder/MusicTest.class b/target/test-classes/io/zipcoder/MusicTest.class new file mode 100644 index 0000000000000000000000000000000000000000..0bd632d170481e62846904ae1a5cffa32935d3ba GIT binary patch literal 2002 zcmcJP-B%k`6vg*Il9{F5yebN_S{Excacak+p3oM?@!{pwRd(Pdzv(Mz8x4-=fU=A^4m1?1{ zn!C~umJMWmY0GAyTt^4y3|ObOIJcD_7`XL)^;n#Vk}aIWQa$jLbI6S3iB9LQ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