Commit 811e179a authored by Lefebvre michael's avatar Lefebvre michael

retrait ok

parent cc4e0a2a
......@@ -7,11 +7,13 @@ import java.util.*;
public interface IPicsouDAO {
//gestion des comptes
public List<CompteDTO> findCompteByClient(int idClient);
public CompteDTO findCompteById(int idCompte) throws ExceptionMetier;
List<CompteDTO> findCompteByClient(int idClient);
CompteDTO findCompteById(int idCompte) throws ExceptionMetier;
//exception du pauvre : renvoyer un status
public void credit(int idCompte, double montant) throws ExceptionMetier;
public void debit(int idCompte, double montant) throws ExceptionMetier;
public void transfert(int idCompteOrigine, int idCompteDestination, double montant) throws ExceptionMetier;
void credit(int idCompte, double montant) throws ExceptionMetier;
void debit(int idCompte, double montant) throws ExceptionMetier;
void transfert(int idCompteOrigine, int idCompteDestination, double montant) throws ExceptionMetier;
void retrait(int iCarte, String code, double montant, String adr) throws ExceptionMetier;
}
......@@ -4,10 +4,8 @@ import dto.CompteDTO;
import exception.ExceptionMetier;
import service.ConnectPicsou;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.security.spec.RSAOtherPrimeInfo;
import java.sql.*;
import java.util.*;
public class MetierPicsou implements IPicsouDAO{
......@@ -50,7 +48,190 @@ public class MetierPicsou implements IPicsouDAO{
" SET solde = solde - ? " +
"WHERE id = ? ";
private final static String SQLSommeDixJours =
" SELECT COUNT(*) as cpt, sum(montant) as somme " +
" from RETRAIT " +
" where (CURRENT_DATE - 10) <= date_retrait " +
" and id_carte = ? " +
" and etat = 'succes' ";
private final static String SQLUpdateNbEssai =
" Update CARTE " +
" set nb_essai = ? " +
" WHERE id = ? " ;
private final static String SQLInsertRetrait =
" INSERT INTO RETRAIT(id_carte, date_retrait, adresseGAB, montant, etat, id_mouvement) " +
" VALUES( ?, current_timestamp, ?, ?, 'succes', ? )";
private final static String SQLRetraitAnomalie =
" INSERT INTO RETRAIT(id_carte, date_retrait, adresseGAB, montant, etat, id_mouvement) " +
" VALUES( ?, current_timestamp, ?, ?, ?, null )" ;
private final static String SQLfindCarteById =
"SELECT * " + " FROM CARTE " + " WHERE id = ? ";
//-------------------------------------- F I N S Q L -------------------------------------------
@Override
public void retrait(int idCarte, String code, double montant, String adr) throws ExceptionMetier {
if (montant <= 0) {
throw new ExceptionMetier("Retrait : montant négatif");
}
Connection conn = ConnectPicsou.getInstance();
try {
conn.setAutoCommit((false));
//enregistrement des preparestatements
PreparedStatement pstmtCarteById = conn.prepareStatement(SQLfindCarteById);
pstmtCarteById.setInt(1, idCarte);
PreparedStatement pstmtAnomalie = conn.prepareStatement(SQLRetraitAnomalie);
PreparedStatement pstmtNbEssai = conn.prepareStatement(SQLUpdateNbEssai);
PreparedStatement pstmtDixJours = conn.prepareStatement(SQLSommeDixJours);
PreparedStatement pstmtCompte = conn.prepareStatement(findCompteById);
PreparedStatement pstmtInsertMvt = conn.prepareStatement(SQLInsertMvt, Statement.RETURN_GENERATED_KEYS);
PreparedStatement pstmtInsertRetrait = conn.prepareStatement(SQLInsertRetrait);
PreparedStatement pstmtUpdateCompte = conn.prepareStatement(SQLUpdateDebitCompte);
//controle de la cb (inconnu? , bloquée, ...)
ResultSet rs = pstmtCarteById.executeQuery();
if (!rs.next()) {
throw new ExceptionMetier("Retrait : carte inconnue");
}
//carte bloque?
String bloque = rs.getString("bloque");
//equalsIgnoreCase verifie sans tenir copte des maj et min
if (bloque.equalsIgnoreCase("oui")) {
pstmtAnomalie.setInt(1, idCarte);
pstmtAnomalie.setString(1, adr);
pstmtAnomalie.setDouble(3, montant);
pstmtAnomalie.setString(4, "CBbloquee");
pstmtAnomalie.executeUpdate();
conn.commit();
throw new ExceptionMetier("Retrait : carte bloquée");
}
//controle code
int nbEssai = rs.getInt("nb_essai");
String carteCode = rs.getString("codesecret");
if (!carteCode.equalsIgnoreCase(code)) {
pstmtAnomalie.setInt(1, idCarte);
pstmtAnomalie.setString(1, adr);
pstmtAnomalie.setDouble(3, montant);
pstmtAnomalie.setString(4, "ERRcode");
pstmtAnomalie.executeUpdate();
nbEssai = nbEssai + 1;
pstmtNbEssai.setInt(1, nbEssai);
pstmtNbEssai.setInt(2, idCarte);
pstmtNbEssai.executeUpdate();
conn.commit();
throw new ExceptionMetier("Retrait : code erroné");
}
pstmtNbEssai.setInt(1, 0);
pstmtNbEssai.setInt(2, idCarte);
pstmtNbEssai.executeUpdate();
//controle somme des dix dernier jours
pstmtDixJours.setInt(1, idCarte);
int idCompte = rs.getInt("id_compte");
ResultSet rs1 = pstmtDixJours.executeQuery();
rs1.next();
int cpt = rs1.getInt("cpt");
double somme = (cpt == 0 ? 0.0 : rs1.getDouble("somme"));
if (somme >= 500.0) {
pstmtAnomalie.setInt(1, idCarte);
pstmtAnomalie.setString(1, adr);
pstmtAnomalie.setDouble(3, montant);
pstmtAnomalie.setString(4, "10jours");
pstmtAnomalie.executeUpdate();
conn.commit();
throw new ExceptionMetier("Retrait : 10 jours glissants");
}
pstmtCompte.setInt(1, idCompte);
ResultSet rs2 = pstmtCompte.executeQuery();
if (!rs2.next()) {
throw new ExceptionMetier("Retrait id compte inconnu");
} else {
Double plafond = rs2.getDouble("plafond");
Double solde = rs2.getDouble("solde");
if (solde + plafond < montant) {
pstmtAnomalie.setInt(1, idCarte);
pstmtAnomalie.setString(1, adr);
pstmtAnomalie.setDouble(3, montant);
pstmtAnomalie.setString(4, "Montant");
pstmtAnomalie.executeUpdate();
conn.commit();
throw new ExceptionMetier("Retrait : solde insuffisant");
}
//ajout mouvement
pstmtInsertMvt.setInt(1, idCompte);
pstmtInsertMvt.setDouble(2, montant);
pstmtInsertMvt.setString(3, "CB");
pstmtInsertMvt.executeUpdate();
//recuperation de la cle primaire
ResultSet rsInsetMvt = pstmtInsertMvt.getGeneratedKeys();
rsInsetMvt.next();
int key = rsInsetMvt.getInt(1);
//ajout retrait
pstmtInsertRetrait.setInt(1, idCarte);
pstmtInsertRetrait.setString(2, adr);
pstmtInsertRetrait.setDouble(3, montant);
pstmtInsertRetrait.setInt(4, key);
pstmtInsertRetrait.executeUpdate();
//mise a jour compte : solde
pstmtUpdateCompte.setInt(2, idCompte);
pstmtUpdateCompte.setDouble(1, montant);
pstmtUpdateCompte.executeUpdate();
conn.commit();
}
} catch (SQLException ex) {
ex.printStackTrace();
try {
conn.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override
public List<CompteDTO> findCompteByClient(int idClient) {
List<CompteDTO> liste = new ArrayList<>();
......@@ -280,10 +461,8 @@ public class MetierPicsou implements IPicsouDAO{
public static void main(String[] args) {
IPicsouDAO mp = MetierPicsou.getDao();
try {
System.out.println(dao.findCompteById(2));
System.out.println(dao.findCompteById(3));
dao.transfert(2,3,200);
System.out.println(dao.findCompteById(2));
dao.retrait(2,"5678",50.0, "lens");
System.out.println(dao.findCompteById(3));
}catch (Exception e){
System.err.println(e.getMessage());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment