ServiceBanqueBean.java
3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package ejb.sessions;
import java.util.Collection;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import ejb.entites.Action;
import ejb.entites.Compte;
import ejb.entites.LigneAction;
@javax.ejb.Stateless
public class ServiceBanqueBean
implements ServiceBanqueLocal, ServiceBanqueRemote {
@PersistenceContext(unitName="appliBanque") protected EntityManager em ;
@Override
public void addCompte(int numeroCompte, String nomTitulaire, double soldeDepart)
throws CompteDejaExistantException {
try {
this.getCompte(numeroCompte) ;
throw new CompteDejaExistantException() ;
} catch(CompteInconnuException e) {
Compte c=new Compte() ;
c.setNumero(numeroCompte); c.setSolde(soldeDepart);
c.setTitulaire(nomTitulaire);
em.persist(c);
}
}
@Override
public void addAction(String nomAction, double taux)
throws ActionDejaExistanteException {
try {
this.getAction(nomAction) ;
throw new ActionDejaExistanteException() ;
} catch(ActionInconnueException e) {
Action a=new Action() ;
a.setNom(nomAction); a.setTaux(taux);
em.persist(a);
}
}
private Action getAction(String nomAction) throws ActionInconnueException {
Action a= (Action) em.find(Action.class, nomAction) ;
if (a==null) throw new ActionInconnueException() ;
return a ;
}
@Override
public void crediterCompte(int numeroCompte, double montant)
throws CompteInconnuException {
Compte c = this.getCompte(numeroCompte) ;
c.setSolde(c.getSolde()+montant);
}
@Override
public void debiterCompte(int numeroCompte, double montant)
throws CompteInconnuException {
Compte c = this.getCompte(numeroCompte) ;
c.setSolde(c.getSolde()-montant);
}
@Override
public void virementVers(int numCompteDebit, int numCompteCredit,
double montant) throws CompteInconnuException,
ApprovisionnementException {
}
@Override
public void acheteActions(int numeroCompte, String nomAction, int nb)
throws CompteInconnuException, ActionInconnueException,
ApprovisionnementException {
Compte c = this.getCompte(numeroCompte) ;
Action a = this.getAction(nomAction) ;
if (c.getSolde()< nb*a.getTaux())
throw new ApprovisionnementException() ;
else
c.setSolde(c.getSolde()-nb*a.getTaux()) ;
boolean trouve=false ;
for (LigneAction la : c.getLignesactions())
if (la.getAction().getNom().equals(nomAction)) {
trouve=true ; la.setNombre(la.getNombre()+nb);
}
if (!trouve) {
LigneAction la = new LigneAction() ;
la.setAction(a); la.setProprietaire(c) ; la.setNombre(nb);
em.persist(la);
}
}
@Override
public void vendActions(int numeroCompte, String nomAction, int nb)
throws CompteInconnuException, ActionInconnueException,
ApprovisionnementException {
Compte c = this.getCompte(numeroCompte) ;
LigneAction la=null ;
for (LigneAction vla : c.getLignesactions())
if (vla.getAction().getNom().equals(nomAction)) {
la=vla ;
}
if (la != null) {
if (nb > la.getNombre())
throw new ApprovisionnementException() ;
else {
c.setSolde(c.getSolde()+nb*la.getAction().getTaux());
la.setNombre(la.getNombre()-nb) ;
if (la.getNombre()==0) em.remove(la);
}
}
}
@Override
public Set<LigneAction> getActionsAchetees(int numeroCompte)
throws CompteInconnuException {
Compte c = this.getCompte(numeroCompte) ;
return c.getLignesactions() ;
}
@Override public Compte getCompte(int numeroCompte) throws CompteInconnuException {
Compte c= (Compte) em.find(Compte.class, numeroCompte) ;
if (c==null) throw new CompteInconnuException() ;
return c;
}
}