import java.util.Iterator;
import java.util.ArrayList;
/**
* A class to hold details of audio tracks.
* Individual tracks may be played.
*
* @author David J. Barnes and Michael Kölling
* @version 2016.02.29
* @author CAC, Modified 9/30/24 to add Diddy-related stuff.
*/
public class MusicOrganizer
{
// An ArrayList for storing music tracks.
private ArrayList<Track> tracks;
// A player for the music tracks.
private MusicPlayer player;
// A reader that can read music files and load them as tracks.
private TrackReader reader;
/**
* Create a MusicOrganizer
*/
public MusicOrganizer()
{
tracks = new ArrayList<>();
player = new MusicPlayer();
reader = new TrackReader();
readLibrary("../audio");
System.out.println("Music library loaded. " + getNumberOfTracks() + " tracks.");
System.out.println();
}
public int getTotalPlayingTime() {
int totalTime = 0;
for(Track track: tracks) {
totalTime+=track.getPlayingTime();
}
return totalTime;
}
public ArrayList<Track> get99MinuteTracks() {
ArrayList<Track> ninetyNiners = new ArrayList<>();
for(Track track: tracks) {
if(track.getPlayingTime()==99) {
ninetyNiners.add(track);
}
}
return ninetyNiners;
}
/**
* Add a track file to the collection.
* @param filename The file name of the track to be added.
*/
public void addFile(String filename)
{
tracks.add(new Track(filename));
}
/**
* Add a track to the collection.
* @param track The track to be added.
*/
public void addTrack(Track track)
{
tracks.add(track);
}
/**
* Play a track in the collection.
* @param index The index of the track to be played.
*/
public void playTrack(int index)
{
if(indexValid(index)) {
Track track = tracks.get(index);
player.startPlaying(track.getFilename());
System.out.println("Now playing: " + track.getArtist() + " - " + track.getTitle());
}
}
/**
* Return the number of tracks in the collection.
* @return The number of tracks in the collection.
*/
public int getNumberOfTracks()
{
return tracks.size();
}
/**
* List a track from the collection.
* @param index The index of the track to be listed.
*/
public void listTrack(int index)
{
System.out.print("Track " + index + ": ");
Track track = tracks.get(index);
System.out.println(track.getDetails());
}
/**
* Show a list of all the tracks in the collection.
*/
public void listAllTracks()
{
System.out.println("Track listing: ");
for(Track track : tracks) {
System.out.println(track.getDetails());
}
System.out.println();
}
/**
* List all tracks by the given artist.
* @param artist The artist's name.
*/
public void listByArtist(String artist)
{
for(Track track : tracks) {
if(track.getArtist().contains(artist)) {
System.out.println(track.getDetails());
}
}
}
public void ditchDiddy() {
Iterator<Track> iter = tracks.iterator();
while(iter.hasNext()) {
if(iter.next().getArtist().equals("P. Diddy")) {
iter.remove();
}
}
}
/**
* Remove all tracks with "Diddy" anywhere in the Artist name, and return
* an array list of all of the deleted tracks. (An empty list if none were deleted.)
*
* @return An ArrayList of the Tracks that were deleted.
*/
public ArrayList<Track> ditchDiddyLike() {
ArrayList<Track> diddyLike = new ArrayList<>();
Iterator<Track> iter = tracks.iterator();
while(iter.hasNext()) {
Track current = iter.next();
if(current.getArtist().contains("Diddy")) {
diddyLike.add(current);
iter.remove();
}
}
return diddyLike;
}
public ArrayList<Integer> whichDiddyLike() {
ArrayList<Integer> diddyLike = new ArrayList<>();
for(int i=0;i<tracks.size();i++) {
Track track = tracks.get(i);
if(track.getArtist().contains("Diddy")) {
diddyLike.add(i);
}
}
return diddyLike;
}
/**
* Remove a track from the collection.
* @param index The index of the track to be removed.
*/
public void removeTrack(int index)
{
if(indexValid(index)) {
tracks.remove(index);
}
}
/**
* Play the first track in the collection, if there is one.
*/
public void playFirst()
{
if(tracks.size() > 0) {
player.startPlaying(tracks.get(0).getFilename());
}
}
/**
* Stop the player.
*/
public void stopPlaying()
{
player.stop();
}
/**
* Determine whether the given index is valid for the collection.
* Print an error message if it is not.
* @param index The index to be checked.
* @return true if the index is valid, false otherwise.
*/
private boolean indexValid(int index)
{
// The return value.
// Set according to whether the index is valid or not.
boolean valid;
if(index < 0) {
System.out.println("Index cannot be negative: " + index);
valid = false;
}
else if(index >= tracks.size()) {
System.out.println("Index is too large: " + index);
valid = false;
}
else {
valid = true;
}
return valid;
}
private void readLibrary(String folderName)
{
ArrayList<Track> tempTracks = reader.readTracks(folderName, ".mp3");
// Put all thetracks into the organizer.
for(Track track : tempTracks) {
addTrack(track);
}
}
}