Dunque,Il problema di fondo sai qual'è, non ci si improvvisa programmatori....(
scusa non volevo fare il figo ma praticamente ho risposto qui solo per scrivere sta frase):
TestLibrettoEsami: ancora non hai chiuso lo scanner...ti odio. Occhio che gli oggetti han dei metodi...usali nè! Adesso lo scanner si alimenta da file ".txt". Basta che metti un file di testo nel package dove hai la classe e ci incolli le righe che usi di esempio (io ho usato le tue e va....e ovviamente aggiusta a seconda del nome file il getResourceAsStream()).
codice:
public class TestLibrettoEsami{ private static InputStream in = null;
private static Scanner scan = null;
private static LibrettoEsami l = null;
public static void main(String[] args) {
scan = new Scanner(new TestLibrettoEsami().getClass().getResourceAsStream("esamiSostenuti.txt"));
l=new LibrettoEsami();
while(scan.hasNextLine()){
String[] d = scan.nextLine().split("#");
if(d.length == 3){
l.addEsame(new Esame(d[0],
isNumeric(d[1])?Integer.parseInt(d[1]):0,
isNumeric(d[2])?Integer.parseInt(d[2]):0));
}
else if(d.length == 2){
l.addEsame(new Esame(d[0],
isNumeric(d[1])?Integer.parseInt(d[1]):0));
}
else{
System.out.println("Attenzione riga non valida!");
}
System.out.println("==================");
}
scan.close();
}
public static boolean isNumeric(String str)
{
return str.matches("-?\\d+(\\.\\d+)?");
}
}
LibrettoEsami: Ok, messe un paio di sysout giusto per verifica...
codice:
public class LibrettoEsami{
private ArrayList <Esame> esami;
final int numesami=40;
private int cfu;
private double media;
public LibrettoEsami(){
esami=new ArrayList <Esame>();
cfu=0;
media=0.0;
}
public boolean addEsame(Esame e){
if(esami.size()<numesami&&esami.contains(e)){
System.out.println("esame già registrato");
return false;
}else
esami.add(e);
System.out.println("Esame aggiunto: " + e.getNome() + "(cfu: " + e.getCFU() + " Voto:" +e.getVoto() + ")");
return true;
}
public int totCFU(){
for(Esame e:esami)
this.cfu+=e.getCFU();
return cfu;
}
//media=sommaPesataVoti / sommaCrediti;
public double mediaVoti(){
double sommavoti=0.0;
double sommaCrediti=0.0;
for(Esame e:esami){
sommavoti+=(e.getVoto()*e.getCFU());
sommaCrediti+=e.getCFU();
}
media=sommavoti/sommaCrediti;
return media;
}
public String toString(){
String s=" ";
for(Esame e:esami){
if(e.getVoto()!=0)
s=e.getNome()+" "+e.getCFU()+" CFU "+e.getVoto();
else
s=e.getNome()+" "+e.getCFU()+" CFU : esame non sostenuto";
}
return s;
}
}
Esame: Qui c'è il trick!!!! ...vediamo se lo comprendi da sola.
codice:
public class Esame{
@Override
public boolean equals(Object obj) {
return (this.nome.equals(((Esame) obj).nome)
&& this.cfu == ((Esame) obj).cfu
//&& this.voto ==((Esame) obj).voto se anche il voto è determinante decommentare...
);
}
private String nome;
private int cfu;
private int voto;
public Esame(String nome, int cfu, int voto){
this.nome=nome;
this.cfu=cfu;
this.voto=voto;
}
public Esame(String nome, int cfu){
this(nome,cfu,0);
}
public void setVoto(int voto){
this.voto=voto;
}
public int getVoto(){
return voto;
}
public int getCFU(){
return cfu;
}
public String getNome(){
return nome;
}
public boolean isSostenuto(){
if(voto!=0 && voto<=30){
return true;
}else
return false;
}
public String toString(){
String s="il nome dell'esame è "+nome+" i cfu sono : "+cfu;
if(isSostenuto()){
s=s+" il voto è : "+voto;
}
return s;
}
}