Reputation: 105
just wondering how I would go about creating a playlist of song objects (song1, song2, song3, song4) where each object has it's own values (name, artist, filesize, duration). I am not able to use arrays/lists for this program. Just look at the getSong()
and addSongToPlaylist()
methods below, I included the other methods just to show the structure of the song objects.
This is my SongDatabase class:
public class SongDatabase {
Scanner console = new Scanner(System.in);
private Song song1=null, song2=null, song3=null, song4=null;
public void addNewSong() {
if (song1 == null) {
song1 = getFromUser();
}
else if (song2 == null) {
song2 = getFromUser();
}
else if (song3 == null) {
song3 = getFromUser();
}
else if (song4 == null) {
song4 = getFromUser();
}
else {
System.out.println("The database is currently full. Please delete a song before adding a new one.");
System.out.println("");
}
}
private Song getFromUser() {
Song song = new Song();
System.out.println("Name of song:");
song.setName(console.next());
System.out.println("Artist:");
song.setArtist(console.next());
System.out.println("File size (MB):");
song.setFileSize(console.nextInt());
System.out.println("Duration (seconds):");
song.setDuration(console.nextInt());
System.out.println("Song successfully added."); /
System.out.println("");
return song;
}
public Song getSong(int songNumber) {
if (songNumber == 1){
return song1;
}
else if (songNumber == 2){
return song2;
}
else if (songNumber == 3){
return song3;
}
else if (songNumber == 4){
return song4;
}
else {
return song1;
}
}
And then my menu class:
public class Menu {
Scanner console = new Scanner(System.in);
private SongDatabase database = new SongDatabase();
private Playlist playlist = new Playlist();
private int songCount=0;
private void addSongToPlaylist() {
if (songCount <=3) {
System.out.println("Please enter the number of the song you'd like to be added to the playlist.");
System.out.println("");
int songNumber;
songNumber = console.nextInt();
switch (songNumber) {
case 1:
playlist.setSong(database.getSong(1));
break;
case 2:
playlist.setSong(database.getSong(2));
break;
case 3:
playlist.setSong(database.getSong(3));
break;
case 4:
playlist.setSong(database.getSong(4));
break;
default:
System.out.println("Please enter a valid song number.");
break;
}
songCount++;
}
So far, the addSongToPlaylist()
method should get the song1 object from the getSong()
method right? But I am unsure how to add these song objects into a playlist in the Playlist
class. I know I need a method like public void setSong(parameters?)
but I am unsure how to structure it. Any ideas? Help would be greatly appreciated, thanks!
Upvotes: 0
Views: 1858
Reputation: 598
If you cannot use List
or Array
and if your PlayList
is limited to 3 Songs
then you can try like this
public class Playlist {
private Song firstSong;
private Song secondSong;
private Song thirdSong;
public void setSong(Song song) {
if (song != null) {
if (firstSong == null) {
firstSong = song;
} else if (secondSong == null) {
secondSong = song;
} else if (thirdSong == null) {
thirdSong = song;
}
}
}
}
Upvotes: 1
Reputation: 2504
you can use any datastructure to hold the list of songs, like array, list, set, map etc.
below is an example code using list.
class PlayList {
List<Song> songList = new ArrayList<Song>();
void addSongInList(Song song){
songList.add(song)l
}
}
Upvotes: 0