Opzione 3 + gestione dati non ancora creati
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
data/
|
||||||
@@ -6,6 +6,7 @@ package gradeanalyzer;
|
|||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -30,6 +31,7 @@ public class GradeAnalyzer {
|
|||||||
static final String ERRORE_GENERICO = "Opzione non valida.";
|
static final String ERRORE_GENERICO = "Opzione non valida.";
|
||||||
static ArrayList<Valutazione> valutazioni = valutazioniDaFile();
|
static ArrayList<Valutazione> valutazioni = valutazioniDaFile();
|
||||||
static boolean isSaved = true;
|
static boolean isSaved = true;
|
||||||
|
static boolean isEmpty = true;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
int scelta = -1;
|
int scelta = -1;
|
||||||
@@ -51,20 +53,38 @@ public class GradeAnalyzer {
|
|||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
isSaved = false;
|
isSaved = false;
|
||||||
|
isEmpty = false;
|
||||||
inserisciDati();
|
inserisciDati();
|
||||||
System.out.println("Studente aggiunto correttamente.");
|
System.out.println("Studente aggiunto correttamente.");
|
||||||
pausa();
|
pausa();
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
if (!isSaved) {
|
if (isEmpty) {
|
||||||
System.out.println("Attenzione: ci sono dati non salvati.");
|
System.out.println(
|
||||||
salva();
|
"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();
|
pausa();
|
||||||
break;
|
break;
|
||||||
case 3:
|
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();
|
pausa();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -124,7 +144,7 @@ public class GradeAnalyzer {
|
|||||||
valutazioni.add(new Valutazione(nome, cognome, voto));
|
valutazioni.add(new Valutazione(nome, cognome, voto));
|
||||||
}
|
}
|
||||||
|
|
||||||
salva();
|
salvaValutazioni();
|
||||||
}
|
}
|
||||||
|
|
||||||
static double voto() {
|
static double voto() {
|
||||||
@@ -152,19 +172,38 @@ public class GradeAnalyzer {
|
|||||||
|
|
||||||
static ArrayList<Valutazione> valutazioniDaFile() {
|
static ArrayList<Valutazione> valutazioniDaFile() {
|
||||||
ArrayList<Valutazione> valutazioni = new ArrayList<>();
|
ArrayList<Valutazione> valutazioni = new ArrayList<>();
|
||||||
try (BufferedReader bf = new BufferedReader(new FileReader(PATH_VOTI))) {
|
|
||||||
String riga = bf.readLine();
|
|
||||||
String[] valoriValutazione;
|
|
||||||
|
|
||||||
while (riga != null) {
|
File voti = new File(PATH_VOTI);
|
||||||
valoriValutazione = riga.split(";");
|
|
||||||
valutazioni.add(new Valutazione(valoriValutazione[0], valoriValutazione[1],
|
if (!voti.exists()) {
|
||||||
Double.parseDouble(valoriValutazione[2])));
|
if (!voti.getParentFile().exists()) {
|
||||||
riga = bf.readLine();
|
voti.getParentFile().mkdir();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(PATH_VOTI))) {
|
||||||
System.out.println("Errore nella lettura del file: " + e.getMessage());
|
bw.write("");
|
||||||
pausa();
|
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;
|
return valutazioni;
|
||||||
@@ -174,7 +213,8 @@ public class GradeAnalyzer {
|
|||||||
boolean exit = false;
|
boolean exit = false;
|
||||||
int posizione = -1;
|
int posizione = -1;
|
||||||
for (int i = 0; i < valutazioni.size() && !exit; i++) {
|
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;
|
posizione = i;
|
||||||
exit = true;
|
exit = true;
|
||||||
}
|
}
|
||||||
@@ -182,7 +222,7 @@ public class GradeAnalyzer {
|
|||||||
return posizione;
|
return posizione;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void salva() {
|
static void salvaValutazioni() {
|
||||||
boolean exit;
|
boolean exit;
|
||||||
String scelta;
|
String scelta;
|
||||||
do {
|
do {
|
||||||
@@ -192,24 +232,9 @@ public class GradeAnalyzer {
|
|||||||
|
|
||||||
switch (scelta) {
|
switch (scelta) {
|
||||||
case "s":
|
case "s":
|
||||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(PATH_VOTI))) {
|
salva(valutazioni, PATH_VOTI);
|
||||||
StringBuilder sb = new StringBuilder();
|
valutazioni = valutazioniDaFile();
|
||||||
for (Valutazione valutazione : valutazioni) {
|
isSaved = true;
|
||||||
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());
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case "n":
|
case "n":
|
||||||
System.out.println("I dati non sono stati salvati.");
|
System.out.println("I dati non sono stati salvati.");
|
||||||
@@ -222,6 +247,25 @@ public class GradeAnalyzer {
|
|||||||
} while (!exit);
|
} 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() {
|
static void trovaMigliorePeggiore() {
|
||||||
ArrayList<Valutazione> copiaValutazioni = new ArrayList<>();
|
ArrayList<Valutazione> copiaValutazioni = new ArrayList<>();
|
||||||
for (Valutazione valutazione : valutazioni) {
|
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])));
|
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