Untitled
unknown
plain_text
a year ago
10 kB
12
Indexable
/**
* The SongRecord class represents information about a particular audio file.
*/
public class SongRecord {
private String title;
private String artist;
private int minutes;
private int seconds;
/**
* Default constructor for the SongRecord class.
* Initializes the member variables with empty values.
*/
public SongRecord() {
title = "";
artist = "";
minutes = 0;
seconds = 0;
}
/**
* Constructs a SongRecord object with the specified information.
* @param title The title of the song.
* @param artist The artist of the song.
* @param minutes The length of the song in minutes.
* @param seconds The length of the song in seconds.
*/
public SongRecord(String title, String artist, int minutes, int seconds) {
this.title = title;
this.artist = artist;
this.minutes = minutes;
this.seconds = seconds;
}
/**
* Returns the title of the song.
*/
public String getTitle() {
return title;
}
/**
* Sets the title of the song.
*/
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
/**
* Sets the artist of the song.
*/
public void setArtist(String artist) {
this.artist = artist;
}
/**
* Returns the length of the song in minutes.
*/
public int getMinutes() {
return minutes;
}
/**
* Sets the length of the song in minutes.
*/
public void setMinutes(int minutes) {
if (minutes < 0) {
throw new IllegalArgumentException("Minutes cannot be negative.");
}
this.minutes = minutes;
}
/**
* Returns the length of the song in seconds.
*/
public int getSeconds() {
return seconds;
}
/**
* Sets the length of the song in seconds.
*/
public void setSeconds(int seconds) {
if (seconds < 0 || seconds > 59) {
throw new IllegalArgumentException("Seconds must be between 0 and 59 (inclusive).");
}
this.seconds = seconds;
}
/**
* Returns a string representation of the SongRecord object.
*/
public String toString() {
return "Title: " + title + ", Artist: " + artist + ", Duration: " + minutes + ":" + seconds;
}
}
Playlist.java:
/**
* The Playlist class represents a playlist of audio files.
*/
public class Playlist {
private final int MAX_SONGS = 50;
private SongRecord[] songRecords;
private int songCount;
/**
* Constructor for the Playlist class.
* Constructs an instance of the Playlist class with no SongRecord objects in it.
*/
public Playlist() {
songRecords = new SongRecord[MAX_SONGS];
songCount = 0;
}
/**
* Returns the number of SongRecords currently in this Playlist.
*/
public int size() {
return songCount;
}
/**
* Adds a new SongRecord to this Playlist at the specified position.
*/
public void addSong(SongRecord song, int position) {
if (position < 1 || position > songCount + 1) {
throw new IllegalArgumentException("Invalid position.");
}
if (songCount >= MAX_SONGS) {
throw new IllegalArgumentException("The Playlist is full.");
}
// Move existing songs to make space for the new song
for (int i = songCount; i >= position; i--) {
songRecords[i] = songRecords[i - 1];
}
// Add the new song to the specified position
songRecords[position - 1] = song;
songCount++;
}
/**
* Removes the SongRecord at the specified position from this Playlist.
* @param position The position in the playlist where the song will be removed from.
* @throws IllegalArgumentException If position is not within the valid range.
*/
public void removeSong(int position) {
if (position < 1 || position > songCount) {
throw new IllegalArgumentException("Invalid position.");
}
// Move songs after the removed song forward
for (int i = position - 1; i < songCount - 1; i++) {
songRecords[i] = songRecords[i + 1];
}
// Set the last position to null
songRecords[songCount - 1] = null;
songCount--;
}
/**
* Returns the SongRecord at the given position in this Playlist.
* @param position The position of the SongRecord to retrieve.
* @return The SongRecord at the specified position in this Playlist.
* @throws IllegalArgumentException If position is not within the valid range.
*/
public SongRecord getSong(int position) {
if (position < 1 || position > songCount) {
throw new IllegalArgumentException("Invalid position.");
}
return songRecords[position - 1];
}
/**
* Prints a neatly formatted table of each SongRecord in the Playlist on its own line with its position number.
*/
public void printAllSongs() {
for (int i = 0; i < songCount; i++) {
System.out.println((i + 1) + ". " + songRecords[i]);
}
}
/**
* Generates a new Playlist containing all SongRecords in the original Playlist performed by the specified artist.
* @param originalList The original Playlist.
* @param artist The name of the artist.
* @return A new Playlist object containing all SongRecords in the original Playlist performed by the specified artist.
*/
public static Playlist getSongsByArtist(Playlist originalList, String artist) {
if (originalList == null || artist == null) {
return null;
}
Playlist artistPlaylist = new Playlist();
for (int i = 0; i < originalList.songCount; i++) {
if (originalList.songRecords[i].getArtist().equals(artist)) {
artistPlaylist.addSong(originalList.songRecords[i], artistPlaylist.size() + 1);
}
}
return artistPlaylist;
}
}
PlaylistOperations.java:
import java.util.Scanner;
public class PlaylistOperations {
public static void main(String[] args) {
Playlist playlist = new Playlist();
Scanner scanner = new Scanner(System.in);
boolean quit = false;
while (!quit) {
System.out.println("A) Add Song\nB) Print Songs by Artist\nG) Get Song\nR) Remove Song\nP) Print All Songs\nS) Size\nQ) Quit");
System.out.print("Select a menu option: ");
String option = scanner.nextLine();
switch (option.toUpperCase()) {
case "A":
System.out.print("Enter the song title: ");
String title = scanner.nextLine();
System.out.print("Enter the song artist: ");
String artist = scanner.nextLine();
System.out.print("Enter the song length (minutes): ");
int minutes = Integer.parseInt(scanner.nextLine());
System.out.print("Enter the song length (seconds): ");
int seconds = Integer.parseInt(scanner.nextLine());
System.out.print("Enter the position: ");
int position = Integer.parseInt(scanner.nextLine());
try {
SongRecord song = new SongRecord(title, artist, minutes, seconds);
playlist.addSong(song, position);
System.out.println("Song Added: " + song.getTitle() + " By " + song.getArtist());
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
break;
case "B":
System.out.print("Enter the artist name: ");
String artistName = scanner.nextLine();
Playlist artistPlaylist = Playlist.getSongsByArtist(playlist, artistName);
if (artistPlaylist == null) {
System.out.println("Invalid input.");
} else {
artistPlaylist.printAllSongs();
}
break;
case "G":
System.out.print("Enter the position: ");
int getPosition = Integer.parseInt(scanner.nextLine());
try {
SongRecord getSong = playlist.getSong(getPosition);
System.out.println("Song at position " + getPosition + ": " + getSong.getTitle() + " By " + getSong.getArtist());
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
break;
case "R":
System.out.print("Enter the position: ");
int removePosition = Integer.parseInt(scanner.nextLine());
try {
playlist.removeSong(removePosition);
System.out.println("Song removed from position " + removePosition);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
break;
case "P":
playlist.printAllSongs();
break;
case "S":
System.out.println("Size of the playlist: " + playlist.size());
break;
case "Q":
quit = true;
break;
default:
System.out.println("Invalid option. Please try again.");
break;
}
}
scanner.close();
}
}
FullPlaylistException.java:
/**
* The FullPlaylistException class represents an exception that is thrown when there is no more room inside the Playlist to store a new SongRecord object.
*/
public class FullPlaylistException extends Exception {
public FullPlaylistException(String message) {
super(message);
}
}Editor is loading...
Leave a Comment