GestioneAgenda.java
Created with JBuilder
public class GestioneAgenda {

  // GestioneAgenda usa GestioneDB per la memorizzazione
  private GestioneDB gestoreDBAgenda;
  private String     tabellaRecapiti;

  // Il costruttore necessita di un riferimento al gestore e del nome
  // della tabella su cui operare
  public GestioneAgenda( GestioneDB gestore, String nomeTabella ) {
    gestoreDBAgenda = gestore;
    tabellaRecapiti = nomeTabella;
  }

  // Inserimento di una persona
  public boolean aggiungi( Persona p ) {
    StringBuffer update = new StringBuffer();
    update.append("INSERT INTO " + tabellaRecapiti + " ");
    update.append("(");
    for (int i = Persona.NOME; i <= Persona.MAIL; i++) {
      update.append(Persona.NOMI_CAMPI[i]);
      if (i < Persona.MAIL) {
        update.append(",");
      }
    }
    update.append(") VALUES(");
    for (int i = Persona.NOME; i <= Persona.MAIL; i++) {
      update.append("'" + p.restituisci(i) + "'");
      if (i < Persona.MAIL) {
        update.append(",");
      }
    }
    update.append(");");
    gestoreDBAgenda.eseguiUpdateSql(update.toString());
    return gestoreDBAgenda.restituisciRisultatoUltimoUpdate();
  }

  // Cancellazione di una persona (per nome e cognome)
  public boolean elimina( Persona p ) {
    String update = "DELETE FROM " + tabellaRecapiti + " WHERE " +
                    Persona.NOMI_CAMPI[Persona.NOME] + "=" +
                    "'" + p.restituisci(Persona.NOME) + "'" + " AND " +
                    Persona.NOMI_CAMPI[Persona.COGNOME] + "=" +
                    "'" + p.restituisci(Persona.COGNOME) + "'" + ";";
    gestoreDBAgenda.eseguiUpdateSql(update);
    return gestoreDBAgenda.restituisciRisultatoUltimoUpdate();
  }

  // Modifica dei dati di una persona (ricerca per nome e cognome)
  public boolean modifica( Persona p, int campo, String nuovoValore ) {
    String update = "UPDATE " + tabellaRecapiti +
                    " SET " + Persona.NOMI_CAMPI[campo] + "=" +
                    "'" + nuovoValore + "'" + " WHERE " +
                    Persona.NOMI_CAMPI[Persona.NOME] + "=" +
                    "'" + p.restituisci(Persona.NOME) + "'" + " AND " +
                    Persona.NOMI_CAMPI[Persona.COGNOME] + "=" +
                    "'" + p.restituisci(Persona.COGNOME) + "'" + ";";
    gestoreDBAgenda.eseguiUpdateSql(update);
    return gestoreDBAgenda.restituisciRisultatoUltimoUpdate();
  }

  // Ricerca una persona (per via)
  public Persona ricercaNominativoPer(String indirizzo, String cap) {
    String query = "SELECT * FROM " + tabellaRecapiti + " WHERE " +
                   Persona.NOMI_CAMPI[Persona.INDIRIZZO] + "=" +
                   "'" + indirizzo + "'" + " AND " +
                   Persona.NOMI_CAMPI[Persona.CAP] + "=" +
                   "'" + cap + "'" + ";";
    gestoreDBAgenda.eseguiQuerySql(query);
    if (gestoreDBAgenda.avanzaAlProssimoRecord()) {
      String[] temp = new String[Persona.NUMERO_CAMPI];
      for (int i = Persona.INDIRIZZO; i <= Persona.MAIL; i++) {
        temp[i] = (String)gestoreDBAgenda.restituisciOggetto(i);
      }
      Persona p = new Persona(temp);
      return p;
    } else {
      return null;
    }
  }

  // Restituisce un elenco di tutti i nominativi inseriti in agenda
  public String restituisciTuttiNominativi() {
    String query = "SELECT * FROM " + tabellaRecapiti + " ORDER BY " +
                   Persona.NOMI_CAMPI[Persona.COGNOME] + ";";
    gestoreDBAgenda.eseguiQuerySql(query);
    return gestoreDBAgenda.restituisciRisultatoComeTesto();
  }


}







GestioneAgenda.java
Created with JBuilder