Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package principal;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class Election {
- private Connection db;
- private int barre = 5; // barre des 5%
- /* création objet Election, connexion à la base */
- private PreparedStatement nbSuffrages;
- private PreparedStatement nbSuffragesUtiles;
- private PreparedStatement updateNbSiegesPremier;
- private PreparedStatement listesUtiles;
- public Election(String user, String password) throws SQLException {
- this.db = DriverManager.getConnection("jdbc:oracle:thin:@oracle.fil.univ-lille1.fr:1521:filora", user,
- password);
- nbSuffrages = db.prepareStatement("SELECT SUM(nbVoix) FROM ELECTION");
- nbSuffragesUtiles = db.prepareStatement("SELECT SUM(nbVoix) FROM ELECTION WHERE nbVoix > ?");
- updateNbSiegesPremier = db.prepareStatement("UPDATE ELECTION SET nbSieges = ? WHERE liste = ?");
- listesUtiles = db.prepareStatement("SELECT * FROM ELECTION");
- }
- /* fin de connexion */
- public void fin() {
- try {
- db.close();
- } catch (SQLException e) {
- System.err.println("pb déconnexion : " + e.getMessage());
- }
- }
- /*
- * calcul de la répartition des sièges, met à jour la colonne "nbSieges" de
- * la table "Election"
- */
- public void calculSieges(int nbSiegesAPourvoir) throws SQLException {
- int nbSuffrages = this.getNbSuffrages();
- int seuil = (nbSuffrages * this.barre) / 100;
- int nbSuffragesUtiles = this.getNbSuffragesUtiles(seuil);
- int quotientElectoral = nbSuffragesUtiles / nbSiegesAPourvoir;
- this.repartitionSieges(seuil, quotientElectoral);
- this.repartitionResteSieges(seuil, nbSiegesAPourvoir);
- }
- /* calcul du nombre de suffrages exprimés */
- private int getNbSuffrages() throws SQLException {
- int total = -1;
- ResultSet rs = nbSuffrages.executeQuery();
- while(rs.next()){
- total = rs.getInt(1);
- }
- return total;
- }
- /* calcul du nombre de suffrages utiles */
- private int getNbSuffragesUtiles(int seuil) throws SQLException {
- int total = -1;
- int nbTotalSuffrages = getNbSuffrages();
- nbTotalSuffrages = nbTotalSuffrages*(seuil/100);
- nbSuffragesUtiles.setInt(1, nbTotalSuffrages);
- ResultSet rs = nbSuffragesUtiles.executeQuery();
- while(rs.next()){
- total = rs.getInt(1);
- }
- return total;
- }
- /* première répartition des sièges en fonction du quotient électoral */
- private void repartitionSieges(int seuil, int quotientElectoral) throws SQLException {
- ResultSet rs1 = listesUtiles.executeQuery();
- while(rs1.next()){
- String tmp = rs1.getString(1);
- int nbsiegestmp = rs1.getInt(2)/quotientElectoral;
- updateNbSiegesPremier.setInt(1,nbsiegestmp);
- updateNbSiegesPremier.setString(2, tmp);
- ResultSet rs2 = updateNbSiegesPremier.executeQuery();
- }
- }
- /* répartition finale des sièges encore à pourvoir */
- private void repartitionResteSieges(int seuil, int nbSiegesAPourvoir) throws SQLException {
- }
- public static void main(String args[]) {
- Election election = null;
- try {
- election = new Election("declerck", "tmpbdd"); // login et mot
- // de passe
- election.calculSieges(6); // 6 sièges sont à pourvoir
- } catch (SQLException e) {
- // on traite l’exception
- } finally {
- if (election != null)
- election.fin();
- } // quoiqu’il arrive on se déconnecte
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement