360 lines
13 KiB
Java
360 lines
13 KiB
Java
/*
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
|
|
*/
|
|
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;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.Scanner;
|
|
|
|
import com.googlecode.lanterna.TerminalPosition;
|
|
import com.googlecode.lanterna.gui2.BasicWindow;
|
|
import com.googlecode.lanterna.gui2.Button;
|
|
import com.googlecode.lanterna.gui2.Direction;
|
|
import com.googlecode.lanterna.gui2.EmptySpace;
|
|
import com.googlecode.lanterna.gui2.Label;
|
|
import com.googlecode.lanterna.gui2.LinearLayout;
|
|
import com.googlecode.lanterna.gui2.MultiWindowTextGUI;
|
|
import com.googlecode.lanterna.gui2.Panel;
|
|
import com.googlecode.lanterna.gui2.Window;
|
|
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
|
|
import com.googlecode.lanterna.gui2.menu.MenuBar;
|
|
import com.googlecode.lanterna.gui2.menu.MenuItem;
|
|
import com.googlecode.lanterna.gui2.menu.Menu;
|
|
import com.googlecode.lanterna.screen.Screen;
|
|
import com.googlecode.lanterna.screen.TerminalScreen;
|
|
import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
|
|
|
|
/**
|
|
*
|
|
* @author Verde
|
|
*/
|
|
public class GradeAnalyzer {
|
|
|
|
/**
|
|
* @param args the command line arguments
|
|
*/
|
|
|
|
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<Valutazione> valutazioni = valutazioniDaFile();
|
|
static boolean isSaved = true;
|
|
static boolean isEmpty = true;
|
|
|
|
/*
|
|
public static void main(String[] args) {
|
|
int scelta = -1;
|
|
|
|
do {
|
|
System.out.println("Scegliere un'opzione:");
|
|
System.out.println("1. Inserisci dati alunno");
|
|
System.out.println("2. Trova voto più alto e più basso");
|
|
System.out.println("3. Promossi");
|
|
System.out.println("0. Esci");
|
|
System.out.print("Opzione: ");
|
|
|
|
try {
|
|
scelta = sc.nextInt();
|
|
sc.nextLine();
|
|
|
|
switch (scelta) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
isSaved = false;
|
|
isEmpty = false;
|
|
inserisciDati();
|
|
System.out.println("Studente aggiunto correttamente.");
|
|
pausa();
|
|
break;
|
|
case 2:
|
|
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();
|
|
}
|
|
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:
|
|
System.out.println(ERRORE_GENERICO);
|
|
pausa();
|
|
break;
|
|
}
|
|
} catch (InputMismatchException _) {
|
|
System.out.println("Errore: scelta non valida.");
|
|
pausa();
|
|
}
|
|
} while (scelta != 0);
|
|
}
|
|
*/
|
|
|
|
public static void main(String[] args) {
|
|
try (Screen schermo = new TerminalScreen(new DefaultTerminalFactory().createTerminal())) {
|
|
schermo.startScreen(); //crea terminale, inserisci CRT e dai corrente
|
|
final WindowBasedTextGUI gui = new MultiWindowTextGUI(schermo); //crea interfaccia con le finestre
|
|
|
|
gui.addWindowAndWait(new FinestraPrincipale(gui));
|
|
|
|
}
|
|
catch (IOException e) {
|
|
System.out.println("Qualcosa è andato storto nella creazione dell'interfaccia.");
|
|
System.out.println(e.getMessage());
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public static void pausa() {
|
|
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));
|
|
}
|
|
|
|
salvaValutazioni();
|
|
}
|
|
|
|
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<Valutazione> valutazioniDaFile() {
|
|
ArrayList<Valutazione> valutazioni = new ArrayList<>();
|
|
|
|
File voti = new File(PATH_VOTI);
|
|
|
|
if (!voti.exists()) {
|
|
if (!voti.getParentFile().exists()) {
|
|
voti.getParentFile().mkdir();
|
|
}
|
|
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;
|
|
}
|
|
|
|
static int exists(String nome, String cognome, ArrayList<Valutazione> valutazioni) {
|
|
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())) {
|
|
posizione = i;
|
|
exit = true;
|
|
}
|
|
}
|
|
return posizione;
|
|
}
|
|
|
|
static void salvaValutazioni() {
|
|
boolean exit;
|
|
String scelta;
|
|
do {
|
|
exit = true;
|
|
System.out.print("Salvare i dati [S/N]? ");
|
|
scelta = sc.nextLine().trim().toLowerCase();
|
|
|
|
switch (scelta) {
|
|
case "s":
|
|
salva(valutazioni, PATH_VOTI);
|
|
valutazioni = valutazioniDaFile();
|
|
isSaved = true;
|
|
break;
|
|
case "n":
|
|
System.out.println("I dati non sono stati salvati.");
|
|
break;
|
|
default:
|
|
System.out.println(ERRORE_GENERICO);
|
|
pausa();
|
|
exit = false;
|
|
}
|
|
} 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) {
|
|
copiaValutazioni.add(valutazione);
|
|
}
|
|
String[] nomi = new String[valutazioni.size()];
|
|
String[] cognomi = new String[valutazioni.size()];
|
|
double[] voti = new double[valutazioni.size()];
|
|
String stringaFormattazione = "%-20s%-20s%-20s";
|
|
|
|
Collections.sort(copiaValutazioni);
|
|
|
|
for (int i = 0; i < copiaValutazioni.size(); i++) {
|
|
nomi[i] = copiaValutazioni.get(i).getNome();
|
|
cognomi[i] = copiaValutazioni.get(i).getCognome();
|
|
voti[i] = copiaValutazioni.get(i).getVoto();
|
|
}
|
|
|
|
System.out.println("Voto migliore:");
|
|
System.out.println(copiaValutazioni.get(copiaValutazioni.size() - 1).toString());
|
|
System.out.println("\nVoto peggiore:");
|
|
System.out.println(copiaValutazioni.get(0).toString());
|
|
|
|
System.out.println("\nTutte le valutazioni, in ordine crescente:\n");
|
|
System.out.println(String.format(stringaFormattazione, "Nome", "Cognome", "Voto"));
|
|
for (int i = 0; i < nomi.length; 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);
|
|
}
|
|
}
|