Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. package principal;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. public class Election {
  10.     private Connection db;
  11.     private int barre = 5; // barre des 5%
  12.     /* création objet Election, connexion à la base */
  13.    
  14.     private PreparedStatement nbSuffrages;
  15.     private PreparedStatement nbSuffragesUtiles;
  16.     private PreparedStatement updateNbSiegesPremier;
  17.     private PreparedStatement listesUtiles;
  18.  
  19.     public Election(String user, String password) throws SQLException {
  20.         this.db = DriverManager.getConnection("jdbc:oracle:thin:@oracle.fil.univ-lille1.fr:1521:filora", user,
  21.                 password);
  22.        
  23.         nbSuffrages = db.prepareStatement("SELECT SUM(nbVoix) FROM ELECTION");
  24.         nbSuffragesUtiles = db.prepareStatement("SELECT SUM(nbVoix) FROM ELECTION WHERE nbVoix > ?");
  25.         updateNbSiegesPremier = db.prepareStatement("UPDATE ELECTION SET nbSieges = ? WHERE liste = ?");
  26.         listesUtiles = db.prepareStatement("SELECT * FROM ELECTION");
  27.  
  28.     }
  29.  
  30.     /* fin de connexion */
  31.     public void fin() {
  32.         try {
  33.             db.close();
  34.         } catch (SQLException e) {
  35.             System.err.println("pb déconnexion : " + e.getMessage());
  36.         }
  37.     }
  38.  
  39.     /*
  40.      * calcul de la répartition des sièges, met à jour la colonne "nbSieges" de
  41.      * la table "Election"
  42.      */
  43.     public void calculSieges(int nbSiegesAPourvoir) throws SQLException {
  44.         int nbSuffrages = this.getNbSuffrages();
  45.         int seuil = (nbSuffrages * this.barre) / 100;
  46.         int nbSuffragesUtiles = this.getNbSuffragesUtiles(seuil);
  47.         int quotientElectoral = nbSuffragesUtiles / nbSiegesAPourvoir;
  48.         this.repartitionSieges(seuil, quotientElectoral);
  49.         this.repartitionResteSieges(seuil, nbSiegesAPourvoir);
  50.     }
  51.  
  52.     /* calcul du nombre de suffrages exprimés */
  53.     private int getNbSuffrages() throws SQLException {
  54.         int total = -1;
  55.         ResultSet rs = nbSuffrages.executeQuery();
  56.         while(rs.next()){
  57.             total = rs.getInt(1);
  58.         }
  59.         return total;
  60.     }
  61.  
  62.     /* calcul du nombre de suffrages utiles */
  63.     private int getNbSuffragesUtiles(int seuil) throws SQLException {
  64.         int total = -1;
  65.         int nbTotalSuffrages = getNbSuffrages();
  66.         nbTotalSuffrages = nbTotalSuffrages*(seuil/100);
  67.         nbSuffragesUtiles.setInt(1, nbTotalSuffrages);
  68.         ResultSet rs = nbSuffragesUtiles.executeQuery();
  69.         while(rs.next()){
  70.             total = rs.getInt(1);
  71.         }
  72.         return total;
  73.     }
  74.  
  75.     /* première répartition des sièges en fonction du quotient électoral */
  76.     private void repartitionSieges(int seuil, int quotientElectoral) throws SQLException {
  77.         ResultSet rs1 = listesUtiles.executeQuery();
  78.         while(rs1.next()){
  79.             String tmp = rs1.getString(1);
  80.             int nbsiegestmp = rs1.getInt(2)/quotientElectoral;
  81.             updateNbSiegesPremier.setInt(1,nbsiegestmp);
  82.             updateNbSiegesPremier.setString(2, tmp);
  83.             ResultSet rs2 = updateNbSiegesPremier.executeQuery();
  84.         }
  85.     }
  86.  
  87.     /* répartition finale des sièges encore à pourvoir */
  88.     private void repartitionResteSieges(int seuil, int nbSiegesAPourvoir) throws SQLException {
  89.        
  90.     }
  91.  
  92.     public static void main(String args[]) {
  93.         Election election = null;
  94.         try {
  95.             election = new Election("declerck", "tmpbdd"); // login et mot
  96.                                                                 // de passe
  97.             election.calculSieges(6); // 6 sièges sont à pourvoir
  98.         } catch (SQLException e) {
  99.             // on traite l’exception
  100.         } finally {
  101.             if (election != null)
  102.                 election.fin();
  103.         } // quoiqu’il arrive on se déconnecte
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement