<< Back to Object Oriented Programming Portfolio

Album with Music Assignment


Album class

import java.util.ArrayList;

public class Album
{
    private ArrayList<Song> songs;
    private String artist;

    public Album(String artist)
    {
        this.artist = artist;
        this.songs = new ArrayList<>();
    }

    public void addSong(Song song)
    {
        this.songs.add(song);
    }

    public Song getMostPopularSong()
    {
        Song mostPopular = this.songs.get(0);
        for (Song song : this.songs)
        {
            if (song.getTimesPlayed() > mostPopular.getTimesPlayed())
            {
                mostPopular = song;
            }
        }

        return mostPopular;
    }

    public int getTotalPlayTime()
    {
        int totalPlayTime = 0;
        for (Song song : this.songs)
        {
            totalPlayTime += song.getPlayTimeInSeconds();
        }

        return totalPlayTime;
    }

    public boolean isRoadTripWorthy()
    {
        return (this.getTotalPlayTime() / 60) > 60;
    }

    public ArrayList<Song> getFilteredSongs(String filter)
    {
        ArrayList<Song> filteredSongs = new ArrayList<>();
        for (Song song : this.songs)
        {
            if (song.getTitle().contains(filter))
            {
                filteredSongs.add(song);
            }
        }

        return filteredSongs;
    }
}

Song class

public class Song
{
    private String title;
    private int playTimeInSeconds;
    private int timesPlayed;

    public Song(String title, int playTimeInSeconds)
    {
        this.title = title;
        this.playTimeInSeconds = playTimeInSeconds;
        this.timesPlayed = 0;
    }

    public String getTitle()
    {
        return this.title;
    }

    public int getPlayTimeInSeconds()
    {
        return this.playTimeInSeconds;
    }

    public int getTimesPlayed()
    {
        return this.timesPlayed;
    }

    public void play()
    {
        this.timesPlayed++;
    }
}