diff --git a/src/gradeanalyzer/GradeAnalyzer.java b/src/gradeanalyzer/GradeAnalyzer.java index 2c5fb0d..88034df 100644 --- a/src/gradeanalyzer/GradeAnalyzer.java +++ b/src/gradeanalyzer/GradeAnalyzer.java @@ -4,6 +4,12 @@ */ package gradeanalyzer; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; import java.util.InputMismatchException; import java.util.Scanner; @@ -20,6 +26,9 @@ public class GradeAnalyzer { static Scanner sc = new Scanner(System.in); static final String PATH_VOTI = "./src/gradeanalyzer/data/voti.txt"; static final String PATH_PROMOSSI = "./src/gradeanalyzer/data/promossi.txt"; + static final String ERRORE_GENERICO = "Opzione non valida."; + static ArrayList valutazioni = valutazioniDaFile(); + static boolean isSaved; public static void main(String[] args) { int scelta = -1; @@ -40,11 +49,13 @@ public class GradeAnalyzer { case 0: break; case 1: - + isSaved = false; + inserisciDati(); + System.out.println("Studente aggiunto correttamente."); pausa(); break; case 2: - + pausa(); break; case 3: @@ -52,12 +63,11 @@ public class GradeAnalyzer { pausa(); break; default: - System.out.println("Opzione non valida."); + System.out.println(ERRORE_GENERICO); pausa(); break; } - } - catch (InputMismatchException _) { + } catch (InputMismatchException _) { System.out.println("Errore: scelta non valida."); pausa(); } @@ -68,4 +78,142 @@ public class GradeAnalyzer { System.out.println("Premere un tasto per continuare. . ."); sc.nextLine(); } + + static void inserisciDati() { + String nome; + String cognome; + double voto; + String scelta; + boolean exit; + int posizione; + + System.out.print("Inserisci il nome: "); + nome = sc.nextLine().trim(); + + System.out.print("Inserisci il cognome: "); + cognome = sc.nextLine().trim(); + + voto = voto(); + + if ((posizione = exists(nome, cognome, valutazioni)) != -1) { + System.out.println("Attenzione: lo studente esiste già."); + do { + exit = true; + System.out.print("Aggiornarne i dati [S/N]? "); + scelta = sc.nextLine().trim().toLowerCase(); + + switch (scelta) { + case "s": + valutazioni.get(posizione).setVoto(voto); + break; + case "n": + System.out.println("Lo studente non è stato aggiornato."); + break; + default: + System.out.println(ERRORE_GENERICO); + pausa(); + exit = false; + } + } while (!exit); + } else { + valutazioni.add(new Valutazione(nome, cognome, voto)); + } + + salva(); + } + + static double voto() { + double voto; + boolean error; + final double VOTO_MINIMO = 1; + final double VOTO_MASSIMO = 10; + + do { + error = false; + System.out.print("Inserisci il voto: "); + voto = Math.round((sc.nextDouble() * 10) / 10); + sc.nextLine(); + + if (voto < VOTO_MINIMO || voto > VOTO_MASSIMO) { + System.out.println( + "Errore: il voto deve essere compreso tra " + VOTO_MINIMO + " e " + VOTO_MASSIMO + "."); + pausa(); + error = true; + } + } while (error); + + return voto; + } + + static ArrayList valutazioniDaFile() { + ArrayList 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(); + } + } catch (IOException e) { + System.out.println("Errore nella lettura del file: " + e.getMessage()); + pausa(); + } + + return valutazioni; + } + + static int exists(String nome, String cognome, ArrayList valutazioni) { + boolean exit = false; + int posizione = -1; + for (int i = 0; i < valutazioni.size() && !exit; i++) { + if (nome.equals(valutazioni.get(i).getNome()) && cognome.equals(valutazioni.get(i).getCognome())) { + posizione = i; + exit = true; + } + } + return posizione; + } + + static void salva() { + boolean exit; + String scelta; + do { + exit = true; + System.out.print("Salvare i dati [S/N]? "); + scelta = sc.nextLine().trim().toLowerCase(); + + 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()); + } + break; + case "n": + System.out.println("I dati non sono stati salvati."); + break; + default: + System.out.println(ERRORE_GENERICO); + pausa(); + exit = false; + } + } while (!exit); + } } diff --git a/src/gradeanalyzer/Valutazione.java b/src/gradeanalyzer/Valutazione.java new file mode 100644 index 0000000..ebf2a70 --- /dev/null +++ b/src/gradeanalyzer/Valutazione.java @@ -0,0 +1,38 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package gradeanalyzer; + +/** + * + * @author Verde + */ +public class Valutazione { + private String nome; + private String cognome; + private double voto; + + public Valutazione(String nome, String cognome, double voto) { + this.nome = nome; + this.cognome = cognome; + this.voto = voto; + } + + public String getNome() { + return nome; + } + + public String getCognome() { + return cognome; + } + + public void setVoto(double voto) { + this.voto = voto; + } + + public double getVoto() { + return voto; + } + +} \ No newline at end of file diff --git a/src/gradeanalyzer/Voto.java b/src/gradeanalyzer/Voto.java deleted file mode 100644 index b4a66f9..0000000 --- a/src/gradeanalyzer/Voto.java +++ /dev/null @@ -1,13 +0,0 @@ -/* - * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license - * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template - */ -package gradeanalyzer; - -/** - * - * @author Verde - */ -public class Voto { - -} \ No newline at end of file