Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java (1) The Songl User Classes Here Center and a user of the Music Exchange Cen

ID: 3733743 • Letter: J

Question

java

(1) The Songl User Classes Here Center and a user of the Music Exchange Center that logs in to download music: are simple Song and User classes that represent a song that is available at the Music Excha nge public class Song f private String private String private int title; artist; duration; public Song) this("","", 0, 0); public Song (String t, String a, int m, int s) I title-t artista; duration = m * 60 + s; public String getTitle public String getArtist public int getDuration return title; return artist: return duration; public int getMinutes) return duration 60 public int getSeconds t return duration % 60; public String toString return"+title "" by " + artist" "+ (duration / 60) + ":" + (duration% 60));

Explanation / Answer

package music;

import java.util.ArrayList;

import jdk.nashorn.internal.runtime.options.LoggingOption;
import sun.security.jgss.spi.MechanismFactory;

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;
   }
  
   //Add Song to list
   public void addSong(Song s)
   {
       songs.add(s);
   }
  
   public int totalSongTime()
   {
       int totalTime=0;
       for (Song song : songs) {
           totalTime+=song.getSeconds();
       }
       return totalTime;
   }
  
   public void register(MusicExchangeCente m)
   {
       m.registerUser(this);
   }
  
   public void logon(MusicExchangeCente m)
   {
       //get the list of users
       ArrayList<User> users=m.getUsers();
       for (User user : users) {
           if(m.userWithName(user.getUserName()).equals(this))
           m.onlineUsers().add(this);
       }
   }
  
   public void logoff(MusicExchangeCente m)
   {
       //get the list of users
       ArrayList<User> users=m.getUsers();
       for (User user : users) {
           if(m.userWithName(user.getUserName()).equals(this))
           m.onlineUsers().remove(this);
       }
   }
  
  
   public ArrayList<String> requestCompleteSongList(MusicExchangeCente m)
   {
       ArrayList<String> allSongs=new ArrayList<>();
       ArrayList<Song> songs=m.allAvailableSongs();
       for (Song song : songs) {
           allSongs.add(song.toString());
       }
       return allSongs;
   }
  
   public ArrayList<String> requestSongListByArtist(MusicExchangeCente m,String artist)
   {
       ArrayList<String> allSongsByArtist=new ArrayList<>();
       ArrayList<Song> songs=m.availableSongsByArtist(artist);
       for (Song song : songs) {
           allSongsByArtist.add(song.toString());
       }
       return allSongsByArtist;
   }
  
  
  
   @Override
   public String toString() {
       String s=" "+userName+" |songs (   "+getSongs()+"   ";
       if(!online)
       {
           s+=" not ";
       }
       return s+" Online ";
   }
  
}

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 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;
   }

   public Song getSong(String title,String ownerName)
   {
       for (User user : users) {
           if(user.getUserName().equalsIgnoreCase(ownerName))
           {
               ArrayList<Song> songs=user.getSongs();
               for (Song song : songs) {
                   if(song.getTitle().equalsIgnoreCase(title)) {
                       return song;
                   }
               }
           }
       }
       return null;
   }
   @Override
   public String toString() {
       return "MusicExchangeCente ( " + onlineUsers().size() + "   online , " + allAvailableSongs().size()
               + " songs available)";
   }

   public User userWithName(String s) {
       for (User user : users) {
           if (user.getUserName().equalsIgnoreCase(s)) {
               return user;
           }
       }
       return null;
   }

   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;
   }

   public ArrayList<User> getUsers() {
       return users;
   }

   public void setUsers(ArrayList<User> users) {
       this.users = users;
   }

}