Il 0% ha trovato utile questo documento (0 voti)
5 visualizzazioni

message (3)

Copyright
© © All Rights Reserved
Per noi i diritti sui contenuti sono una cosa seria. Se sospetti che questo contenuto sia tuo, rivendicalo qui.
Formati disponibili
Scarica in formato TXT, PDF, TXT o leggi online su Scribd
Il 0% ha trovato utile questo documento (0 voti)
5 visualizzazioni

message (3)

Copyright
© © All Rights Reserved
Per noi i diritti sui contenuti sono una cosa seria. Se sospetti che questo contenuto sia tuo, rivendicalo qui.
Formati disponibili
Scarica in formato TXT, PDF, TXT o leggi online su Scribd
Sei sulla pagina 1/ 4

import javax.swing.

*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class GestioneVoti extends JFrame {


private JTextField materiaInput;
private JTextArea outputArea;
private JTextField nuovoVotoInput;
private JTextField cancellaVotoInput;
private Map<String, List<Integer>> votiMap;

public GestioneVoti() {
votiMap = new HashMap<>();

setTitle("Gestione Voti");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());

JPanel topPanel = new JPanel(new GridLayout(2, 1));

JPanel importPanel = new JPanel();


JButton importButton = new JButton("Importa da voti.csv");
importButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
importaVoti();
}
});
importPanel.add(importButton);

JPanel materiaPanel = new JPanel();


materiaInput = new JTextField(15);
JButton visualizzaButton = new JButton("Visualizza Voti");
visualizzaButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
visualizzaVoti();
}
});
materiaPanel.add(new JLabel("Materia:"));
materiaPanel.add(materiaInput);
materiaPanel.add(visualizzaButton);

topPanel.add(importPanel);
topPanel.add(materiaPanel);

add(topPanel, BorderLayout.NORTH);

outputArea = new JTextArea();


add(new JScrollPane(outputArea), BorderLayout.CENTER);

JPanel bottomPanel = new JPanel(new GridLayout(3, 1));

JPanel aggiungiPanel = new JPanel();


nuovoVotoInput = new JTextField(5);
JButton aggiungiButton = new JButton("Aggiungi Voto");
aggiungiButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
aggiungiVoto();
}
});
aggiungiPanel.add(new JLabel("Nuovo Voto:"));
aggiungiPanel.add(nuovoVotoInput);
aggiungiPanel.add(aggiungiButton);

JPanel cancellaPanel = new JPanel();


cancellaVotoInput = new JTextField(5);
JButton cancellaButton = new JButton("Cancella Voto");
cancellaButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cancellaVoto();
}
});
cancellaPanel.add(new JLabel("Voto da cancellare:"));
cancellaPanel.add(cancellaVotoInput);
cancellaPanel.add(cancellaButton);

JPanel mediaPanel = new JPanel();


JButton mediaButton = new JButton("Calcola Media");
mediaButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calcolaMedia();
}
});
JButton salvaButton = new JButton("Salva su file");
salvaButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
salvaVoti();
}
});
mediaPanel.add(mediaButton);
mediaPanel.add(salvaButton);

bottomPanel.add(aggiungiPanel);
bottomPanel.add(cancellaPanel);
bottomPanel.add(mediaPanel);

add(bottomPanel, BorderLayout.SOUTH);
}

private void importaVoti() {


try (BufferedReader br = new BufferedReader(new FileReader("voti.csv"))) {
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(",");
String materia = parts[0];
List<Integer> voti = new ArrayList<>();
for (int i = 1; i < parts.length; i++) {
voti.add(Integer.parseInt(parts[i]));
}
votiMap.put(materia, voti);
}
outputArea.setText("Dati importati con successo.");
} catch (IOException e) {
e.printStackTrace();
outputArea.setText("Errore durante l'importazione dei dati.");
}
}

private void visualizzaVoti() {


String materia = materiaInput.getText().trim();
if (votiMap.containsKey(materia)) {
outputArea.setText("Voti per " + materia + ": " +
votiMap.get(materia));
} else {
outputArea.setText("Materia non trovata.");
}
}

private void aggiungiVoto() {


String materia = materiaInput.getText().trim();
int voto = Integer.parseInt(nuovoVotoInput.getText().trim());
if (votiMap.containsKey(materia)) {
votiMap.get(materia).add(voto);
outputArea.setText("Voto aggiunto con successo.");
} else {
outputArea.setText("Materia non trovata.");
}
}

private void cancellaVoto() {


String materia = materiaInput.getText().trim();
int voto = Integer.parseInt(cancellaVotoInput.getText().trim());
if (votiMap.containsKey(materia)) {
votiMap.get(materia).remove((Integer) voto);
outputArea.setText("Voto cancellato con successo.");
} else {
outputArea.setText("Materia non trovata.");
}
}

private void calcolaMedia() {


String materia = materiaInput.getText().trim();
if (materia.equals("tutte")) {
double sommaMedia = 0;
int count = 0;
for (Map.Entry<String, List<Integer>> entry : votiMap.entrySet()) {
List<Integer> voti = entry.getValue();
sommaMedia +=
voti.stream().mapToInt(Integer::intValue).average().orElse(0);
count++;
}
double media = sommaMedia / count;
outputArea.setText("Media di tutte le materie: " + media);
} else if (votiMap.containsKey(materia)) {
List<Integer> voti = votiMap.get(materia);
double media =
voti.stream().mapToInt(Integer::intValue).average().orElse(0);
outputArea.setText("Media dei voti per " + materia + ": " + media);
} else {
outputArea.setText("Materia non trovata.");
}
}

private void salvaVoti() {


try (PrintWriter pw = new PrintWriter(new FileWriter("voti.csv"))) {
for (Map.Entry<String, List<Integer>> entry : votiMap.entrySet()) {
pw.print(entry.getKey());
for (int voto : entry.getValue()) {
pw.print("," + voto);
}
pw.println();
}
outputArea.setText("Dati salvati con successo.");
} catch (IOException e) {
e.printStackTrace();
outputArea.setText("Errore durante il salvataggio dei dati.");
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GestioneVoti().setVisible(true);
}
});
}
}

Potrebbero piacerti anche