I'm building a class that manage Vector<Studente>
and Vector<StudenteLaureato>
types, where StudenteLaureato
extends Studente
.
Before I added the method inserisciLaureati
which merges Vector<Studente>
and Vector<StudenteLaureato>
, everything was working fine.
Now, when I passstampaDati
a Vector<Studente>
that also contains StudenteLaureato
objects, it doesn't print their subclass specific attributes. Can't figure out how to edit the function to solve this.
public class GestioneRegistro {
private Vector<Studente> elenco = new Vector<Studente>();
Vector<Studente> letturaDati(String nomeFileDaLeggere) {
System.out.println("inizio la lettura");
String rowRead;
BufferedReader in;
in = new BufferedReader(new InputStreamReader(new FileInputStream(nomeFileDaLeggere)));
while((rowRead = in.readLine()) != null) {
StringTokenizer st = new StringTokenizer(rowRead);
StudenteLaureato s = new StudenteLaureato(st.nextToken(), st.nextToken(), st.nextToken());
elenco.add(s);
}
return elenco;
}
public void stampaDati(Vector<Studente> elencoloc){
List<Studente> arrlist = new ArrayList<Studente>();
Enumeration<Studente> e = elencoloc.elements();
arrlist = Collections.list(e);
System.out.println("Contenuto di elenco: "+arrlist);
}
public Vector<Studente> merger(Vector<Studente> Va, Vector<Studente> Vb) {
Vector<Studente> merge = new Vector<Studente>();
merge.addAll(Va);
merge.addAll(Vb);
return merge;
}
public void inserisciLaureati(Vector<Studente> elencoloc, String nomeFileDaLeggere) {
elencoloc = merger( elencoloc, letturaDati(nomeFileDaLeggere));
}
public static void main(String args[]) {
GestioneRegistro reg = new GestioneRegistro();
Vector<Studente> elencoloc = new Vector<Studente>();
elencoloc = reg.letturaDati(args[0]);
reg.stampaDati(elencoloc);
reg.inserisciLaureati(elencoloc, "C:\\Users\\Nixon\\Desktop\\archiviolau.txt");
}
}
where Studente is this:
public class Studente implements Serializable{
private static final long serialVersionUID = 1L;
String nome;
String cognome;
String matricola;
public Studente(String n, String c, String m){
nome = n;
cognome = c;
matricola = m;
}
public String toString() {
return nome+" "+cognome+" "+matricola;
}
}
and StudenteLaureato is this:
public class StudenteLaureato extends Studente {
private static final long serialVersionUID = 1L;
String indirizzo;
String dataLaurea;
public StudenteLaureato(String n, String c, String m, String i, String d) {
super(n, c, m);
indirizzo = i;
dataLaurea = d;
}
public StudenteLaureato(String n, String c, String m){
super(n, c, m);
}
public String toString() {
return nome+" "+cognome+" "+matricola+" "+indirizzo+" "+dataLaurea;
}
}