Skip to content

Commit 5694715

Browse files
committed
fix: wrorking to fix code errors in lesson 16
1 parent 8818846 commit 5694715

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.codedifferently.lesson16.justinsplaylist;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class playlist {
7+
private String name;
8+
private String genre;
9+
private int duration;
10+
private List<String> songs;
11+
12+
public playlist(String name, String genre, int duration) {
13+
this.name = name;
14+
this.genre = genre;
15+
this.duration = duration;
16+
this.songs = new ArrayList<>();
17+
}
18+
19+
// Getters and Setters
20+
public String getName() { return name; }
21+
public void setName(String name) { this.name = name; }
22+
23+
public String getGenre() { return genre; }
24+
public void setGenre(String genre) { this.genre = genre; }
25+
26+
public int getDuration() { return duration; }
27+
public void setDuration(int duration) { this.duration = duration; }
28+
29+
public String getDetails() {
30+
return "Name: " + name + ", Genre: " + genre + ", Duration: " + duration;
31+
}
32+
33+
34+
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.codedifferently.lesson16.JustinsPylaylist;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Playlist {
7+
private final List<String> songs = new ArrayList<>();
8+
9+
public void addSong(String song) {
10+
songs.add(song);
11+
}
12+
13+
public void playAll() {
14+
if (songs.isEmpty()) {
15+
throw new IllegalStateException("No songs in the playlist to play.");
16+
}
17+
18+
System.out.println("Playing all songs in the playlist:");
19+
for (String song : songs) {
20+
System.out.println("Playing: " + song);
21+
}
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.codedifferently.lesson16.justinsplaylist;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import static org.assertj.core.api.Assertions.*;
6+
import static org.assertj.core.api.assertThat;
7+
import com.codedifferently.lesson16.JustinsPlaylist.Playlist;
8+
9+
public class playlistTest {
10+
11+
private playlist playlist;
12+
13+
@BeforeEach
14+
public void setUp() {
15+
playlist = new playlist("Chill Vibes", "Lo-Fi", 120);
16+
}
17+
18+
@Test
19+
public void testGetDetailsReturnsCorrectInfo() {
20+
String details = playlist.getDetails();
21+
assertThat(details).isEqualTo("Name: Chill Vibes, Genre: Lo-Fi, Duration: 120");
22+
}
23+
24+
@Test
25+
public void testAddSongSuccessfully() {
26+
playlist.addSong("Evening Walk");
27+
assertThatCode(() -> playlist.playAll()).doesNotThrowAnyException();
28+
}
29+
30+
@Test
31+
public void testAddSongThrowsExceptionOnEmptyInput() {
32+
assertThatThrownBy(() -> Playlist.addSong(" "))
33+
.isInstanceOf(IllegalArgumentException.class)
34+
.hasMessage("Cannot add a null or empty song.");
35+
}
36+
37+
@Test
38+
public void testPlayAllThrowsExceptionWhenNoSongs() {
39+
assertThatThrownBy(() -> playlist.playAll())
40+
.isInstanceOf(IllegalStateException.class)
41+
.hasMessage("No songs in the playlist to play.");
42+
}
43+
44+
@Test
45+
public void testSetAndGetName() {
46+
playlist.setName("Late Night Beats");
47+
assertThat(playlist.getName()).isEqualTo("Late Night Beats");
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.codedifferently.lesson16.playlist.test;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.assertj.core.api.Assertions.*;
7+
8+
public class PlaylistTest {
9+
10+
private playlist playlist;
11+
12+
@BeforeEach
13+
public void setUp() {
14+
playlist = new playlist();
15+
}
16+
17+
@Test
18+
public void testAddSongDoesNotThrow() {
19+
assertThatCode(() -> playlist.addSong("My Favorite Song"))
20+
.doesNotThrowAnyException();
21+
}
22+
23+
@Test
24+
public void testPlayAllThrowsWhenNoSongs() {
25+
assertThatThrownBy(() -> playlist.playAll())
26+
.isInstanceOf(IllegalStateException.class)
27+
.hasMessage("No songs in the playlist to play.");
28+
}
29+
30+
@Test
31+
public void testPlayAllPlaysSongsWhenNotEmpty() {
32+
playlist.addSong("Song 1");
33+
playlist.addSong("Song 2");
34+
35+
// We're not capturing console output here — just making sure no exceptions are thrown
36+
assertThatCode(() -> playlist.playAll())
37+
.doesNotThrowAnyException();
38+
}
39+
}
40+
41+
}

0 commit comments

Comments
 (0)