From 2b0ee697b60772cc5871b04c9ca71ceddfabecb2 Mon Sep 17 00:00:00 2001 From: alpina0707 Date: Tue, 15 Feb 2022 00:28:29 +0100 Subject: [PATCH] added new test case for deleteStation() & code for saved station delete until there is only one station in savedPlaylist --- .../java/device/radioPlayer/RadioPlayer.java | 14 +++++++-- .../device/radioPlayer/RadioPlayerTest.java | 29 +++++++++++++++++-- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/main/java/device/radioPlayer/RadioPlayer.java b/src/main/java/device/radioPlayer/RadioPlayer.java index 258eff5..12e4148 100644 --- a/src/main/java/device/radioPlayer/RadioPlayer.java +++ b/src/main/java/device/radioPlayer/RadioPlayer.java @@ -187,9 +187,19 @@ public class RadioPlayer implements Device { public String saveStation() { if (savedPlaylist.contains(playedStation)) return "Station " + playedStation + " is already saved"; - else - savedPlaylist.add(playedStation); + else savedPlaylist.add(playedStation); regionPlaylist.clear(); return "Station " + playedStation + " is saved in your Station list"; } + + + public String deleteStation() { + String station = playedStation; + if ((savedPlaylist.size()) > 1) { + savedPlaylist.remove(playedStation); + playedStation = savedPlaylist.get(0); + return "Station: 0" + (savedPlaylist.indexOf(playedStation) + 1) + " " + station + " has been deleted"; + } else return " Last Station: 01 " + station + " canĀ“t by deleted"; + } + } diff --git a/src/test/java/device/radioPlayer/RadioPlayerTest.java b/src/test/java/device/radioPlayer/RadioPlayerTest.java index 225f0f6..c272ddb 100644 --- a/src/test/java/device/radioPlayer/RadioPlayerTest.java +++ b/src/test/java/device/radioPlayer/RadioPlayerTest.java @@ -196,11 +196,11 @@ class RadioPlayerTest { @ParameterizedTest @MethodSource("saveStationOptions") - void testSaveStation(String testName, RadioPlayer testRp,String testTyp, String expectedResult) { + void testSaveStation(String testName, RadioPlayer testRp, String testTyp, String expectedResult) { String savedStation; if (testTyp.equals("normal")) { - savedStation = testRp.savedPlaylist.get(testRp.savedPlaylist.size() - 1);} - else savedStation = Boolean.toString(testRp.regionPlaylist.isEmpty()); + savedStation = testRp.savedPlaylist.get(testRp.savedPlaylist.size() - 1); + } else savedStation = Boolean.toString(testRp.regionPlaylist.isEmpty()); assertThat(savedStation).describedAs(testName).isEqualTo(expectedResult); } @@ -221,4 +221,27 @@ class RadioPlayerTest { ); } + @ParameterizedTest + @MethodSource("deleteStationOptions") + void testDeleteStation(String testName, RadioPlayer testRp, String testTyp, Boolean expectedResult) { + Boolean savedStation; + if (testTyp.equals("delete")) { + savedStation = testRp.savedPlaylist.contains("YouFM"); + } else savedStation = testRp.savedPlaylist.contains("MegaHits"); + assertThat(savedStation).describedAs(testName).isEqualTo(expectedResult); + } + + static Stream deleteStationOptions() { + RadioPlayer rp = new RadioPlayer(); + rp.deleteStation(); + RadioPlayer rp1 = new RadioPlayer(); + rp1.deleteStation(); + rp1.deleteStation(); + rp1.deleteStation(); + + return Stream.of( + Arguments.of("Test for delete station in saved playlist", rp, "delete", false), + Arguments.of("Test for not delete station in saved playlist if this is the last station", rp1, "noDelete", true) + ); + } } \ No newline at end of file