Hello i've these 3 classes: Here i put the name of a student and the exams he gave
package traccia50719;
import java.util.*;
public class Lab {
public static void main(String[] args) {
Studente studente = inserimento();
System.out.println("fine inserimento\n");
studente.print();
System.out.println("\nfine programma");
}
private static Studente inserimento() {
Studente s = null;
Esame esame=null;
System.out.println("\nmatricola:");
Scanner mat = new Scanner(System.in);
int matricola= mat.nextInt();
System.out.println("\ncognome:");
Scanner cog = new Scanner(System.in);
String cognome= cog.next();
System.out.println("\nNome:");
Scanner nom = new Scanner(System.in);
String nome= nom.next();
s= new Studente(matricola, cognome, nome);
do{
System.out.println("\ncodice esame:");
Scanner cod = new Scanner(System.in);
int codicesame= cod.nextInt();
if(codicesame==0){
break;
}
System.out.println("\nNome esame:");
Scanner nomes = new Scanner(System.in);
String nomesame= nomes.next();
System.out.println("\nvoto esame:");
Scanner vot = new Scanner(System.in);
int votoesame= vot.nextInt();
esame = new Esame(codicesame,nomesame,votoesame);
s.addEsame(esame);
}while(true);
return s;
}
}
In this class i've the student with the constructor but when i try to print exams with iterator i have only one exam printed. Why?
package traccia50719;
import java.util.*;
public class Studente {
private int matricola;
private String cognome;
private String nome;
private Set<Esame> esami = new TreeSet<Esame>();
public Studente(int matricola, String cognome, String nome){
this.matricola=matricola;
this.cognome=cognome;
this.nome=nome;
}
public void addEsame(Esame e){
this.esami.add(e);
}
public void print(){
System.out.println("\nmatricola:" + this.matricola);
System.out.println("\ncognome:" + this.cognome);
System.out.println("\nnome:" + this.nome);
Iterator<Esame> i = this.esami.iterator();
while(i.hasNext()){
Esame e = (Esame) i.next();
e.stampaesame();
}
}
}
This is the 3 class Exam and i've stampaEsame() that print name and the result of the exam
package traccia50719;
public class Esame implements Comparable{
private int codice;
private String nome;
private int voto;
public Esame(int codice, String nome, int voto){
this.codice=codice;
this.nome=nome;
this.voto=voto;
}
public void stampaesame(){
System.out.println("\n nome esame:" +this.nome);
System.out.println("\n voto esame:" +this.voto);
}
public boolean equals(Object o) {
Esame esa = (Esame) o;
if(this.codice==esa.codice){
return true;
}else return false;
}
public int compareTo(Object arg0) {
// TODO Auto-generated method stub
return 0;
}
}