You could use it like this (Create Get / Set and they're ready for various features)
Class:
public class Album {
private int id;
private String Nome_album;
Date Lancamento;
private Musica[] tracklist;
static private int ultimo = 1;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNome_album() {
return Nome_album;
}
public void setNome_album(String Nome_album) {
this.Nome_album = Nome_album;
}
public Musica[] getTracklist() {
return tracklist;
}
public void setTracklist(Musica[] tracklist) {
this.tracklist = tracklist;
}
public static int getUltimo() {
return ultimo;
}
public static void setUltimo(int ultimo) {
Album.ultimo = ultimo;
}
public Album(int id, String nome_album, Date lancamento, Musica[] tracklist) {
id=ultimo;
ultimo++;
Nome_album = nome_album;
Lancamento = lancamento;
this.tracklist = tracklist;
}
}
In Encoding:
Musica[] musica = new Musica[10];
Album album = new Album(1, null, null, musica);
int totalMusica = album.getTracklist().length; // quantidade de música
I would do this:
Classes
public class Musica {
private int id;
private String nome;
private int ano;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public int getAno() {
return ano;
}
public void setAno(int ano) {
this.ano = ano;
}
}
package classes;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Album {
private int id;
private String nomeAlbum;
private Date lancamento;
private List<Musica> tracklist;
static private int ultimo = 1;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNomeAlbum() {
return nomeAlbum;
}
public void setNomeAlbum(String nomeAlbum) {
this.nomeAlbum = nomeAlbum;
}
public Date getLancamento() {
return lancamento;
}
public void setLancamento(Date lancamento) {
this.lancamento = lancamento;
}
public List getTracklist() {
return tracklist;
}
public void setTracklist(List tracklist) {
this.tracklist = tracklist;
}
public static int getUltimo() {
return ultimo;
}
public static void setUltimo(int ultimo) {
Album.ultimo = ultimo;
}
//Contructor
public Album(){
this.tracklist = new ArrayList<>();
}
public Album(int id, String nomeAlbum, Date lancamento, List tracklist) {
this.id = ultimo;
this.nomeAlbum = nomeAlbum;
this.lancamento = lancamento;
this.tracklist = tracklist;
}
}
Testing
import classes.Album;
import classes.Musica;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class JavaApplication1 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Musica music1 = new Musica();
Musica music2 = new Musica();
List<Musica> musicas = new ArrayList<>();
musicas.add(music1);
musicas.add(music2);
Album album = new Album(1, "Nome Album 1", new Date() , musicas);
album.getTracklist().size(); // quantidade que estão na lista
}
}
Why would I do this?
Because List
is dynamic so you can add / remove / change songs from a given album, one album being able to have more songs than the other and limiting the list would be an error ...