public class Aluno {
private int codigo;
private String nome;
private int anoNascimento;
private List<Float> notas = new ArrayList<Float>();
public Aluno(int codigo, String nome, int anoNascimento, ArrayList notas) {
this.codigo = codigo;
this.nome = nome;
this.anoNascimento = anoNascimento;
this.notas = notas;
}
public Aluno() {
}
public int getCodigo() {
return codigo;
}
@Override //por que???
public String toString() {
return "Alunos Cadastrados\n{" + "codigo= " + codigo + "nome= " + nome + "anoNascimento= " + anoNascimento + "notas= " + notas + '}';
}
public String getNome() {
return nome;
}
public int getAnoNascimento() {
return anoNascimento;
}
public List<Float> getNotas() {
return notas;
}
public void setCodigo(int codigo) {
this.codigo = codigo;
}
public void setNome(String nome) {
this.nome = nome;
}
public void setAnoNascimento(int anoNascimento) {
this.anoNascimento = anoNascimento;
}
public void setNotas(List<Float> notas) {
this.notas = notas;
}
public float calculaMedia(List<Float> notas){
float soma = 0;
for (Float k : notas){
soma=soma+k;
}
int n=notas.size();
float media=(soma/n);
return media;
}
public void mostrarTudo(int cod, String nome, List nota, float media){
DecimalFormat df = new DecimalFormat("#.##");
JOptionPane.showMessageDialog(null,"Codigo:"+cod+"Nome:"+nome+"Notas:"+nota+"Media"+df.format(media),"Objeto Aluno",
JOptionPane.PLAIN_MESSAGE);
}
[code]
List<Aluno> aluno = new ArrayList<Aluno>();
a.setCodigo(Integer.parseInt(tfCod.getText()));
a.setNome(tfNome.getText());
a.setAnoNascimento(Integer.parseInt(tfAno.getText()));
List<Float> notas = new ArrayList<Float>();
notas.add(Float.parseFloat(tfn1.getText()));
notas.add(Float.parseFloat(tfn2.getText()));
notas.add(Float.parseFloat(tfn3.getText()));
a.setNotas(notas);
float media = a.calculaMedia(notas);
aluno.add(a);
a.mostrarTudo(a.getCodigo(), a.getNome(), a.getNotas(), media);
limparCampos();
System.out.println(aluno);
}
Aluno a = new Aluno();
obrigado