2016-03-12 17 views
0

我有麻煩讓什麼使播放列表數組在用戶循環中工作。我可以在播放列表類中檢索歌曲列表數組,但User類內的循環不會接受播放列表類中的歌曲列表。任何幫助!?如何通過在用戶類中輸入播放列表數組中的位置來獲取歌曲標題?

請求:一種方法,返回位於播放列表中位置pos的歌曲的標題。輸出是空字符串,該位置不是有效的位置。

Playlist類:

public class Playlist { 
//Instance Variables 
private int numOfSongs; 
private Songs[] songList; 

// Constructors 
public Playlist(int maxNumofSongs){ 
    this.numOfSongs = 0; 
    this.songList = new Songs[maxNumofSongs]; 
} 
//Getters 
    public Songs[] getSongList(){ 
     return this.songList; 
    } 
//Methods 
public void addSong(String title, String filePath, String artist){ 
    Songs p = new Songs(title, filePath, artist); 
    addSong(p); 
} 
public void addSong(Songs p){ 
    songList[this.numOfSongs] = p; 
    this.numOfSongs++; 
} 
public Songs getSong(int pos){ 
    if (pos <= this.songList.length) 
     return this.songList[pos]; 
    else 
     return null; 
} 
public int getSongByTitle(String title){ 
    int pos = -1; 

    for (int i = 0; i < this.numOfSongs; i++) 
     if (this.songList[i].getTitle() == title) 
      pos = i; 

    return pos; 
} 
public String toString(){ 
    String playlistDesc = ""; 
    playlistDesc += "Number of Songs added in Playlist: "+ numOfSongs; 
    return playlistDesc; 
} 

}

用戶類別:

public class User { 
//Instance Variables 
private String name; 
private String email; 
private Playlist favoriteSongs; 

//Constructors 
public User(String name, String email, Playlist favoriteSongs){ 
    this.name = name; 
    this.email = email; 
    Songs[] songs = this.favoriteSongs.getSongList(); 
} 
public User(String name, String email){ 
    this.name = name; 
    this.email = email; 
} 
//Setters 
public void setPlayList(Playlist list){ 
    this.favoriteSongs = list; 
} 
//Get song title by inputting the position in the playlist array 
public String getSongTitle(int pos){ 
    if (pos == this.favoriteSongs.getSongList()) 
     return Playlist.length[pos]; 
} 
//Add new song to the playlist 
public int addSong(String title, String filePath, String artist){ 
    for(int i=0; i<this.Playlist.length; i++) { 
     if ((this.songList[i].getTitle().getArtist() == addSong(i))) // or what ever you want to compare 
      return 0; 
      } 
      // if you do not found any thing 
     return -1; 
     } 
} 
// Counts how many songs with the same artist 
public int artistSongCount(String artist){ 
    int count = 0; 
    for (int i=0; i < Playlist.numOfSongs.length; i++) 
     if (this.favoriteSongs[i].getArtist() == artist) 
      count++; 

    return count; 
} 
} 
//Print out details of user 
public String toString(){ 
    String userOutput = ""; 
    userOutput += "Name: "+ name; 
    userOutput += "Email: "+ email; 
    return userOutput; 
} 

}

+0

爲了讓你開始,你引用了你的用戶類中的類「播放列表」,而不是變量(favoriteSongs)。還要看看播放列表中的變量numOfSongs,它是一個私有變量,並且您試圖在User中調用它。 – user1875195

回答

1

一開始,我假定這是一個 「學習鍛鍊」因此我會給你提示,而不是給你提供正確的代碼

我認爲這是給你帶來困難的方法。

//Get song title by inputting the position in the playlist array 
public String getSongTitle(int pos){ 
    if (pos == this.favoriteSongs.getSongList()) 
     return Playlist.length[pos]; 
} 

提示:

  1. 這不會編譯。如果if休息失敗會返回什麼?

  2. pos == this.favoriteSongs.getSongList()getSongList()返回的對象的類型是什麼?你可以使用==來比較一個整數的對象嗎? (提示:號碼)

  3. 關於此Playlist.length[pos]。這實際上意味着什麼? Playlist是一個類,因此Playlist.length將是一個靜態字段的名稱。 Playlist課上有這樣的領域嗎?

  4. 這是大提示。您已經有了一種方法,可以在播放列表中的某個位置返回歌曲。 (尋找它!)。我可以看到(代碼getSongByTitle)如何獲得歌曲的標題。所以你所要做的就是做這兩件事,並處理例外情況(非有效位置)。

相關問題