Opzione 3 + gestione dati non ancora creati
This commit is contained in:
@@ -6,6 +6,7 @@ package gradeanalyzer;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
@@ -30,6 +31,7 @@ public class GradeAnalyzer {
|
||||
static final String ERRORE_GENERICO = "Opzione non valida.";
|
||||
static ArrayList<Valutazione> valutazioni = valutazioniDaFile();
|
||||
static boolean isSaved = true;
|
||||
static boolean isEmpty = true;
|
||||
|
||||
public static void main(String[] args) {
|
||||
int scelta = -1;
|
||||
@@ -51,20 +53,38 @@ public class GradeAnalyzer {
|
||||
break;
|
||||
case 1:
|
||||
isSaved = false;
|
||||
isEmpty = false;
|
||||
inserisciDati();
|
||||
System.out.println("Studente aggiunto correttamente.");
|
||||
pausa();
|
||||
break;
|
||||
case 2:
|
||||
if (!isSaved) {
|
||||
System.out.println("Attenzione: ci sono dati non salvati.");
|
||||
salva();
|
||||
if (isEmpty) {
|
||||
System.out.println(
|
||||
"Errore: inserire almeno la valutazione di uno studente prima di proseguire.");
|
||||
} else {
|
||||
if (!isSaved) {
|
||||
System.out.println("Attenzione: ci sono dati non salvati.");
|
||||
salvaValutazioni();
|
||||
}
|
||||
trovaMigliorePeggiore();
|
||||
}
|
||||
trovaMigliorePeggiore();
|
||||
pausa();
|
||||
break;
|
||||
case 3:
|
||||
|
||||
if (isEmpty) {
|
||||
System.out.println(
|
||||
"Errore: inserire almeno la valutazione di uno studente prima di proseguire.");
|
||||
} else {
|
||||
if (!isSaved) {
|
||||
System.out.println("Attenzione: ci sono dati non salvati.");
|
||||
System.out.println("Verranno salvati automaticamente.");
|
||||
salva(valutazioni, PATH_VOTI);
|
||||
valutazioni = valutazioniDaFile();
|
||||
isSaved = true;
|
||||
}
|
||||
promossi();
|
||||
}
|
||||
pausa();
|
||||
break;
|
||||
default:
|
||||
@@ -124,7 +144,7 @@ public class GradeAnalyzer {
|
||||
valutazioni.add(new Valutazione(nome, cognome, voto));
|
||||
}
|
||||
|
||||
salva();
|
||||
salvaValutazioni();
|
||||
}
|
||||
|
||||
static double voto() {
|
||||
@@ -152,19 +172,38 @@ public class GradeAnalyzer {
|
||||
|
||||
static ArrayList<Valutazione> valutazioniDaFile() {
|
||||
ArrayList<Valutazione> valutazioni = new ArrayList<>();
|
||||
try (BufferedReader bf = new BufferedReader(new FileReader(PATH_VOTI))) {
|
||||
String riga = bf.readLine();
|
||||
String[] valoriValutazione;
|
||||
|
||||
while (riga != null) {
|
||||
valoriValutazione = riga.split(";");
|
||||
valutazioni.add(new Valutazione(valoriValutazione[0], valoriValutazione[1],
|
||||
Double.parseDouble(valoriValutazione[2])));
|
||||
riga = bf.readLine();
|
||||
File voti = new File(PATH_VOTI);
|
||||
|
||||
if (!voti.exists()) {
|
||||
if (!voti.getParentFile().exists()) {
|
||||
voti.getParentFile().mkdir();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("Errore nella lettura del file: " + e.getMessage());
|
||||
pausa();
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(PATH_VOTI))) {
|
||||
bw.write("");
|
||||
System.out.println("Nuovo file creato.");
|
||||
System.out.println("Aggiungere le valutazioni prima di proseguire.");
|
||||
} catch (IOException e) {
|
||||
System.out.println("Errore nella scrittura del file: " + e.getMessage());
|
||||
pausa();
|
||||
}
|
||||
} else {
|
||||
try (BufferedReader bf = new BufferedReader(new FileReader(PATH_VOTI))) {
|
||||
String riga = bf.readLine();
|
||||
String[] valoriValutazione;
|
||||
|
||||
while (riga != null) {
|
||||
isEmpty = false;
|
||||
valoriValutazione = riga.split(";");
|
||||
valutazioni.add(new Valutazione(valoriValutazione[0], valoriValutazione[1],
|
||||
Double.parseDouble(valoriValutazione[2])));
|
||||
riga = bf.readLine();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("Errore nella lettura del file: " + e.getMessage());
|
||||
pausa();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return valutazioni;
|
||||
@@ -174,7 +213,8 @@ public class GradeAnalyzer {
|
||||
boolean exit = false;
|
||||
int posizione = -1;
|
||||
for (int i = 0; i < valutazioni.size() && !exit; i++) {
|
||||
if (nome.equalsIgnoreCase(valutazioni.get(i).getNome()) && cognome.equalsIgnoreCase(valutazioni.get(i).getCognome())) {
|
||||
if (nome.equalsIgnoreCase(valutazioni.get(i).getNome())
|
||||
&& cognome.equalsIgnoreCase(valutazioni.get(i).getCognome())) {
|
||||
posizione = i;
|
||||
exit = true;
|
||||
}
|
||||
@@ -182,7 +222,7 @@ public class GradeAnalyzer {
|
||||
return posizione;
|
||||
}
|
||||
|
||||
static void salva() {
|
||||
static void salvaValutazioni() {
|
||||
boolean exit;
|
||||
String scelta;
|
||||
do {
|
||||
@@ -192,24 +232,9 @@ public class GradeAnalyzer {
|
||||
|
||||
switch (scelta) {
|
||||
case "s":
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(PATH_VOTI))) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Valutazione valutazione : valutazioni) {
|
||||
sb.append(valutazione.getNome());
|
||||
sb.append(";");
|
||||
sb.append(valutazione.getCognome());
|
||||
sb.append(";");
|
||||
sb.append(valutazione.getVoto());
|
||||
sb.append("\n");
|
||||
}
|
||||
|
||||
bw.write(sb.toString());
|
||||
System.out.println("Dati salvati correttamente.");
|
||||
valutazioni = valutazioniDaFile();
|
||||
isSaved = true;
|
||||
} catch (IOException e) {
|
||||
System.out.println("Errore nella scrittura del file: " + e.getMessage());
|
||||
}
|
||||
salva(valutazioni, PATH_VOTI);
|
||||
valutazioni = valutazioniDaFile();
|
||||
isSaved = true;
|
||||
break;
|
||||
case "n":
|
||||
System.out.println("I dati non sono stati salvati.");
|
||||
@@ -222,6 +247,25 @@ public class GradeAnalyzer {
|
||||
} while (!exit);
|
||||
}
|
||||
|
||||
static void salva(ArrayList<Valutazione> valutazioni, String path) {
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(path))) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Valutazione valutazione : valutazioni) {
|
||||
sb.append(valutazione.getNome());
|
||||
sb.append(";");
|
||||
sb.append(valutazione.getCognome());
|
||||
sb.append(";");
|
||||
sb.append(valutazione.getVoto());
|
||||
sb.append("\n");
|
||||
}
|
||||
|
||||
bw.write(sb.toString());
|
||||
System.out.println("Dati salvati correttamente.");
|
||||
} catch (IOException e) {
|
||||
System.out.println("Errore nella scrittura del file: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
static void trovaMigliorePeggiore() {
|
||||
ArrayList<Valutazione> copiaValutazioni = new ArrayList<>();
|
||||
for (Valutazione valutazione : valutazioni) {
|
||||
@@ -251,4 +295,31 @@ public class GradeAnalyzer {
|
||||
System.out.println(String.format(stringaFormattazione, nomi[i], cognomi[i], Double.toString(voti[i])));
|
||||
}
|
||||
}
|
||||
|
||||
static void promossi() {
|
||||
final int MINIMO_PROMOZIONE = 6;
|
||||
String stringaFormattazione = "%-20s%-20s%-20s";
|
||||
ArrayList<Valutazione> promossi = new ArrayList<>();
|
||||
ArrayList<Valutazione> copiaValutazioni = new ArrayList<>();
|
||||
for (Valutazione valutazione : valutazioni) {
|
||||
copiaValutazioni.add(valutazione);
|
||||
}
|
||||
|
||||
Collections.sort(copiaValutazioni);
|
||||
|
||||
for (Valutazione valutazione : copiaValutazioni) {
|
||||
if (valutazione.getVoto() >= MINIMO_PROMOZIONE) {
|
||||
promossi.add(valutazione);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("\nTutte le valutazioni dei promossi, in ordine crescente:\n");
|
||||
System.out.println(String.format(stringaFormattazione, "Nome", "Cognome", "Voto"));
|
||||
for (int i = 0; i < promossi.size(); i++) {
|
||||
System.out.println(String.format(stringaFormattazione, promossi.get(i).getNome(),
|
||||
promossi.get(i).getCognome(), Double.toString(promossi.get(i).getVoto())));
|
||||
}
|
||||
|
||||
salva(promossi, PATH_PROMOSSI);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user