Last active
March 10, 2024 23:17
-
-
Save MelissaBruno/12788057e9c3a981b9e1 to your computer and use it in GitHub Desktop.
Create a playlist of songs in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InvalidLengthException extends Exception | |
{ | |
private String lengthSent; | |
//Creates a blank InvalidLengthException with no arguements. | |
private InvalidLengthException() | |
{ | |
} | |
//Creates an InvalidLengthException that takes in the invalid String. | |
public InvalidLengthException(String inString) | |
{ | |
this.lengthSent = inString; | |
} | |
//Returns the inString of an InvalidLengthException. | |
public String getLengthSet() | |
{ | |
return this.lengthSent; | |
} | |
//Returns InvalidLengthException as a String. | |
public String toString() | |
{ | |
String returnString; | |
returnString = this.lengthSent + " is an invalid length!\n"; | |
return returnString; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InvalidRatingException extends Exception | |
{ | |
private int numberSent; | |
//Creates a blank InvalidRatingException with no arguments. | |
private InvalidRatingException() | |
{ | |
} | |
//Creates an InvalidRatingException with the invalid number. | |
public InvalidRatingException(int inNumberSent) | |
{ | |
this.numberSent = inNumberSent; | |
} | |
//Returns the numberSent of an InvalidRatingException. | |
public int getNumberSent() | |
{ | |
return this.numberSent; | |
} | |
//Returns InvalidRatingException as a String. | |
public String toString() | |
{ | |
String returnString; | |
returnString = this.numberSent + " is an invalid rating! Please choose a rating between 1 and 5.\n"; | |
return returnString; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main | |
{ | |
public static void main( String[] arg) | |
{ | |
Menu menu = new Menu(); | |
menu.loadPlaylist(); | |
menu.runMenu(); | |
} | |
} //End class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.io.*; | |
public class Playlist implements Serializable | |
{ | |
private ArrayList<Song> playlist; | |
//Creates a Playlist object that contains an ArrayList for Songs. | |
public Playlist() | |
{ | |
playlist = new ArrayList<Song>(); | |
} | |
//Adds a Song to a Playlist. | |
public void addSong(Song song) | |
{ | |
playlist.add(song); | |
} | |
//Returns the size of a Playlist. | |
public int getPlaylistSize() | |
{ | |
return playlist.size(); | |
} | |
//Returns the artist of the Song at position (index) of a Playlist. | |
public String getArtist(int index) | |
{ | |
return playlist.get(index).getArtist(); | |
} | |
//Returns the title of the Song at position (index) of a Playlist. | |
public String getTitle(int index) | |
{ | |
return playlist.get(index).getTitle(); | |
} | |
//Returns the length of the Song at position (index) of a Playlist. | |
public String getLength(int index) | |
{ | |
return playlist.get(index).getLength(); | |
} | |
//Returns the genre of the Song at position (index) of a Playlist. | |
public String getGenre(int index) | |
{ | |
return playlist.get(index).getGenre(); | |
} | |
//Returns the rating of the Song at position (index) of a Playlist. | |
public int getRating(int index) | |
{ | |
return playlist.get(index).getRating(); | |
} | |
//Sets the artist of the Song at position (index) to artist. | |
public void updateArtist(int index, String artist) | |
{ | |
playlist.get(--index).setArtist(artist); | |
} | |
//Sets the title of the Song at position (index) to the title. | |
public void updateTitle(int index, String title) | |
{ | |
playlist.get(--index).setTitle(title); | |
} | |
//Sets the length of the Song at position (index) to length; throws InvalidLengthException. | |
public void updateLength(int index, String length) throws InvalidLengthException | |
{ | |
if(length.matches("(\\d.*):(\\d.*)")) | |
{ | |
playlist.get(--index).setLength(length); | |
} | |
else | |
{ | |
throw new InvalidLengthException(length); | |
} | |
} | |
//Sets the genre of the Song at position (index) to genre. | |
public void updateGenre(int index, String genre) | |
{ | |
playlist.get(--index).setGenre(genre); | |
} | |
//Sets the rating of the Song at position (index) to rating. | |
public void updateRating(int index, int rating) throws InvalidRatingException | |
{ | |
if(rating > 0 && rating < 6) | |
{ | |
playlist.get(--index).setRating(rating); | |
} | |
else | |
{ | |
throw new InvalidRatingException(rating); | |
} | |
} | |
//Removes the song located at the specified index from the Playlist. | |
public void removeSong(int index) | |
{ | |
playlist.remove(index); | |
} | |
//Clears the Playlist of all Songs by invoking clear() on a Playlist. | |
public void deletePlaylist() | |
{ | |
if(playlist.size() == 0) | |
{ | |
System.out.print("Playlist is empty!\n"); | |
} | |
else | |
{ | |
playlist.clear(); | |
System.out.print("Playlist has been deleted.\n"); | |
} | |
} | |
} //End class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class Song implements Serializable | |
{ | |
private String artist; | |
private String title; | |
private String length; | |
private String genre; | |
private int rating; | |
//Creates a playlist object with an artist, title, length, genre, and rating. | |
public Song(String artist, String title, String length, String genre, int rating) | |
{ | |
this.artist = artist; | |
this.title = title; | |
this.length = length; | |
this.genre = genre; | |
this.rating = rating; | |
} | |
//Sets Song's artist to argument artist. | |
public void setArtist(String artist) | |
{ | |
this.artist = artist; | |
} | |
//Return the value of artist in a Song. | |
public String getArtist() | |
{ | |
return artist; | |
} | |
//Sets Song's title to argument title. | |
public void setTitle(String title) | |
{ | |
this.title = title; | |
} | |
//Returns the value of title in a Song. | |
public String getTitle() | |
{ | |
return this.title; | |
} | |
//Sets song's length to length argument; creates InvalidLengthException. | |
/* This is not exactly how I wanted to do this, instead | |
* this checks for validity by seeing if the argument String | |
* contains both a colon and only numbers.*/ | |
public void setLength(String length) throws InvalidLengthException | |
{ | |
if(length.matches("(\\d.*):(\\d.*)")) | |
{ | |
this.length = length; | |
} | |
else | |
{ | |
throw new InvalidLengthException(length); | |
} | |
} | |
//Returns the value of length in a Song. | |
public String getLength() | |
{ | |
return this.length; | |
} | |
//Sets Song's genre to argument genre. | |
public void setGenre(String genre) | |
{ | |
this.genre = genre; | |
} | |
//Returns value of genre in a Song. | |
public String getGenre() | |
{ | |
return this.genre; | |
} | |
//Sets Song's rating to argument rating; creates InvalidRatingException. | |
public void setRating(int rating) throws InvalidRatingException | |
{ | |
if(rating > 0 || rating < 6) | |
{ | |
this.rating = rating; } | |
else | |
{ | |
throw new InvalidRatingException(rating); | |
} | |
} | |
//Returns rating value in a Song. | |
public int getRating() | |
{ | |
return this.rating; | |
} | |
} //End class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this work?? Plz tell me