From cd09cb87758069b47552ec7fd0e3629d9276ffa7 Mon Sep 17 00:00:00 2001 From: alpina0707 Date: Wed, 16 Feb 2022 18:55:20 +0100 Subject: [PATCH] added third test case for chooseItem() & and added productive code for RadioPlayer --- .../java/device/radioPlayer/RadioPlayer.java | 27 ++++++++++--------- .../device/radioPlayer/RadioPlayerTest.java | 5 +++- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/main/java/device/radioPlayer/RadioPlayer.java b/src/main/java/device/radioPlayer/RadioPlayer.java index 8568110..5e9d7fe 100644 --- a/src/main/java/device/radioPlayer/RadioPlayer.java +++ b/src/main/java/device/radioPlayer/RadioPlayer.java @@ -158,12 +158,16 @@ public class RadioPlayer implements Device { @Override public String chooseItem(int itemNr) { - if(itemNr > savedPlaylist.size()) { - playedStation = savedPlaylist.get(savedPlaylist.size()-1); + if (itemNr > savedPlaylist.size()) { + playedStation = savedPlaylist.get(savedPlaylist.size() - 1); return ("Radio is playing station: 0" + savedPlaylist.size() + " " + this.playedStation + " from saved playlist"); - }else{ + } else if (itemNr < 1) { + playedStation = savedPlaylist.get(0); + return ("Radio is playing station: 01 " + 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"); + } } @@ -214,7 +218,7 @@ public class RadioPlayer implements Device { } public String changeToSavedPlaylist() { - if(regionPlaylist.contains(playedStation)) { + if (regionPlaylist.contains(playedStation)) { playedStation = savedPlaylist.get(0); regionPlaylist.clear(); return "Playlist switched now playing station: 0" + (savedPlaylist.indexOf(playedStation) + 1) + " " + this.playedStation + " from saved playlist"; @@ -224,16 +228,15 @@ public class RadioPlayer implements Device { public String changeOrderInSavedPlaylist(int nr) { String station = playedStation; - if((nr-1) > savedPlaylist.size()) { + if ((nr - 1) > savedPlaylist.size()) { savedPlaylist.remove(playedStation); - savedPlaylist.add(savedPlaylist.size(),station); - return"Station " + playedStation + " is now on place 0" + (savedPlaylist.size()+1) + "in saved playlist"; - }else if(nr < 0) { + savedPlaylist.add(savedPlaylist.size(), station); + return "Station " + playedStation + " is now on place 0" + (savedPlaylist.size() + 1) + "in saved playlist"; + } else if (nr < 0) { savedPlaylist.remove(playedStation); - savedPlaylist.add(0,station); - return"Station " + playedStation + " is now on place 01 in saved playlist"; - } - else { + savedPlaylist.add(0, station); + return "Station " + playedStation + " is now on place 01 in saved playlist"; + } else { savedPlaylist.remove(playedStation); savedPlaylist.add(nr - 1, station); return "Station " + playedStation + " is now on place 0" + nr + "in saved playlist"; diff --git a/src/test/java/device/radioPlayer/RadioPlayerTest.java b/src/test/java/device/radioPlayer/RadioPlayerTest.java index b7fa5ef..6e6121c 100644 --- a/src/test/java/device/radioPlayer/RadioPlayerTest.java +++ b/src/test/java/device/radioPlayer/RadioPlayerTest.java @@ -185,10 +185,13 @@ class RadioPlayerTest { rp.chooseItem(2); RadioPlayer rp1 = new RadioPlayer(); rp1.chooseItem(5); + RadioPlayer rp2 = new RadioPlayer(); + rp2.chooseItem(-1); return Stream.of( 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") + 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"), + Arguments.of("Test select station in saved playlist to play with nr is lower than 0. First station in playlist gets played", rp2, "YouFM") ); }