From eb4581c1834fe79708a34f8e96202b500e1aa210 Mon Sep 17 00:00:00 2001 From: tattommasi <47092404+tattommasi@users.noreply.github.com> Date: Sat, 13 Apr 2019 00:57:23 +0200 Subject: [PATCH 01/10] soluzione --- .../it/polito/tdp/lab04/DAO/StudenteDAO.java | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/DAO/StudenteDAO.java b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/DAO/StudenteDAO.java index cbffc31..12d37ce 100644 --- a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/DAO/StudenteDAO.java +++ b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/DAO/StudenteDAO.java @@ -1,5 +1,105 @@ package it.polito.tdp.lab04.DAO; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.LinkedList; +import java.util.List; + +import it.polito.tdp.lab04.model.Corso; +import it.polito.tdp.lab04.model.Studente; + public class StudenteDAO { + /* + * Controllo se uno studente (matricola) è iscritto ad un corso (codins) + */ + public boolean isStudenteIscrittoACorso(Studente studente, Corso corso) { + + final String sql = "SELECT * FROM iscrizione where codins=? and matricola=?"; + boolean returnValue = false; + + try { + Connection conn = ConnectDB.getConnection(); + PreparedStatement st = conn.prepareStatement(sql); + st.setString(1, corso.getCodins()); + st.setInt(2, studente.getMatricola()); + + ResultSet rs = st.executeQuery(); + + if (rs.next()) + returnValue = true; + + conn.close(); + + } catch (SQLException e) { + e.printStackTrace(); + throw new RuntimeException("Errore Db"); + } + + return returnValue; + } + + /* + * Data una matricola ottengo la lista dei corsi (codins) a cui è iscritto + */ + public List getCorsiFromStudente(Studente studente) { + + final String sql = "SELECT * FROM iscrizione, corso WHERE iscrizione.codins=corso.codins AND matricola=?"; + + List corsi = new LinkedList(); + + try { + Connection conn = ConnectDB.getConnection(); + PreparedStatement st = conn.prepareStatement(sql); + st.setInt(1, studente.getMatricola()); + ResultSet rs = st.executeQuery(); + + while (rs.next()) { + + Corso corso = new Corso(rs.getString("codins"), rs.getInt("crediti"), rs.getString("nome"), + rs.getInt("pd")); + corsi.add(corso); + } + + conn.close(); + + } catch (SQLException e) { + e.printStackTrace(); + throw new RuntimeException("Errore Db"); + } + + return corsi; + } + + /* + * Data una matricola ottengo lo studente. + */ + public Studente getStudente(int matricola) { + + final String sql = "SELECT * FROM studente where matricola=?"; + Studente studente = null; + + try { + Connection conn = ConnectDB.getConnection(); + PreparedStatement st = conn.prepareStatement(sql); + st.setInt(1, matricola); + + ResultSet rs = st.executeQuery(); + + if (rs.next()) { + studente = new Studente(matricola, rs.getString("nome"), rs.getString("cognome"), rs.getString("cds")); + } + + conn.close(); + + } catch (SQLException e) { + e.printStackTrace(); + throw new RuntimeException("Errore Db"); + } + + return studente; + } + } From 30a84c76e23c377acc5b7ad92b0d3139560bf3dd Mon Sep 17 00:00:00 2001 From: tattommasi <47092404+tattommasi@users.noreply.github.com> Date: Sat, 13 Apr 2019 00:59:33 +0200 Subject: [PATCH 02/10] soluzione --- .../src/it/polito/tdp/lab04/DAO/TestDB.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/DAO/TestDB.java b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/DAO/TestDB.java index 001c89d..76358a2 100644 --- a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/DAO/TestDB.java +++ b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/DAO/TestDB.java @@ -8,8 +8,14 @@ public static void main(String[] args) { * This is a main to check the DB connection */ - CorsoDAO cdao = new CorsoDAO(); - cdao.getTuttiICorsi(); + try { + CorsoDAO cdao = new CorsoDAO(); + cdao.getTuttiICorsi(); + System.out.println("TestDB passed"); + + } catch (RuntimeException e) { + System.err.println("TestDB failed"); + } } From e2b163c667590d8bdebc0eff22ca9c5aebb07c5f Mon Sep 17 00:00:00 2001 From: tattommasi <47092404+tattommasi@users.noreply.github.com> Date: Sat, 13 Apr 2019 01:11:58 +0200 Subject: [PATCH 03/10] soluzione --- .../src/it/polito/tdp/lab04/DAO/CorsoDAO.java | 91 ++++++++++++++++--- 1 file changed, 77 insertions(+), 14 deletions(-) diff --git a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/DAO/CorsoDAO.java b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/DAO/CorsoDAO.java index bcf7854..dc17c7f 100644 --- a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/DAO/CorsoDAO.java +++ b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/DAO/CorsoDAO.java @@ -29,45 +29,108 @@ public List getTuttiICorsi() { while (rs.next()) { - String codins = rs.getString("codins"); - int numeroCrediti = rs.getInt("crediti"); - String nome = rs.getString("nome"); - int periodoDidattico = rs.getInt("pd"); + //String codins = rs.getString("codins"); + //int numeroCrediti = rs.getInt("crediti"); + //String nome = rs.getString("nome"); + //int periodoDidattico = rs.getInt("pd"); - System.out.println(codins + " " + numeroCrediti + " " + nome + " " + periodoDidattico); + //System.out.println(codins + " " + numeroCrediti + " " + nome + " " + periodoDidattico); - // Crea un nuovo JAVA Bean Corso - // Aggiungi il nuovo oggetto Corso alla lista corsi + Corso s = new Corso(rs.getString("codins"), rs.getInt("crediti"), rs.getString("nome"), rs.getInt("pd")); + corsi.add(s); } - return corsi; - + conn.close(); + } catch (SQLException e) { // e.printStackTrace(); throw new RuntimeException("Errore Db", e); } + return corsi; } /* * Dato un codice insegnamento, ottengo il corso */ public void getCorso(Corso corso) { - // TODO + final String sql = "SELECT * FROM corso where codins=?"; + + try { + Connection conn = ConnectDB.getConnection(); + PreparedStatement st = conn.prepareStatement(sql); + st.setString(1, corso.getCodins()); + + ResultSet rs = st.executeQuery(); + + if (rs.next()) { + corso.setCrediti(rs.getInt("crediti")); + corso.setNome(rs.getString("nome")); + corso.setPd(rs.getInt("pd")); + } + + conn.close(); + + } catch (SQLException e) { + e.printStackTrace(); + throw new RuntimeException("Errore Db", e); + } } /* * Ottengo tutti gli studenti iscritti al Corso */ public void getStudentiIscrittiAlCorso(Corso corso) { - // TODO + final String sql = "SELECT * FROM iscrizione, studente WHERE iscrizione.matricola=studente.matricola AND codins=?"; + + List studentiIscrittiAlCorso = new ArrayList(); + + try { + Connection conn = ConnectDB.getConnection(); + PreparedStatement st = conn.prepareStatement(sql); + st.setString(1, corso.getCodins()); + + ResultSet rs = st.executeQuery(); + + while (rs.next()) { + + studentiIscrittiAlCorso.add(new Studente(rs.getInt("matricola"), rs.getString("nome"), rs.getString("cognome"), rs.getString("cds"))); + } + + conn.close(); + + } catch (SQLException e) { + e.printStackTrace(); + throw new RuntimeException("Errore Db", e); + } + + return studentiIscrittiAlCorso; } /* * Data una matricola ed il codice insegnamento, iscrivi lo studente al corso. */ public boolean inscriviStudenteACorso(Studente studente, Corso corso) { - // TODO - // ritorna true se l'iscrizione e' avvenuta con successo - return false; + String sql = "INSERT IGNORE INTO `iscritticorsi`.`iscrizione` (`matricola`, `codins`) VALUES(?,?)"; + boolean returnValue = false; + + try { + Connection conn = ConnectDB.getConnection(); + PreparedStatement st = conn.prepareStatement(sql); + st.setInt(1, studente.getMatricola()); + st.setString(2, corso.getCodins()); + + int res = st.executeUpdate(); + + if (res == 1) + returnValue = true; + + conn.close(); + + } catch (SQLException e) { + e.printStackTrace(); + throw new RuntimeException("Errore Db", e); + } + + return returnValue; } } From b848cdb159284c787a606f4a98b6ba4472a1797e Mon Sep 17 00:00:00 2001 From: tattommasi <47092404+tattommasi@users.noreply.github.com> Date: Sat, 13 Apr 2019 01:14:56 +0200 Subject: [PATCH 04/10] soluzione --- .../src/it/polito/tdp/lab04/controller/Main.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/controller/Main.java b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/controller/Main.java index 6c62254..de0da01 100644 --- a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/controller/Main.java +++ b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/controller/Main.java @@ -1,5 +1,6 @@ package it.polito.tdp.lab04.controller; +import it.polito.tdp.lab04.model.Model; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; @@ -16,11 +17,8 @@ public void start(Stage primaryStage) { BorderPane root = (BorderPane) loader.load(); SegreteriaStudentiController controller = loader.getController(); - - /* - * Create and set the model here! - */ - // controller.setModel(); + Model model = new Model(); + controller.setModel(model); Scene scene = new Scene(root); scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); From e3e503d983e97ba75632e7f30972a804846bf933 Mon Sep 17 00:00:00 2001 From: tattommasi <47092404+tattommasi@users.noreply.github.com> Date: Sat, 13 Apr 2019 01:16:36 +0200 Subject: [PATCH 05/10] soluzione --- .../SegreteriaStudentiController.java | 252 ++++++++++++++++++ 1 file changed, 252 insertions(+) diff --git a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/controller/SegreteriaStudentiController.java b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/controller/SegreteriaStudentiController.java index c6efb03..c8712b9 100644 --- a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/controller/SegreteriaStudentiController.java +++ b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/controller/SegreteriaStudentiController.java @@ -1,5 +1,257 @@ package it.polito.tdp.lab04.controller; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +import it.polito.tdp.lab04.model.Corso; +import it.polito.tdp.lab04.model.Model; +import it.polito.tdp.lab04.model.Studente; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.control.Button; +import javafx.scene.control.ComboBox; +import javafx.scene.control.TextArea; +import javafx.scene.control.TextField; + public class SegreteriaStudentiController { + private Model model; + List corsi; + + @FXML + private ComboBox comboCorso; + + @FXML + private Button btnCercaIscrittiCorso; + + @FXML + private Button btnCercaCorsi; + + @FXML + private Button btnCercaNome; + + @FXML + private TextArea txtResult; + + @FXML + private Button btnIscrivi; + + @FXML + private TextField txtMatricola; + + @FXML + private Button btnReset; + + @FXML + private TextField txtNome; + + @FXML + private TextField txtCognome; + + public void setModel(Model model) { + + this.model = model; + setComboItems(); + } + + private void setComboItems() { + + // Ottieni tutti i corsi dal model + corsi = model.getTuttiICorsi(); + + // Aggiungi tutti i corsi alla ComboBox + Collections.sort(corsi); + comboCorso.getItems().addAll(corsi); + } + + @FXML + void doReset(ActionEvent event) { + + txtMatricola.clear(); + txtResult.clear(); + txtNome.clear(); + txtCognome.clear(); + comboCorso.getSelectionModel().clearSelection(); + } + + /* + * Data la matricola, cerca nome e cognome + */ + @FXML + void doCercaNome(ActionEvent event) { + + txtResult.clear(); + txtNome.clear(); + txtCognome.clear(); + + try { + + int matricola = Integer.parseInt(txtMatricola.getText()); + Studente studente = model.getStudente(matricola); + + if (studente == null) { + txtResult.appendText("Nessun risultato: matricola inesistente"); + return; + } + + txtNome.setText(studente.getNome()); + txtCognome.setText(studente.getCognome()); + + } catch (NumberFormatException e) { + txtResult.setText("Inserire una matricola nel formato corretto."); + } catch (RuntimeException e) { + txtResult.setText("ERRORE DI CONNESSIONE AL DATABASE!"); + } + } + + /* + * Data una matricola devo stampare tutti i corsi a cui lo studente è iscritto. + */ + @FXML + void doCercaCorsi(ActionEvent event) { + + txtResult.clear(); + + try { + + int matricola = Integer.parseInt(txtMatricola.getText()); + + Studente studente = model.getStudente(matricola); + if (studente == null) { + txtResult.appendText("Nessun risultato: matricola inesistente"); + return; + } + + List corsi = model.cercaCorsiDatoStudente(studente); + + StringBuilder sb = new StringBuilder(); + + for (Corso corso : corsi) { + sb.append(String.format("%-8s ", corso.getCodins())); + sb.append(String.format("%-4s ", corso.getCrediti())); + sb.append(String.format("%-45s ", corso.getNome())); + sb.append(String.format("%-4s ", corso.getPd())); + sb.append("\n"); + } + txtResult.appendText(sb.toString()); + + } catch (NumberFormatException e) { + txtResult.setText("Inserire una matricola nel formato corretto."); + } catch (RuntimeException e) { + txtResult.setText("ERRORE DI CONNESSIONE AL DATABASE!"); + } + } + + /* + * Dato un corso, devo stamapre le informazioni su tutti gli studenti. + */ + @FXML + void doCercaIscrittiCorso(ActionEvent event) { + + txtResult.clear(); + txtNome.clear(); + txtCognome.clear(); + + try { + + Corso corso = comboCorso.getValue(); + if (corso == null) { + txtResult.setText("Selezionare un corso."); + return; + } + + List studenti = model.studentiIscrittiAlCorso(corso); + + StringBuilder sb = new StringBuilder(); + + for (Studente studente : studenti) { + + sb.append(String.format("%-10s ", studente.getMatricola())); + sb.append(String.format("%-20s ", studente.getCognome())); + sb.append(String.format("%-20s ", studente.getNome())); + sb.append(String.format("%-10s ", studente.getCds())); + sb.append("\n"); + } + + txtResult.appendText(sb.toString()); + + } catch (RuntimeException e) { + txtResult.setText("ERRORE DI CONNESSIONE AL DATABASE!"); + } + } + + @FXML + void doIscrivi(ActionEvent event) { + + txtResult.clear(); + + try { + + if (txtMatricola.getText().isEmpty()) { + txtResult.setText("Inserire una matricola."); + return; + } + + if (comboCorso.getValue() == null) { + txtResult.setText("Selezionare un corso."); + return; + } + + // Prendo la matricola in input + int matricola = Integer.parseInt(txtMatricola.getText()); + + // (opzionale) + // Inserisco il Nome e Cognome dello studente nell'interfaccia + Studente studente = model.getStudente(matricola); + if (studente == null) { + txtResult.appendText("Nessun risultato: matricola inesistente"); + return; + } + + txtNome.setText(studente.getNome()); + txtCognome.setText(studente.getCognome()); + + // Ottengo il nome del corso + Corso corso = comboCorso.getValue(); + + // Controllo se lo studente è già iscritto al corso + if (model.isStudenteIscrittoACorso(studente, corso)) { + txtResult.appendText("Studente già iscritto a questo corso"); + return; + } + + // Iscrivo lo studente al corso. + // Controllo che l'inserimento vada a buon fine + if (!model.inscriviStudenteACorso(studente, corso)) { + txtResult.appendText("Errore durante l'iscrizione al corso"); + return; + } else { + txtResult.appendText("Studente iscritto al corso!"); + } + + } catch (NumberFormatException e) { + txtResult.setText("Inserire una matricola nel formato corretto."); + } catch (RuntimeException e) { + txtResult.setText("ERRORE DI CONNESSIONE AL DATABASE!"); + } + } + + @FXML + void initialize() { + assert comboCorso != null : "fx:id=\"comboCorso\" was not injected: check your FXML file 'SegreteriaStudenti.fxml'."; + assert btnCercaIscrittiCorso != null : "fx:id=\"btnCercaIscrittiCorso\" was not injected: check your FXML file 'SegreteriaStudenti.fxml'."; + assert btnCercaCorsi != null : "fx:id=\"btnCercaCorsi\" was not injected: check your FXML file 'SegreteriaStudenti.fxml'."; + assert btnCercaNome != null : "fx:id=\"btnCercaNome\" was not injected: check your FXML file 'SegreteriaStudenti.fxml'."; + assert txtNome != null : "fx:id=\"txtNome\" was not injected: check your FXML file 'SegreteriaStudenti.fxml'."; + assert txtResult != null : "fx:id=\"txtResult\" was not injected: check your FXML file 'SegreteriaStudenti.fxml'."; + assert txtCognome != null : "fx:id=\"txtCognome\" was not injected: check your FXML file 'SegreteriaStudenti.fxml'."; + assert btnIscrivi != null : "fx:id=\"btnIscrivi\" was not injected: check your FXML file 'SegreteriaStudenti.fxml'."; + assert txtMatricola != null : "fx:id=\"txtMatricola\" was not injected: check your FXML file 'SegreteriaStudenti.fxml'."; + assert btnReset != null : "fx:id=\"btnReset\" was not injected: check your FXML file 'SegreteriaStudenti.fxml'."; + + // Utilizzare questo font per incolonnare correttamente i dati + txtResult.setStyle("-fx-font-family: monospace"); + } + } From 7cbbc2714eabca9a777c17cacdd8cc405736857f Mon Sep 17 00:00:00 2001 From: tattommasi <47092404+tattommasi@users.noreply.github.com> Date: Sat, 13 Apr 2019 01:18:35 +0200 Subject: [PATCH 06/10] soluzione --- .../lab04/controller/SegreteriaStudenti.fxml | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/controller/SegreteriaStudenti.fxml b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/controller/SegreteriaStudenti.fxml index 6de7542..7caa52f 100644 --- a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/controller/SegreteriaStudenti.fxml +++ b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/controller/SegreteriaStudenti.fxml @@ -1,8 +1,15 @@ + + + + + + + @@ -11,4 +18,99 @@ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + +
From e7e1f3e1ac9264eb1d79a3266b9f1cbc49470b06 Mon Sep 17 00:00:00 2001 From: tattommasi <47092404+tattommasi@users.noreply.github.com> Date: Sat, 13 Apr 2019 01:19:54 +0200 Subject: [PATCH 07/10] soluzione --- .../src/it/polito/tdp/lab04/model/Corso.java | 107 +++++++++++++++++- 1 file changed, 106 insertions(+), 1 deletion(-) diff --git a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/model/Corso.java b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/model/Corso.java index cc59505..51f9792 100644 --- a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/model/Corso.java +++ b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/model/Corso.java @@ -1,5 +1,110 @@ package it.polito.tdp.lab04.model; -public class Corso { +import java.util.ArrayList; +import java.util.List; +public class Corso implements Comparable { + + private String codins; + private String nome; + private int crediti; + private int pd; + + public Corso() { + } + + public Corso(String codins) { + this.codins = codins; + } + + public Corso(String codins, int crediti, String nome, int pd) { + this.codins = codins; + this.crediti = crediti; + this.nome = nome; + this.pd = pd; + } + + /* + * Getters and Setters + */ + public String getCodins() { + if (codins == null) + return ""; + return codins; + } + + public void setCodins(String codins) { + this.codins = codins; + } + + public String getNome() { + if (nome == null) + return ""; + return nome; + } + + public void setNome(String nome) { + this.nome = nome; + } + + public int getCrediti() { + return crediti; + } + + public void setCrediti(int crediti) { + this.crediti = crediti; + } + + public int getPd() { + return pd; + } + + public void setPd(int pd) { + this.pd = pd; + } + + /* + * Hash Code and Equals + */ + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((nome == null) ? 0 : nome.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Corso other = (Corso) obj; + if (nome == null) { + if (other.nome != null) + return false; + } else if (!nome.equals(other.nome)) + return false; + return true; + } + + /* + * Usato per ordinare alfabeticamente (in ordine crescente) i nomi dei corsi. + */ + @Override + public int compareTo(Corso corsoInput) { + return this.nome.compareTo(corsoInput.nome); + } + + /* + * Metodo toString() + */ + @Override + public String toString() { + return nome; + } } From 8bf918f7eebdaf8c29b77d692afc856f73d214b4 Mon Sep 17 00:00:00 2001 From: tattommasi <47092404+tattommasi@users.noreply.github.com> Date: Sat, 13 Apr 2019 01:21:23 +0200 Subject: [PATCH 08/10] soluzione --- .../src/it/polito/tdp/lab04/model/Model.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/model/Model.java b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/model/Model.java index 9ad5d83..07d383f 100644 --- a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/model/Model.java +++ b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/model/Model.java @@ -1,5 +1,58 @@ package it.polito.tdp.lab04.model; +import java.util.List; + +import it.polito.tdp.lab04.DAO.CorsoDAO; +import it.polito.tdp.lab04.DAO.StudenteDAO; + public class Model { + private StudenteDAO studenteDao; + private CorsoDAO corsoDao; + + public Model() { + + studenteDao = new StudenteDAO(); + corsoDao = new CorsoDAO(); + } + + public Studente getStudente(int matricola) { + + return studenteDao.getStudente(matricola); + } + + public List studentiIscrittiAlCorso(Corso corso) { + + return corsoDao.getStudentiIscrittiAlCorso(corso); + } + + public List cercaCorsiDatoStudente(Studente studente) { + + return studenteDao.getCorsiFromStudente(studente); + } + + /* + * Ritorna TRUE se lo studente è iscritto al corso, FALSE altrimenti + */ + public boolean isStudenteIscrittoACorso(Studente studente, Corso corso) { + + return studenteDao.isStudenteIscrittoACorso(studente, corso); + } + + /* + * Iscrivi una studente ad un corso. Ritorna TRUE se l'operazione va a buon fine. + */ + public boolean inscriviStudenteACorso(Studente studente, Corso corso) { + + return corsoDao.inscriviStudenteACorso(studente, corso); + } + + /* + * Ritorna tutti i corsi + */ + public List getTuttiICorsi() { + + return corsoDao.getTuttiICorsi(); + } + } From ee426e111f6296463ccd95eea0046188ff360041 Mon Sep 17 00:00:00 2001 From: tattommasi <47092404+tattommasi@users.noreply.github.com> Date: Sat, 13 Apr 2019 01:22:22 +0200 Subject: [PATCH 09/10] soluzione --- .../it/polito/tdp/lab04/model/Studente.java | 98 ++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/model/Studente.java b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/model/Studente.java index ca7ee61..273b26a 100644 --- a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/model/Studente.java +++ b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/model/Studente.java @@ -1,5 +1,101 @@ package it.polito.tdp.lab04.model; +import java.util.ArrayList; +import java.util.List; + public class Studente { - + + private int matricola; + private String cognome; + private String nome; + private String cds; + + public Studente() { + } + + public Studente(int matricola) { + this.matricola = matricola; + } + + public Studente(int matricola, String cognome, String nome, String cds) { + this.matricola = matricola; + this.cognome = cognome; + this.nome = nome; + this.cds = cds; + } + + /* + * Getters and Setters + */ + public int getMatricola() { + return matricola; + } + + public void setMatricola(int matricola) { + this.matricola = matricola; + } + + public String getCognome() { + if (cognome == null) + return ""; + return cognome; + } + + public void setCognome(String cognome) { + this.cognome = cognome; + } + + public String getNome() { + if (nome == null) + return ""; + return nome; + } + + public void setNome(String nome) { + this.nome = nome; + } + + public String getCds() { + if (cds == null) + return ""; + return cds; + } + + public void setCds(String cds) { + this.cds = cds; + } + + /* + * Hash Code and Equals + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + matricola; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Studente other = (Studente) obj; + if (matricola != other.matricola) + return false; + return true; + } + + /* + * Metodo toString() + */ + @Override + public String toString() { + return cognome + " " + nome; + } + } From 063e51ff50abff9010b1a7e417d0debde469eebe Mon Sep 17 00:00:00 2001 From: tattommasi <47092404+tattommasi@users.noreply.github.com> Date: Sat, 13 Apr 2019 01:23:13 +0200 Subject: [PATCH 10/10] soluzione --- .../it/polito/tdp/lab04/model/TestModel.java | 62 +++++++++++++++++-- 1 file changed, 57 insertions(+), 5 deletions(-) diff --git a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/model/TestModel.java b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/model/TestModel.java index 8a652dd..2c4ce16 100644 --- a/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/model/TestModel.java +++ b/Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/model/TestModel.java @@ -1,15 +1,67 @@ package it.polito.tdp.lab04.model; +import java.util.List; + public class TestModel { public static void main(String[] args) { - + Model model = new Model(); - - /* - * Write here your test model - */ + // Punto 1 + StringBuilder sb = new StringBuilder(); + for (Corso corso : model.getTuttiICorsi()) { + sb.append(String.format("%-8s ", corso.getCodins())); + sb.append(String.format("%-2s ", corso.getCrediti())); + sb.append(String.format("%-50s ", corso.getNome())); + sb.append(String.format("%-4s ", corso.getPd())); + sb.append("\n"); + } + System.out.println(sb.toString()); + + // Punto 2 + Studente studente = model.getStudente(146101); + System.out.println(studente + "\n"); + + // Punto 3 + sb = new StringBuilder(); + for (Studente s : model.studentiIscrittiAlCorso(new Corso("02AQJPG"))) { + sb.append(String.format("%-10s ", s.getMatricola())); + sb.append(String.format("%-20s ", s.getCognome())); + sb.append(String.format("%-20s ", s.getNome())); + sb.append(String.format("%-10s ", s.getCds())); + sb.append("\n"); + } + System.out.println(sb.toString()); + + // Punto 4 + sb = new StringBuilder(); + for (Corso corso : model.cercaCorsiDatoStudente(new Studente(146101))) { + sb.append(String.format("%-8s ", corso.getCodins())); + sb.append(String.format("%-2s ", corso.getCrediti())); + sb.append(String.format("%-50s ", corso.getNome())); + sb.append(String.format("%-4s ", corso.getPd())); + sb.append("\n"); + } + System.out.println(sb.toString()); + + // Punto 5 + boolean result = model.isStudenteIscrittoACorso(new Studente(146101), new Corso("01NBAPG")); + System.out.println(result); + + // Punto 6 + model.inscriviStudenteACorso(new Studente(146101), new Corso("01NBAPG")); + + // Ristampo punto 4 + sb = new StringBuilder(); + for (Corso corso : model.cercaCorsiDatoStudente(new Studente(146101))) { + sb.append(String.format("%-8s ", corso.getCodins())); + sb.append(String.format("%-2s ", corso.getCrediti())); + sb.append(String.format("%-50s ", corso.getNome())); + sb.append(String.format("%-4s ", corso.getPd())); + sb.append("\n"); + } + System.out.println(sb.toString()); } }