Untitled

mail@pastecode.io avatar
unknown
plain_text
14 days ago
3.6 kB
2
Indexable
Never
class Song {
    private String title;
    private String artist;
    private int length; // length of the song in seconds

    public Song(String title, String artist, int minutes, int seconds) {
        this.title = title;
        this.artist = artist;
        this.length = minutes * 60 + seconds;
    }

    public String getTitle() {
        return title;
    }

    public String getArtist() {
        return artist;
    }

    public int getLength() {
        return length;
    }

    public void display() {
        int minutes = length / 60;
        int sec = length % 60;
        System.out.printf("%s (%02d:%02d)\n", artist, minutes, sec);
    }
}

class Album {
    private String title;
    private String artist;
    private Song[] tracklist;

    public Album(String title, String artist, Song[] tracklist) {
        this.title = title;
        this.artist = artist;
        this.tracklist = new Song[tracklist.length];
        System.arraycopy(tracklist, 0, this.tracklist, 0, tracklist.length);
    }

    public String getTitle() {
        return title;
    }

    public String getArtist() {
        return artist;
    }

    public int getNumTracks() {
        return tracklist.length;
    }

    public Song getTrack(int trackNumber) {
        if (trackNumber > 0 && trackNumber <= tracklist.length) {
            return tracklist[trackNumber - 1];
        }
        return null;
    }

    public boolean comesBefore(Album otherAlbum) {
        int artistComparison = artist.compareToIgnoreCase(otherAlbum.getArtist());
        if (artistComparison < 0 || (artistComparison == 0 && title.compareToIgnoreCase(otherAlbum.getTitle()) < 0)) {
            return true;
        }
        return false;
    }

    public void displayAlbum() {
        System.out.printf("- %s (%d track%s)\n", artist, tracklist.length, tracklist.length == 1 ? "" : "s");
    }

    public void displayTracklist() {
        for (int i = 0; i < tracklist.length; i++) {
            System.out.printf("%2d. %s\n", i + 1, tracklist[i].getTitle());
        }
    }
}

class AlbumCollection {
    public static final int MAX_ALBUMS = 100;

    private int numAlbums;
    private Album[] albums;

    public AlbumCollection() {
        numAlbums = 0;
        albums = new Album[MAX_ALBUMS];
    }

    public int getNumAlbums() {
        return numAlbums;
    }

    public boolean addAlbum(Album album) {
        if (numAlbums < MAX_ALBUMS) {
            albums[numAlbums] = album;
            numAlbums++;
            return true;
        }
        return false;
    }

    public Album findAlbum(String title, String artist) {
        for (int i = 0; i < numAlbums; i++) {
            if (albums[i].getArtist().equalsIgnoreCase(artist) && albums[i].getTitle().equalsIgnoreCase(title)) {
                return albums[i];
            }
        }
        return null;
    }

    public void sortAlbums() {
        for (int i = 0; i < numAlbums - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < numAlbums; j++) {
                if (albums[j].comesBefore(albums[minIndex])) {
                    minIndex = j;
                }
            }
            if (minIndex != i) {
                Album temp = albums[i];
                albums[i] = albums[minIndex];
                albums[minIndex] = temp;
            }
        }
    }

    public void displayAlbums() {
        for (int i = 0; i < numAlbums; i++) {
            albums[i].displayAlbum();
        }
    }

    public void displaySongs() {
        for (int i = 0; i < numAlbums; i++) {
            albums[i].displayTracklist();
        }
    }
}
Leave a Comment