public string CreditTransaction(int idClient, double amount) { String msg = string.Empty; try { Historique newHistorique; Client clientDB = ClientDAO.Read(idClient); if (clientDB == null || clientDB.IsDelete) { throw new Exception ("Authentification impossible: les identifiants sont incorrects ou bien le compte a été supprimé."); } else { Random rand = new Random(); if (rand.Next(0, 101) > 49) { clientDB.DoCredit(amount); newHistorique = new Historique(clientDB, amount, clientDB.Sold); ClientDAO.Update(clientDB); HistoriqueDAO.Insert(newHistorique); msg = "OK"; } else { throw new Exception ( "Transfert refusé par la banque. Le compte de " + clientDB.Name + " " + clientDB.FirstName + " ne peut pas être crédité du montant suivant : " + amount + "€."); } } BDDConnexion.CloseConnection(); } catch (Exception ex) { msg = ex.Message; } return msg; }
private static List<Historique> ExecuteReader(SqlCommand cmd) { SqlDataReader reader; List<Historique> result = new List<Historique>(); try { reader = cmd.ExecuteReader(); while (reader.Read()) { Historique currentHistorique = new Historique(ClientDAO.Read(Convert.ToInt32(reader["ID_CLIENT"])) , Convert.ToDouble(reader["MONTANT_TRANSACTION"]), Convert.ToDouble(reader["SOLDE_APRES_TRANSACTION"]) , DateTime.Parse(reader["DATE_TRANSACTION"].ToString())); result.Add(currentHistorique); } } catch (Exception e) { throw (e); } reader.Close(); reader.Dispose(); reader = null; return result; }
public static void Insert(Historique historique) { SqlCommand cmd = new SqlCommand(); const string query = "INSERT INTO HISTORIQUE VALUES ('{0}','{1}','{2}','{3}')"; cmd.CommandText = string.Format(query, historique.ClientId.ClientId.ToString(), historique.TransactionSum.ToString().Replace(",", ".") , historique.SoldAfterTransaction.ToString().Replace(",", "."), DateTime.Now.ToShortDateString()); cmd.CommandType = CommandType.Text; cmd.Connection = BDDConnexion.Conn; try { cmd.ExecuteNonQuery(); } catch (Exception e) { throw (e); } finally { cmd.Dispose(); cmd = null; } }
public string PurchaseProducts(int idClient, int purchasePrice) { try { if (purchasePrice < 0) throw new Exception("error prix inferieur à 0"); Client currentClient = ClientDAO.Search("ID_Client='" + idClient + "'").FirstOrDefault(); if (currentClient == null || currentClient.IsDelete) throw new Exception("Le client n'existe pas"); if (currentClient.Sold < purchasePrice) throw new Exception("Le client ne possède pas assez d'argent"); currentClient.DoPurchase(purchasePrice); ClientDAO.Update(currentClient); Historique currentHistorique = new Historique(currentClient, purchasePrice, currentClient.Sold); HistoriqueDAO.Insert(currentHistorique); BDDConnexion.CloseConnection(); } catch (Exception ex) { return ex.Message; } return "l'achat a réussi"; }
public string DebitTransaction(int idClient, double amount) { String msg = string.Empty; try { Historique newHistorique; Client clientDB = ClientDAO.Read(idClient); if (clientDB == null || clientDB.IsDelete) { throw new Exception ("Authentification impossible: les identifiants sont incorrects ou bien le compte a été supprimé."); } else { if ((clientDB.Sold - amount) >= 0) { clientDB.DoPurchase(amount); newHistorique = new Historique(clientDB, amount, clientDB.Sold); ClientDAO.Update(clientDB); HistoriqueDAO.Insert(newHistorique); msg = "Le compte de " + clientDB.Name + " " + clientDB.FirstName + " vient d'être débité du montant suivant : " + amount + "€. Solde actuel : " + (clientDB.Sold) + "€"; } else { throw new Exception ("Le compte de " + clientDB.Name + " " + clientDB.FirstName + " ne peut pas être débité du montant suivant : " + amount + "€. Pensez à réapprovisionner votre compte (solde actuel: " + clientDB.Sold + "€)."); } } BDDConnexion.CloseConnection(); } catch (Exception ex) { msg = ex.Message; } return msg; }