Commit bcedef7e authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch10c] Jukebox code for the actual book

parent a716af49
Loading
Loading
Loading
Loading
+43 −0
Changes for src/main/java/ch10c/JukeboxStreams.java: 43 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch10c;

import java.util.List;
import java.util.stream.Collectors;

public class JukeboxStreams {
  public static void main(String[] args) {
    List<Song> songs = new Songs().getSongs();
    List<Song> rockSongs = songs.stream()
                           .filter(song -> song.getGenre().contains("Rock"))
                           .collect(Collectors.toList());
    System.out.println(rockSongs);
  }
}

class Songs {
  public List<Song> getSongs() {
    return List.of(
            new Song("$10", "Hitchhiker", "Electronic", 2016, 183),
            new Song("Havana", "Camila Cabello", "R&B", 2017, 324),
            new Song("Cassidy", "Grateful Dead", "Rock", 1972, 123),
            new Song("50 ways", "Paul Simon", "Soft Rock", 1975, 199),
            new Song("Hurt", "Nine Inch Nails", "Industrial Rock", 1995, 257),
            new Song("Silence", "Delerium", "Electronic", 1999, 134),
            new Song("Hurt", "Johnny Cash", "Soft Rock", 2002, 392),
            new Song("Watercolour", "Pendulum", "Electronic", 2010, 155),
            new Song("The Outsider", "A Perfect Circle", "Alternative Rock", 2004, 312),
            new Song("With a Little Help from My Friends", "The Beatles", "Rock", 1967, 168),
            new Song("Come Together", "The Beatles", "Blues rock", 1968, 173),
            new Song("Come Together", "The Smokin' Mojo Filters", "Rock", 1995, 165),
            new Song("With a Little Help from My Friends", "Joe Cocker", "Rock", 1968, 46),
            new Song("Immigrant Song", "Karen O", "Industrial Rock", 2011, 12),
            new Song("Breathe", "The Prodigy", "Electronic", 1996, 337),
            new Song("What's Going On", "Gaye", "R&B", 1971, 420),
            new Song("Hallucinate", "Dua Lipa", "Pop", 2020, 75),
            new Song("Walk Me Home", "P!nk", "Pop", 2019, 459),
            new Song("I am not a woman, I'm a god", "Halsey", "Alternative Rock", 2021, 384),
            new Song("Pasos de cero", "Pablo Alborán", "Latin", 2014, 117),
            new Song("Smooth", "Santana", "Latin", 1999, 244),
            new Song("Immigrant song", "Led Zeppelin", "Rock", 1970, 484));
  }

}
 No newline at end of file
+5 −7
Changes for src/main/java/ch10c/Song.java: 5 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -37,12 +37,10 @@ public class Song {

  @Override
  public String toString() {
    return "Song{" +
            "title='" + title + '\'' +
            ", artist='" + artist + '\'' +
            ", genre='" + genre + '\'' +
            ", year=" + year +
            ", timesPlayed=" + timesPlayed +
            '}';
    return title + ", "
            + artist + ", "
            + genre + ", "
            + year + ", "
            + timesPlayed + "\n";
  }
}
+3 −1
Changes for src/test/java/ch10c/JukeboxQueriesTest.java: 3 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ class JukeboxQueriesTest {
  @Test
  void shouldOutputAllSongsPlayed() {
    List<Song> allSongs = songs.getSongs();
    allSongs.forEach(song -> System.out.println(song));

    System.out.println(allSongs);
  }
@@ -81,6 +82,7 @@ class JukeboxQueriesTest {
  void shouldQueryForGenre() {
    List<Song> allSongs = songs.getSongs();
    // ORDER MATTERS
    // CANDIDATE
    // find all genres in played music
    List<String> genres = allSongs.stream()
                                  .map(song -> song.getGenre())
@@ -133,7 +135,7 @@ class JukeboxQueriesTest {
    System.out.println("result = " + genreCount);
  }

  // CANDIDATE
  // CANDIDATE: exercise 1
  // STRAIGHTFORWARD
  // filter
  // toList