(JAVA) Media Library Your company is bidding for a contract to develop an online
ID: 3681443 • Letter: #
Question
(JAVA) Media Library
Your company is bidding for a contract to develop an online media library (e.g. iTunes, Soundcloud) consisting of songs, albums and playlists.
Your solution should consist of the following classes:
Song
- Title of the song
- Artist of the song
- Release year for the song
- Catalog Number (a unique identifier for internal tracking purposes)
- Duration of the song (in minutes and seconds)
- Number of times the song has been played
Playlist
- Name of the playlist
- The date the playlist was created (should be automatically generated fromthe actual current date)
- The date it was last updated (this would be the date a song was last added or removed – see next section)
- Collection of songs on the playlist
Playlist need the following functionality:
The ability to add song to playlist
The ability to remove song from playlist
The ability to display the contents of the playlist
The ability to sort the playlist by song title, artist, year, and number of times played
The user should be able to perform the following tasks in a menu-driven system
Add song to playlist
Remove song from playlist
Given the name of an artist, list all songs by the artist in alphabetical order by song title
List all songs in descending order by number of downloads
Given the name of a playlist, list all songs in playlist
Given the title of a song, list the playlist(s) it is in (a song can be in more than one playlist or in none at all)
Browse a playlist sorted by title, artist, year, and number of times played
Your program should have input validation, error-checking and handling.
Remember that your company does not have this contract yet, but is bidding for it and competing with others, so your solution should be as robust and accurate as possible. The contract will be awarded based on program correctness, user friendliness and modularity.
Explanation / Answer
Song.java
public class Song
{
private String title; //- Title of the song
private String artist; //- Artist of the song
private int year; //- Release year for the song
private int catalog; //- Catalog Number (a unique identifier for internal tracking purposes)
private double duration; //- Duration of the song (in minutes and seconds)
private int timesPlayed; //- Number of times the song has been played
public Song()
{
this.title = "";
this.artist = "";
this.year = 0;
this.catalog = 0;
this.duration = 0.0;
this.timesPlayed = 0;
}
public Song(String title, String artist, int year, int catalog, double duration, int timesPlayed)
{
this.title = title;
this.artist = artist;
this.year = year;
this.catalog = catalog;
this.duration = duration;
this.timesPlayed = timesPlayed;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getArtist()
{
return artist;
}
public void setArtist(String artist)
{
this.artist = artist;
}
public int getYear()
{
return year;
}
public void setYear(int year)
{
this.year = year;
}
public int getCatalog()
{
return catalog;
}
public void setCatalog(int catalog)
{
this.catalog = catalog;
}
public double getDuration()
{
return duration;
}
public void setDuration(double duration)
{
this.duration = duration;
}
public int getTimesPlayed()
{
return timesPlayed;
}
public void setTimesPlayed(int timesPlayed)
{
this.timesPlayed = timesPlayed;
}
public String toString()
{
return String.format("%-20s%-15s%-10d%-10d%-10.2f%-12d", title, artist, year, catalog, duration, timesPlayed);
}
}
Playlist.java
import java.util.Date;
import java.util.ArrayList;
public class Playlist
{
private String name; //- Name of the playlist
private Date dateCreated; //- The date the playlist was created
private Date dateUpdated; //- The date it was last updated
private ArrayList<Song> songs;//- Collection of songs on the playlist
public Playlist()
{
this.name = "";
this.dateCreated = null;
this.dateUpdated = null;
}
public Playlist(String name)
{
this.name = name;
this.dateCreated = new Date();
this.dateUpdated = new Date();
}
public Playlist(String name, Date dateCreated, Date dateUpdated)
{
this.name = name;
this.dateCreated = dateCreated;
this.dateUpdated = dateUpdated;
}
public Playlist(String name, Date dateCreated, Date dateUpdated, ArrayList<Song> songs)
{
this.name = name;
this.dateCreated = dateCreated;
this.dateUpdated = dateUpdated;
this.songs = songs;
}
public String getName()
{
return name;
}
public Date getDateCreated()
{
return dateCreated;
}
public Date getDateUpdated()
{
return dateUpdated;
}
public ArrayList<Song> getSongs()
{
return songs;
}
//The ability to add song to playlist
public void addSong(Song song)
{
boolean found = false;
for(int i = 0; i < songs.size() && !found; i++)
{
if(songs.get(i).getCatalog() == song.getCatalog())
{
found = true;
}
}
if(!found)
{
songs.add(song);
this.dateUpdated = new Date();
}
}
//The ability to remove song from playlist
public void removeSong(int catalogNumber)
{
for(int i = 0; i < songs.size(); i++)
{
if(songs.get(i).getCatalog() == catalogNumber)
{
songs.remove(i);
this.dateUpdated = new Date();
break;
}
}
}
//The ability to display the contents of the playlist
public void printPlaylist()
{
System.out.printf("%-20s%-15s%-10s%-10s%-10s%-12s ", "Title", "Artist", "Year", "Catalog", "Duration", "TimesPlayed");
for(int i = 0; i < songs.size(); i++)
{
System.out.println(songs.get(i));
}
}
//The ability to sort the playlist by song title, artist, year, and number of times played
public void sortSongsInPlaylist()
{
for(int i = 0; i < songs.size() - 1; i++)
{
int minPos = i;
for (int j = i + 1; j < songs.size(); j++)
{
if(songs.get(j).getTitle().compareToIgnoreCase(songs.get(minPos).getTitle()) < 0)
{
minPos = j;
}
else if(songs.get(j).getTitle().compareToIgnoreCase(songs.get(minPos).getTitle()) == 0 && songs.get(j).getArtist().compareToIgnoreCase(songs.get(minPos).getArtist()) < 0)
{
minPos = j;
}
else if(songs.get(j).getTitle().compareToIgnoreCase(songs.get(minPos).getTitle()) == 0 && songs.get(j).getArtist().compareToIgnoreCase(songs.get(minPos).getArtist()) == 0 && songs.get(j).getYear() < songs.get(minPos).getYear())
{
minPos = j;
}
else if(songs.get(j).getTitle().compareToIgnoreCase(songs.get(minPos).getTitle()) == 0 && songs.get(j).getArtist().compareToIgnoreCase(songs.get(minPos).getArtist()) == 0 && songs.get(j).getYear() == songs.get(minPos).getYear() && songs.get(j).getTimesPlayed() < songs.get(minPos).getTimesPlayed())
{
minPos = j;
}
else if(songs.get(j).getTitle().compareToIgnoreCase(songs.get(minPos).getTitle()) == 0 && songs.get(j).getArtist().compareToIgnoreCase(songs.get(minPos).getArtist()) == 0 && songs.get(j).getYear() == songs.get(minPos).getYear() && songs.get(j).getTimesPlayed() == songs.get(minPos).getTimesPlayed() && songs.get(j).getDuration() < songs.get(minPos).getDuration())
{
minPos = j;
}
}
if (minPos != i)
{
Song temp1 = songs.get(i);
Song temp2 = songs.get(minPos);
songs.set(i, temp2);
songs.set(minPos, temp1);
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.