Question
Here are simple Song and User classes that represent a song that is available at the Music Exchange Center and a user of the Music
Here are simple Song and User classes that represent a song that is available at the Music Exchange Center and a user of the Music Exchange Center that logs in to download music:
public class Song { private String title; private String artist; private int duration; public Song() { this("", "", 0, 0); } public Song(String t, String a, int m, int s) { title = t; artist = a; duration = m * 60 + s; } public String getTitle() { return title; } public String getArtist() { return artist; } public int getDuration() { return duration; } public int getMinutes() { return duration / 60; } public int getSeconds() { return duration % 60; } public String toString() { return("\"" + title + "\" by " + artist + " " + (duration / 60) + ":" + (duration%60)); } }
Do the following in the User class:
Add a songList attribute which is an ArrayList of Song objects. This list will contain all the songs that this user has on his/her hard drive to be made available to the Music Exchange Center community. Adjust the 2nd constructor to ensure that this attribute is initialized properly. Write a get method for the attribute as well.
Adjust the toString() method so that the XXX is replaced by the number of songs available in his/her song list.
Create a method called addSong(Song s) which adds a given song to the users song list.
Create a method called totalSongTime() that returns an integer indicating the total duration (i.e., amount of time) (in seconds) that all of the users songs would require if played.
Implement a class called MusicExchangeCenter which represents the company website that users log in and out of in order to download music.
The class should have this attribute:
users - an ArrayList of all registered Users (which may be either logged on or not logged on).
Create the following methods:
A zero-parameter constructor that sets attributes properly.
An onlineUsers() method that returns an ArrayList of all Users that are currently online. Note that this method creates and returns a new ArrayList each time it is called.
An allAvailableSongs() method that returns a new ArrayList of all Songs currently available for download (i.e., all songs that are available from all logged-on users). Note that this method creates and returns a new ArrayList each time it is called.
A toString() method that returns a string representation of the music center showing the number of users currently online as well as the number of songs currently available. (e.g., "Music Exchange Center (3 users on line, 15 songs available)").
A userWithName(String s) method that finds and returns the user object with the given name if it is in the list of users. If not there, null should be returned.
A registerUser(User x) method that adds a given User to the music centers list of users, provided that there are no other users with the same userName. If there are other users with the same userName, then this method does nothing. Use the userWithName() method above.
An availableSongsByArtist(String artist) method that returns a new ArrayList of all Songs currently available for download by the specified artist. Note that this method creates and returns a new ArrayList each time it is called. Go back to the User class and add these methods:
A register(MusicExchangeCenter m) that makes use of the registerUser() method that you just wrote to register the user into the given MusicExchangeCenter m. (Note that this should be a one-line method).
A logon(MusicExchangeCenter m) that simulates a user going online with the given music center. (Assume that users can log onto at most one music center at a time). Make use of the userWithName() method you wrote earlier. Only allow the user to log on if he/she has already registered.
A logoff(MusicExchangeCenter m) that simulates a user going offline from the given music center. Make use of the userWithName() method you wrote earlier.
Now we will test our code. Since we will be testing our classes with the same users, you must add the following static methods to the User class (Each method creates and returns a unique user with some songs in their song list):
public class User { private String userName; private boolean online; public User() { this(""); } public User(String u) { userName = u; online = false; } public String getUserName() { return userName; } public boolean isOnline() { return online; } public String toString() { String s = "" + userName + ": XXX songs ("; if (!online) s += "not "; return s + "online)"; } // 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 4x4 Driver", "Ms. Lonely", 3, 7)); countryCandy.addSong(new Song("This Song is for Johnny", "Lone Wolf", 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; } }
Now test your code with the following test program:
public class MusicExchangeTestProgram { 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, except SleepingSam discoStew.register(mec); ronnieRocker.register(mec); countryCandy.register(mec); peterPunk.register(mec); // Display the state of things before anyone logs on System.out.println("Status: " + mec); System.out.println("On-Line Users: " + mec.onlineUsers()); System.out.println("Available Songs: " + mec.allAvailableSongs() + " "); // Attempt to log on two registered users and one unregistered user discoStew.logon(mec); sleepingSam.logon(mec); // Should not work ronnieRocker.logon(mec); System.out.println("Status: " + mec); System.out.println("On-Line Users: " + mec.onlineUsers()); System.out.println("Available Songs: " + mec.allAvailableSongs() + " "); // Log on two more users countryCandy.logon(mec); peterPunk.logon(mec); System.out.println("Status: " + mec); System.out.println("On-Line Users: " + mec.onlineUsers()); System.out.println("Available Songs: " + mec.allAvailableSongs()); System.out.println("Available Songs By Jaw: " + mec.availableSongsByArtist("Jaw") + " "); // Log off three users (one is not even logged in) countryCandy.logoff(mec); discoStew.logoff(mec); sleepingSam.logoff(mec); System.out.println("Status: " + mec); System.out.println("On-Line Users: " + mec.onlineUsers()); System.out.println("Available Songs: " + mec.allAvailableSongs()); System.out.println("Available Songs By Jaw: " + mec.availableSongsByArtist("Jaw") + " "); // Log off the last two users peterPunk.logoff(mec); ronnieRocker.logoff(mec); System.out.println("Status: " + mec); System.out.println("On-Line Users: " + mec.onlineUsers()); System.out.println("Available Songs: " + mec.allAvailableSongs()); System.out.println("Available Songs By Jaw: " + mec.availableSongsByArtist("Jaw") + " "); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started