You could use it like this (Create the Get/Set and the same are already ready for various functionalities)
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 the Codification:
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 so:
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
}
}
'Cause I’d do it like this?
Because, the List
is dynamic so that you can add/remove/change songs from a particular album, one album may have more songs than the other and limit the list would be a mistake ...
Your class will only have this Builder? if yes you do not need to give a new Music[30] and then by Length you take the amount of music
– user6026
In Java arrays are different from Arraylists. In your code you are using arrays and not Arraylists. Internally Arraylists use arrays to store their objects, but Arraylists are dynamic, that is, increase and decrease in size while arrays have fixed size.
– Piovezan
Yes, this class only has this Constructor. I thank you for the clarification Piovezan and the response of Fccdias! I’m kind of new in Java yet.
– user3000135