From acfc2fef2b549e88cefb7ac26fcac6ad14ff8eba Mon Sep 17 00:00:00 2001 From: alpina0707 Date: Wed, 16 Feb 2022 19:05:22 +0100 Subject: [PATCH] added fifth test case for chooseItem() & and added productive code for RadioPlayer --- src/main/java/device/radioPlayer/RadioPlayer.java | 9 +++++++-- src/test/java/device/radioPlayer/RadioPlayerTest.java | 6 +++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/device/radioPlayer/RadioPlayer.java b/src/main/java/device/radioPlayer/RadioPlayer.java index 42a5159..ca25596 100644 --- a/src/main/java/device/radioPlayer/RadioPlayer.java +++ b/src/main/java/device/radioPlayer/RadioPlayer.java @@ -159,8 +159,13 @@ public class RadioPlayer implements Device { @Override public String chooseItem(int itemNr) { if (regionPlaylist.contains(playedStation)) { - playedStation = regionPlaylist.get(itemNr - 1); - return ("Radio is playing station: 0" + (regionPlaylist.indexOf(playedStation) + 1) + " " + this.playedStation + " from regional playlist"); + if (itemNr > regionPlaylist.size()) { + playedStation = regionPlaylist.get(regionPlaylist.size() - 1); + return ("Radio is playing station: 0" + regionPlaylist.size() + " " + this.playedStation + " from regional playlist"); + } else { + playedStation = regionPlaylist.get(itemNr - 1); + return ("Radio is playing station: 0" + (regionPlaylist.indexOf(playedStation) + 1) + " " + this.playedStation + " from regional playlist"); + } } else { if (itemNr > savedPlaylist.size()) { playedStation = savedPlaylist.get(savedPlaylist.size() - 1); diff --git a/src/test/java/device/radioPlayer/RadioPlayerTest.java b/src/test/java/device/radioPlayer/RadioPlayerTest.java index db2b35c..aa7417c 100644 --- a/src/test/java/device/radioPlayer/RadioPlayerTest.java +++ b/src/test/java/device/radioPlayer/RadioPlayerTest.java @@ -190,12 +190,16 @@ class RadioPlayerTest { RadioPlayer rp3 = new RadioPlayer(); rp3.changeRegion("BY"); rp3.chooseItem(2); + RadioPlayer rp4 = new RadioPlayer(); + rp4.changeRegion("BY"); + rp4.chooseItem(5); 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 lower than 0. First station in playlist gets played", rp2, "YouFM"), - Arguments.of("Test select station in regional playlist to play with nr isn`t bigger than playlist size", rp3, "Bayern 1") + Arguments.of("Test select station in regional playlist to play with nr isn`t bigger than playlist size", rp3, "Bayern 1"), + Arguments.of("Test select station in regional playlist to play with nr is bigger than playlist size. Last station in playlist gets played", rp4, "Hit Radio N1") ); }