From 820d148f3000d97e09ae57794502b5c17286b48d Mon Sep 17 00:00:00 2001 From: alpina0707 Date: Wed, 16 Feb 2022 18:19:37 +0100 Subject: [PATCH] added second 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 | 5 ++++- 2 files changed, 11 insertions(+), 3 deletions(-) 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") ); }