(Solved) : Go Back Song Class Add Attribute Called Owner User Object Representing User Happens Copy S Q27757633 . . .
Go back to the Song class and add to it an attribute calledowner which will be a User object representing the user who happensto own this copy of this song. Note that a Song object may only beowned by one User and that many users can have copies of the samesong. Set it to null initially. In the User class, adjust theaddSong() method so that it sets the owner properly.
In the User class, add a method calledrequestCompleteSonglist(MusicExchangeCenter m). This method shouldgather the list of all available songs from all users that areonline at the given music exchange center (i.e., the union of allof their local song lists), and then return an ArrayList formattedas follows:

Notice that the songs are numbered and that the title, artist,time and owner are all lined up nicely. You should use theString.format() method as described in the notes. Recall that %-30sin the format string will allow you to display a left-justified30-character string. Also, %2d and %02d will allow you to displaynumbers so that they take 2 places, the 0 indicating that a leadingzero character is desired.
In the User class, add a method calledrequestSonglistByArtist(MusicExchangeCenter m, String artist). Thismethod should gather the list of all available songs by the givenartist from all users that are online at the given music exchangecenter (i.e., the union of all of their local song lists), and thenreturn an ArrayList formatted similar to that shown above.
In the MusicExchangeCenter class, add a method calledgetSong(String title, String ownerName) which returns the Songobject with the given title owned by the user with the givenownerName, provided that the user is currently online and that thesong exists. Return null otherwise. (Hint: you will need to searchthrough the center’s users to find User with the matching ownerNameand then search through that user’s songs to find the Song with thegiven title. It may be a good idea to write an extra helper methodin the User class called songWithTitle() that you can make useof).
In the User class, add a downloadSong(MusicExchangeCenter m,String title, String ownerName) method that simulates thedownloading of one of the songs in the catalog. It should use thegetSong() method that you just wrote, and add the downloaded songto the user’s songList (if not null).
Test your code now with the following program:
import java.util.ArrayList;public class MusicExchangeTestProgram2 { public static void main(String args[]) { ArrayList<String> catalog; // Create a new music exchange center MusicExchangeCenter mec = new MusicExchangeCenter(); // Create some users and give them some songs User discoStew = User.DiscoStew(); User sleepingSam = User.SleepingSam(); User ronnieRocker = User.RonnieRocker(); User countryCandy = User.CountryCandy(); User peterPunk = User.PeterPunk(); // Register the users discoStew.register(mec); ronnieRocker.register(mec); sleepingSam.register(mec); countryCandy.register(mec); peterPunk.register(mec); // Log on all users discoStew.logon(mec); sleepingSam.logon(mec); ronnieRocker.logon(mec); countryCandy.logon(mec); peterPunk.logon(mec); System.out.println(“Status: ” + mec); // Simulate a user requesting a list of songs catalog = discoStew.requestCompleteSonglist(mec); System.out.println(“Complete Song List: “); for (String s: catalog) System.out.println(” ” + s); // Simulate a user downloading 3 songs from the list System.out.println(“nDisco Stew before downloading: ” + discoStew); discoStew.downloadSong(mec, “Bite My Arms Off”, “Peter Punk”); discoStew.downloadSong(mec, “Meadows”, “Sleeping Sam”); discoStew.downloadSong(mec, “If I Had a Hammer”, “Country Candy”); discoStew.downloadSong(mec, “Sandy Toes”, “Country Candy”); ronnieRocker.logoff(mec); // log off Ronnie, next download should fail discoStew.downloadSong(mec, “Only You Can Rock Me”, “Ronnie Rocker”); System.out.println(“Disco Stew after downloading: ” + discoStew); ronnieRocker.logon(mec); // log on Ronnie, next download should work discoStew.downloadSong(mec, “Only You Can Rock Me”, “Ronnie Rocker”); System.out.println(“Disco Stew after downloading Ronnie?s: ” + discoStew + “n”); // Simulate a user requesting a list of songs by a specific artist catalog = discoStew.requestSonglistByArtist(mec, “Jaw”); System.out.println(“Song?s by Jaw: “); for (String s: catalog) System.out.println(” ” + s); }}
Now….in order to make all of this legal, we must have a way tocompensate the musical artists for their hard work and wonderfulmusic. The compensation depends on many factors. An artist shouldreceive 25 cents each time one of their songs is downloaded. Hence,for each artist, we will keep track of how much money in royaltiesthey have. Add the following attributes to the MusicExchangeCenterclass:
royalties – a HashMap with the artist’s name as the keys and thevalues are floats representing the total amount of royalties forthat artist so far. It should only contain artists who have hadsongs downloaded.
downloadedSongs – an ArrayList containing all of the songs thathave been downloaded. This list will, in general, contain duplicateSong objects.
Write a method in the MusicExchangeCenter class calleddisplayRoyalties() that displays the royalties for all artists whohave had at least one of their songs downloaded. It should displaya two-line header and then one line per artist showing the royaltyamount as well as the artist name as follows:

In the getSong() method in the MusicExchangeCenter, adjust thecode so that if the song is found (i.e., able to be downloaded),then it is added to the downloadedSongs list. Write the get methodfor the downloadedSongs attribute.
Write a method in the MusicExchangeCenter class calleduniqueDownloads() that returns (i.e., not displays) a TreeSet ofall downloaded Song objects such that the set is sortedalphabetically by song title. There should be no duplicates songsin this set.
Write a method in the MusicExchangeCenter class calledsongsByPopularity() that returns (i.e., not displays) an ArrayListof Pair objects where the key of the pair is an Integer and thevalue is the Song object. The integer key should represent thenumber of times that that song was downloaded. The list returnedshould be sorted in decreasing order according to the number oftimes the song was downloaded. To sort a list of Pair objects, youcan use the following code:
Collections.sort(popular, new Comparator<Pair<Integer,Song>>() {
public int compare(Pair<Integer, Song> p1,Pair<Integer, Song> p2) {
//Put code here
}
});
Just insert the missing code so that it returns an appropriateinteger indicating whether pair p1 comes before or after pair p2 inthe sort order.
Test your code now with the following program:
import javafx.util.Pair;public class MusicExchangeTestProgram3 { public static void main(String args[]) { // Create a new music exchange center MusicExchangeCenter mec = new MusicExchangeCenter(); // Create some users and give them some songs User discoStew = User.DiscoStew(); User sleepingSam = User.SleepingSam(); User ronnieRocker = User.RonnieRocker(); User countryCandy = User.CountryCandy(); User peterPunk = User.PeterPunk(); // Register the users discoStew.register(mec); ronnieRocker.register(mec); sleepingSam.register(mec); countryCandy.register(mec); peterPunk.register(mec); // Log on all users discoStew.logon(mec); sleepingSam.logon(mec); ronnieRocker.logon(mec); countryCandy.logon(mec); peterPunk.logon(mec); System.out.println(“Status: ” + mec); // Simulate users downloading various songs discoStew.downloadSong(mec, “Bite My Arms Off”, “Peter Punk”); discoStew.downloadSong(mec, “Meadows”, “Sleeping Sam”); discoStew.downloadSong(mec, “If I Had a Hammer”, “Country Candy”); discoStew.downloadSong(mec, “Is that My Toenail ?”, “Peter Punk”); sleepingSam.downloadSong(mec, “Anvil Headache”, “Peter Punk”); sleepingSam.downloadSong(mec, “Is that My Toenail ?”, “Disco Stew”); sleepingSam.downloadSong(mec, “If I Had a Hammer”, “Country Candy”); countryCandy.downloadSong(mec, “Anvil Headache”, “Peter Punk”); countryCandy.downloadSong(mec, “Meadows”, “Sleeping Sam”); countryCandy.downloadSong(mec, “If I Had a Hammer”, “Peter Punk”); countryCandy.downloadSong(mec, “Only You Can Rock Me”, “Ronnie Rocker”); countryCandy.downloadSong(mec, “Is that My Toenail ?”, “Disco Stew”); peterPunk.downloadSong(mec, “Is that My Toenail ?”, “Country Candy”); peterPunk.downloadSong(mec, “Rock is Cool”, “Ronnie Rocker”); peterPunk.downloadSong(mec, “What?”, “Ronnie Rocker”); peterPunk.downloadSong(mec, “Meadows”, “Sleeping Sam”); // Display the downloaded songs System.out.println(“nHere are the downloaded songs: “); for (Song s: mec.getDownloadedSongs()) System.out.println(s); // Display the downloaded songs alphabetically System.out.println(“nHere are the unique downloaded songs alphabetically: “); for (Song s: mec.uniqueDownloads()) System.out.println(s); // Display the downloaded songs in order of popularity System.out.println(“nHere are the downloaded songs by populariry: “); for (Pair<Integer,Song> p: mec.songsByPopularity()) System.out.println(“(” + p.getKey() + ” downloads) ” + p.getValue()); // Display the royalties System.out.println(“nHere are the royalties:n”); mec.displayRoyalties(); }}
// User.java
import java.util.ArrayList;
public class User {
private String userName;
private boolean online;
// array list of songs
ArrayList<Song> songList;
public User() {
this(“”);
}
public User(String u) {
userName = u;
online = false;
// initializing songs list
songList = new ArrayList<Song>();
}
// getter method for songs list
public ArrayList<Song> getSongList() {
return songList;
}
public String getUserName() {
return userName;
}
public boolean isOnline() {
return online;
}
public String toString() {
// will show the number of songs also
String s = “” + userName + “: ” + songList.size() + ” songs (“;
if (!online)
s += “not “;
return s + “online)”;
}
/**
* method to add a song to the list
*/
public void addSong(Song song) {
songList.add(song);
}
/**
* method to find the total play time of all songs in the list
*/
public int totalSongTime() {
int total = 0;
for (Song s : songList) {
// adding the duration to the total
total += s.getDuration();
}
return total;
}
/**
* method to register the current user in a MusicExchangeCenter
*/
public void register(MusicExchangeCenter m) {
m.registerUser(this);
}
/**
* method to login the current user into a MusicExchangeCenter, onlyif
* he/she is a member
*/
public void logon(MusicExchangeCenter m) {
if (m.userWithName(userName) != null) {
online = true;
}
}
/**
* method to logoff the current user into a MusicExchangeCenter,only if
* he/she is a member
*/
public void logoff(MusicExchangeCenter m) {
if (m.userWithName(userName) != null) {
online = false;
}
}
// Various Users for test purposes
public static User DiscoStew() {
User discoStew = new User(“Disco Stew”);
discoStew.addSong(new Song(“Hey Jude”, “The Beatles”, 4, 35));
discoStew.addSong(new Song(“Barbie Girl”, “Aqua”, 3, 54));
discoStew.addSong(new Song(“Only You Can Rock Me”, “UFO”, 4,59));
discoStew.addSong(new Song(“Paper Soup Cats”, “Jaw”, 4, 18));
return discoStew;
}
public static User SleepingSam() {
User sleepingSam = new User(“Sleeping Sam”);
sleepingSam.addSong(new Song(“Meadows”, “Sleepfest”, 7, 15));
sleepingSam.addSong(new Song(“Calm is Good”, “Waterfall”, 6,22));
return sleepingSam;
}
public static User RonnieRocker() {
User ronnieRocker = new User(“Ronnie Rocker”);
ronnieRocker.addSong(new Song(“Rock is Cool”, “Yeah”, 4, 17));
ronnieRocker.addSong(new Song(“My Girl is Mean to Me”,
“Can’t Stand Up”, 3, 29));
ronnieRocker.addSong(new Song(“Only You Can Rock Me”, “UFO”, 4,52));
ronnieRocker.addSong(new Song(“We’re Not Gonna Take It”,
“Twisted Sister”, 3, 9));
return ronnieRocker;
}
public static User CountryCandy() {
User countryCandy = new User(“Country Candy”);
countryCandy.addSong(new Song(“If I Had a Hammer”, “Long Road”, 4,15));
countryCandy.addSong(new Song(“My Man is a 4×4 Driver”, “Ms.Lonely”,
3, 7));
countryCandy.addSong(new Song(“This Song is for Johnny”, “LoneWolf”,
4, 22));
return countryCandy;
}
public static User PeterPunk() {
User peterPunk = new User(“Peter Punk”);
peterPunk.addSong(new Song(“Bite My Arms Off”, “Jaw”, 4, 12));
peterPunk
.addSong(new Song(“Where’s My Sweater”, “The Knitters”, 3,41));
peterPunk.addSong(new Song(“Is that My Toenail ?”, “Clip”, 4,47));
peterPunk.addSong(new Song(“Anvil Headache”, “Clip”, 4, 34));
peterPunk.addSong(new Song(“My Hair is on Fire”, “Jaw”, 3,55));
return peterPunk;
}
}
// MusicExchangeCenter.java
import java.util.ArrayList;
public class MusicExchangeCenter {
// array list of all available users
private ArrayList<User> users;
public MusicExchangeCenter() {
// initializing users list
users = new ArrayList<User>();
}
/**
* returns all online users
*/
public ArrayList<User> onlineUsers() {
ArrayList<User> online = new ArrayList<User>();
for (User u : users) {
if (u.isOnline()) {
online.add(u);
}
}
return online;
}
/**
* returns all available songs from all online users
*/
public ArrayList<Song> allAvailableSongs() {
ArrayList<Song> availableSongs = newArrayList<Song>();
for (User u : onlineUsers()) {
availableSongs.addAll(u.getSongList());
}
return availableSongs;
}
@Override
public String toString() {
// returning a string in proper format
return “Music Exchange Center (” + onlineUsers().size()
+ ” users online, ” + allAvailableSongs().size()
+ ” songs available)”;
}
/**
* returns the User with given name if found
*
* @param s
* – user name
* @return – User object if found, else null
*/
public User userWithName(String s) {
for (User u : users) {
if (u.getUserName().equalsIgnoreCase(s)) {
return u;
}
}
return null;
}
/**
* method to register a user, if not already exist
*/
public void registerUser(User x) {
if (userWithName(x.getUserName()) == null) {
users.add(x);
}
}
/**
* returns all available songs by a specific artist , from allonline users
*/
public ArrayList<Song> availableSongsByArtist(String artist){
ArrayList<Song> songsByArtist = newArrayList<Song>();
for (Song s : allAvailableSongs()) {
if (s.getArtist().equalsIgnoreCase(artist)) {
songsByArtist.add(s);
}
}
return songsByArtist;
}
}
TITLE ARTIST TIME OWNER 1. Hey Jude 2. Barbie Girl . Only You Can Rock Me 4. Paper Soup Cats 5. Rock is Cool 6. My Girl is Mean to Me 7. Only You Can Rock Me 8. We’re Not Gonna Take It The Beatles Aqua UFO Jaw Yeah Can’t Stand Up 3:29 Ronnie Rocker UFO Twisted Sister 3:09 Ronnie Rocker 4:35 Disco Stew 3:54 Disco Stew 4:59 Disco Stew 4:18 DiscoStew 4:17 Ronnie Rocker 4:52 Ronnie Rocker etc. Show transcribed image text
Expert Answer
Answer to Go Back Song Class Add Attribute Called Owner User Object Representing User Happens Copy S Q27757633 . . .
OR