Implement a class called MusicExchangeCenter which represents the company websit
ID: 3727454 • Letter: I
Question
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).
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 clients 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 center’s 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.
File AutoSave C on 8 : 0 Document2 - Word Picture Tools Sign in - Ox Home Insert Design Layout References Mailings Review ViewHelp Format Tell me what you want to do e Share Calibri (Body) - 11 - A A Aa - - E-E-F- FEE ! 1 P Find - bct AaBbccc 3. Replace Paste * Format Painter B 1 U - albo x, x? A - ay - A - E === - - - 1 Normal 1 No Spac... Heading 1 Heading 2 Title Subtitle Subtle Em.. Emphasis - Clipboard Font Paragraph Styles Xcut De Copy Editing 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 private String private int title: Artist: duration public Song 01 this (*. *", 0, 0); public Song (stringt, string a, int n, int s) { owner null; title= t: artista: duration 60 8: public String getTitle() { return title; } public String getArtist() { return artisti ) public int getDuration 0 return duration: public int getMinutes) return duration / 60: public int getSeconds() { return duration 60: Page 6 of 788 words IN gp G - + 100Explanation / Answer
package 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) {
this.title = t;
this.artist = a;
this.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;
}
@Override
public String toString() {
return "\*" + title + " by " + artist + " " + (duration/60) + " | "+(duration%60) ;
}
}
package music;
import java.util.ArrayList;
public class User {
private String userName;
private boolean online;
//I think we need one more attribute, that is list of songs a user is having
private ArrayList<Song> songs;
public User()
{
this("");
songs=new ArrayList<>();
}
public User(String userName) {
this.userName = userName;
this.online=false;
songs=new ArrayList<>();
}
public String getUserName() {
return userName;
}
public boolean isOnline() {
return online;
}
//to make user online
public void setOnline(boolean online) {
this.online = online;
}
public ArrayList<Song> getSongs() {
return songs;
}
public void setSongs(ArrayList<Song> songs) {
this.songs = songs;
}
@Override
public String toString() {
String s=" "+userName+" |songs ( "+getSongs()+" ";
if(!online)
{
s+=" not ";
}
return s+" Online ";
}
}
package music;
import java.util.ArrayList;
public class MusicExchangeCente {
private ArrayList<User> users;
public MusicExchangeCente(ArrayList<User> users) {
this.users = users;
}
public MusicExchangeCente() {
this.users = new ArrayList<>();
}
// online users method
public ArrayList<User> onlineUsers() {
ArrayList<User> ArrayList<>();
for (User user : users) {
if (user.isOnline()) {
onlineUsers.add(user);
}
}
return onlineUsers;
}
// online availabelSongs method
/***
* For this method we need an extra attribute in users class named songs. The
* need is that , How can we get songs of a perticular user?
*
* @return
*/
public ArrayList<Song> allAvailableSongs() {
ArrayList<User>> ArrayList<Song> availabelSongs = new ArrayList<>();
for (User user : onlineUsers) {
// get the songs of the user if he/she is online
if (user.isOnline()) {
// get Songs list and add it to available songs
for (Song song : user.getSongs()) {
availabelSongs.add(song);
}
}
}
return availabelSongs;
}
@Override
public String toString() {
return "MusicExchangeCente ( " + onlineUsers().size() + " online , " + allAvailableSongs().size()
+ " songs available)";
}
public ArrayList<User> userWithName(String s) {
ArrayList<User> userWithName = new ArrayList<>();
for (User user : users) {
if (user.getUserName().equalsIgnoreCase(s)) {
userWithName.add(user);
}
}
return userWithName;
}
public void registerUser(User x) {
// get users
// first user
if (users.isEmpty()) {
users.add(x);
} else {
if (users.contains(x)) {
// do nothing because user already exists
} else {
// add to list of users
users.add(x);
}
}
}
public ArrayList<Song> availableSongsByArtist(String artist) {
ArrayList<Song> availableSongsByArtist = new ArrayList<>();
for (Song song : allAvailableSongs()) {
if (song.getArtist().equalsIgnoreCase(artist)) {
availableSongsByArtist.add(song);
}
}
return availableSongsByArtist;
}
}
package music;
import java.util.ArrayList;
public class MusicExchangeCente {
private ArrayList<User> users;
public MusicExchangeCente(ArrayList<User> users) {
this.users = users;
}
public MusicExchangeCente() {
this.users = new ArrayList<>();
}
// online users method
public ArrayList<User> onlineUsers() {
ArrayList<User> ArrayList<>();
for (User user : users) {
if (user.isOnline()) {
onlineUsers.add(user);
}
}
return onlineUsers;
}
// online availabelSongs method
/***
* For this method we need an extra attribute in users class named songs. The
* need is that , How can we get songs of a perticular user?
*
* @return
*/
public ArrayList<Song> allAvailableSongs() {
ArrayList<User>> ArrayList<Song> availabelSongs = new ArrayList<>();
for (User user : onlineUsers) {
// get the songs of the user if he/she is online
if (user.isOnline()) {
// get Songs list and add it to available songs
for (Song song : user.getSongs()) {
availabelSongs.add(song);
}
}
}
return availabelSongs;
}
@Override
public String toString() {
return "MusicExchangeCente ( " + onlineUsers().size() + " online , " + allAvailableSongs().size()
+ " songs available)";
}
public ArrayList<User> userWithName(String s) {
ArrayList<User> userWithName = new ArrayList<>();
for (User user : users) {
if (user.getUserName().equalsIgnoreCase(s)) {
userWithName.add(user);
}
}
return userWithName;
}
public void registerUser(User x) {
// get users
// first user
if (users.isEmpty()) {
users.add(x);
} else {
if (users.contains(x)) {
// do nothing because user already exists
} else {
// add to list of users
users.add(x);
}
}
}
public ArrayList<Song> availableSongsByArtist(String artist) {
ArrayList<Song> availableSongsByArtist = new ArrayList<>();
for (Song song : allAvailableSongs()) {
if (song.getArtist().equalsIgnoreCase(artist)) {
availableSongsByArtist.add(song);
}
}
return availableSongsByArtist;
}
}
output
[ user2 |songs ( [*cccc by artist3 80 | 0, *dddd by artist4 50 | 0] Online
, user3 |songs ( [] Online
]
All availabale songs
[*cccc by artist3 80 | 0, *dddd by artist4 50 | 0]
All availabale songs by artist
[]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.