PDA

View Full Version : [java]classe testlibretto


el922
27-02-2015, 11:03
Ciao è da poco che sto imparando java e mi trovo in difficoltà sull'implementazione di questa classe testlibretto;in pratica devo costruire un libretto universitario:

class Esame{
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 boolean equals(Object obj){
if(nome.equals(obj)){
return true;
}else
return false;
}
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;
}
}

la classe librettoesami:

import java.util.*;
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);
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;
}
}


Fin qui penso tutto bene...ma il mio problema è implementare il tutto da main;in particolare:
Supponiamo che le linee sullo standard input siano

programmazione#12#26
basi di dati#12#25
inglese#3
architettura degli elaboratori I#6#24
programmazione#12#28
END

Il programma deve stampare:

registro PROGRAMMAZIONE 12 CFU 26
registro BASI DI DATI 12 CFU 25
INGLESE 3 CFU : esame non sostenuto
registro ARCHITETTURA DEGLI ELABORATORI I 6 CFU 24
PROGRAMMAZIONE 12 CFU: esame gia` registrato

CFU: 30 - Media: 25.2
Il mio codice è:

import java.util.*;
import java.io.*;
public class TestLibrettoEsami{
public static void main(String[] args) {
LibrettoEsami l =new LibrettoEsami();
Esame e=new Esame("prog",12,24);
Scanner sc = new Scanner(System.in).useDelimiter("#");
//- se l'esame viene aggiunto:NOME_ESAME CFU voto
while(sc.hasNext()){
String materia=sc.next();
if(l.addEsame(e)){
System.out.println(materia.toString());
}

}
}
}


Il punto è come faccio a riconoscere da terminale le diverse stringhe?Il mio programma scritto così riconosce qualsiasi cosa scrivo in input.Come posso risolvere il problema in modo abbastanza semplice? :cry:

monte.cristo
27-02-2015, 17:42
Se questo deve rappresentare una situazione reale, nella classe Esame manca, oltre ad un voto numerico compreso tra 18 e 30, un flag per l'eventuale lode