diff --git a/src/main/java/device/radioPlayer/RadioPlayer.java b/src/main/java/device/radioPlayer/RadioPlayer.java index 5a79bc8..8568110 100644 --- a/src/main/java/device/radioPlayer/RadioPlayer.java +++ b/src/main/java/device/radioPlayer/RadioPlayer.java @@ -158,8 +158,13 @@ public class RadioPlayer implements Device { @Override public String chooseItem(int itemNr) { - playedStation = savedPlaylist.get(itemNr - 1); - return ("Radio is playing station: 0" + (savedPlaylist.indexOf(playedStation) + 1) + " " + this.playedStation + " from saved playlist"); + if(itemNr > savedPlaylist.size()) { + playedStation = savedPlaylist.get(savedPlaylist.size()-1); + return ("Radio is playing station: 0" + savedPlaylist.size() + " " + this.playedStation + " from saved playlist"); + }else{ + playedStation = savedPlaylist.get(itemNr - 1); + return ("Radio is playing station: 0" + (savedPlaylist.indexOf(playedStation) + 1) + " " + this.playedStation + " from saved playlist"); + } } @Override diff --git a/src/test/java/device/radioPlayer/RadioPlayerTest.java b/src/test/java/device/radioPlayer/RadioPlayerTest.java index 6de8fc1..b7fa5ef 100644 --- a/src/test/java/device/radioPlayer/RadioPlayerTest.java +++ b/src/test/java/device/radioPlayer/RadioPlayerTest.java @@ -183,9 +183,12 @@ class RadioPlayerTest { static Stream chooseItemOptions() { RadioPlayer rp = new RadioPlayer(); rp.chooseItem(2); + RadioPlayer rp1 = new RadioPlayer(); + rp1.chooseItem(5); return Stream.of( - Arguments.of("Test select station to play with nr isn`t bigger than playlist size", rp, "Teddy") + Arguments.of("Test select station in saved playlist to play with nr isn`t bigger than playlist size", rp, "Teddy"), + Arguments.of("Test select station in saved playlist to play with nr is bigger than playlist size. Last station in playlist gets played", rp1, "MegaHits") ); }