├── Library.java ├── Playlist.java ├── Song.java ├── User.java └── UserCollection.java /Library.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | import java.util.List; 4 | /* 5 | * This class must keep track of the library of songs in your Spotify program. 6 | * This comes from the file in songFiles. That filename is passed in as a 7 | * command-line argument. 8 | */ 9 | public class Library { 10 | 11 | private List songLibrary; 12 | 13 | //Constructs a new instance of this class 14 | public Library() { 15 | songLibrary = new ArrayList(); 16 | } 17 | 18 | /* 19 | * Returns the Song associated with the String title passed in if it exists 20 | * in the library, or null if the song does not exist in the library. 21 | */ 22 | public Song getSong(String title) { 23 | for (int i = 0; i < songLibrary.size(); i++) { 24 | if (title.equals(songLibrary.get(i).getTitle())) { 25 | return songLibrary.get(i); 26 | } 27 | } 28 | return null; 29 | } 30 | 31 | //Returns a list of all the songs in the library. 32 | public List getAllSongs() { 33 | return songLibrary; 34 | } 35 | 36 | //Adds the passed in song to library. 37 | public void addSong(Song song) { 38 | songLibrary.add(song); 39 | } 40 | 41 | //Removes the passed in song from the library if it exists in the library. 42 | public void removeSong(Song song) { 43 | songLibrary.remove(song); 44 | } 45 | 46 | /* 47 | * Return a string representation of this library. The format is to have 48 | * the string representation of each song on its own line. So you want to 49 | * add the string representation of a song, then a newline for each song. 50 | */ 51 | public String toString() { 52 | List sort = new ArrayList(); 53 | for (int i = 0; i < songLibrary.size(); i++) { 54 | sort.add(songLibrary.get(i).getTitle()); 55 | } 56 | Collections.sort(sort); 57 | String result = ""; 58 | for (int i = 0; i < sort.size(); i++) { 59 | for (int j = 0; j < songLibrary.size(); j++) { 60 | if (sort.get(i).equals(songLibrary.get(j).getTitle())) { 61 | result += songLibrary.get(j).toString() + "\n"; 62 | } 63 | } 64 | } 65 | return result; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Playlist.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | import java.util.List; 4 | 5 | //This class will keep track of a playlist. 6 | public class Playlist { 7 | 8 | private String playlistName; 9 | private List playlistContents; 10 | 11 | 12 | //Constructs a new instance of the Playlist class with the specified name. 13 | public Playlist(String name) { 14 | playlistName = name; 15 | playlistContents = new ArrayList(); 16 | } 17 | 18 | /* 19 | * Constrcts a new instance of the Playlist class with the specified name 20 | * and songs. 21 | */ 22 | public Playlist(String name, List contents) { 23 | playlistName = name; 24 | playlistContents = contents; 25 | } 26 | 27 | //Returns the name of the playlist. 28 | public String getName() { 29 | return playlistName; 30 | } 31 | 32 | //Adds the specified song to the playlist. 33 | public void addSong(Song song) { 34 | playlistContents.add(song); 35 | } 36 | 37 | /* 38 | * Plays the playlist. In order to "play" a song, you just need to print 39 | * each song out. Remember that a song already knows how to describe itself 40 | * with its toString()method. 41 | */ 42 | public void play() { 43 | for (int i = 0; i < playlistContents.size(); i++) { 44 | playlistContents.get(i).play(); 45 | } 46 | } 47 | 48 | /* 49 | * Shuffle the songs in the playlist so they play in a different random 50 | * order next time. 51 | */ 52 | public void shuffle() { 53 | Collections.shuffle(playlistContents); 54 | } 55 | 56 | //Remove the passed in song from the playlist if it exist in the playlist. 57 | public void removeSong(Song song) { 58 | playlistContents.remove(song); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Song.java: -------------------------------------------------------------------------------- 1 | /*This class collects methods/data for a song.*/ 2 | public class Song { 3 | 4 | private String songTitle; 5 | private String songArtist; 6 | private int count; 7 | 8 | /* 9 | * Construct a new instance of the Song class with the specified song 10 | * title and artist 11 | */ 12 | public Song(String title, String artist) { 13 | songTitle = title; 14 | songArtist = artist; 15 | count = 0; 16 | } 17 | 18 | //Return the title of the song 19 | public String getTitle() { 20 | return songTitle; 21 | } 22 | 23 | //Return the artist of the song 24 | public String getArtist() { 25 | return songArtist; 26 | } 27 | 28 | //Return the number of times this song has been played 29 | public int getTimesPlayed() { 30 | return count; 31 | } 32 | 33 | //"Play" this song. To play a song you just print out a description 34 | public void play() { 35 | System.out.println(toString()); 36 | count++; 37 | } 38 | 39 | /* 40 | * Return a string description of this song. It should be of the 41 | * formal: title by artist, timesPlayed play(s). So if you have 42 | * the song God's Plan by Drake and it has been played 27 times, 43 | * it should return "God's Plan by Drake, 27 play(s)" 44 | */ 45 | public String toString() { 46 | return songTitle + " by " + songArtist + ", " + count + " play(s)"; 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /User.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | //This class bundles methods/ data for a User object. 5 | public class User { 6 | 7 | private String userName; 8 | private String userPassword; 9 | private List userPlaylist; 10 | 11 | /* 12 | * Constructs a new instance of the User class with the 13 | * specified name and password. 14 | */ 15 | public User(String name, String password) { 16 | userName = name; 17 | userPassword = password; 18 | userPlaylist = new ArrayList(); 19 | } 20 | 21 | //Returns the name of the user. 22 | public String getName() { 23 | return userName; 24 | } 25 | 26 | /* 27 | * Returns true if the password is valid for this user. 28 | * False otherwise. 29 | */ 30 | public boolean attemptLogin(String password) { 31 | if (password.equals(userPassword)) { 32 | return true; 33 | } 34 | return false; 35 | } 36 | 37 | //Adds the specified playlist to the user's playlists. 38 | public void addPlaylist(Playlist newPlaylist) { 39 | userPlaylist.add(newPlaylist); 40 | } 41 | 42 | //Returns a list of the playlists for this user. 43 | public List getPlaylists() { 44 | return userPlaylist; 45 | } 46 | 47 | /* 48 | * Selects the playlist with the specified name if the 49 | * user has a playlist by that name and plays the newly 50 | * selected playlist. 51 | */ 52 | public void selectPlaylist(String name) { 53 | for (int i = 0; i < userPlaylist.size(); i++) { 54 | if (userPlaylist.get(i).getName().equals(name)) { 55 | userPlaylist.get(i).play(); 56 | break; 57 | } 58 | } 59 | } 60 | 61 | /* 62 | * Returns a string description of the user. The format 63 | * is name, numPlaylists playlist. So if I had a user 64 | * cjenkins who had 12 playlists this method would return 65 | * "cjenkins, 12 playlists". 66 | */ 67 | public String toString() { 68 | return userName + ", " + userPlaylist.size() + " playlists"; 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /UserCollection.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | //This class tracks all of the users in Spotify. 5 | public class UserCollection { 6 | 7 | private List userCollection; 8 | 9 | //Constructs a new instance of the UserCollection class. 10 | public UserCollection() { 11 | userCollection = new ArrayList(); 12 | } 13 | 14 | //Returns true if a user with the specified username exists. 15 | public boolean userExists(String username) { 16 | for (int i = 0; i < userCollection.size(); i++) { 17 | if (userCollection.get(i).getName().equals(username)) { 18 | return true; 19 | } 20 | } 21 | return false; 22 | } 23 | 24 | /* 25 | * Returns the User associated with the login credentials if it 26 | * was a valid login or returns null if the login was invalid. 27 | */ 28 | public User login(String username, String password) { 29 | for (int i = 0; i < userCollection.size(); i++) { 30 | if (userCollection.get(i).getName().equals(username)) { 31 | if (userCollection.get(i).attemptLogin(password)) { 32 | return userCollection.get(i); 33 | } 34 | } 35 | } 36 | return null; 37 | } 38 | 39 | //Adds this user to the collection of all users. 40 | public void addUser(User add) { 41 | userCollection.add(add); 42 | } 43 | 44 | /* 45 | * Returns a string description of all the user. For example 46 | * if there were two user (tconkin, cjenkins) both with 0 47 | * playlists, toString() should return"{ cjenkins: cjenkins, 48 | * 0 playlists, tconklin: tconklin, 0 playlists,}" 49 | */ 50 | public String toString() { 51 | String result = "{ "; 52 | for (int i = 0; i < userCollection.size(); i++) { 53 | result += userCollection.get(i).getName() + ": " 54 | + userCollection.get(i).toString() + ", "; 55 | } 56 | result += "}"; 57 | return result; 58 | } 59 | 60 | } 61 | --------------------------------------------------------------------------------