-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.cpp
299 lines (277 loc) · 7.24 KB
/
Client.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;
class String
{
public:
String(const char* s="");
String(const String& s);
String& operator=(const String& s);
int operator==(const String& s);
const char& operator[](int i){return chaine[i];}
~String(){delete chaine;}
friend ostream& operator<<(ostream& out,const String& s);
private:
char* chaine;
};
//*********** constructeur par défaut ************
String::String(const char* s){
chaine=new char[strlen(s)+1];
assert(chaine!=0);
strcpy(chaine,s);
}
//*********** constructeur ppar recopie ************
String::String(const String& s){
chaine=new char[strlen(s.chaine)+1];
assert(chaine!=0);
strcpy(chaine,s.chaine);
}
//*************** operator= ********************
String& String::operator=(const String& s){
if(this == &s) return *this;
delete chaine;
chaine=new char[strlen(s.chaine)+1];
assert(chaine!=0);
strcpy(chaine,s.chaine);
return *this;
}
//*************** operator== *******************
int String::operator==(const String& s){
if(this == &s) return 1;
return !strcmp(chaine,s.chaine);
}
//*************** operator<< *******************
ostream& operator<<(ostream& out,const String& s){
out<<s.chaine;
return out;
}
const float COMMISION_VIREMENT=30;
const float TAUX_EPARGNE=0.025;
const float MIN_MONTANT_OUVERTURE=100;
//-------------------------------------
//-------------Compte------------------
//-------------------------------------
class Compte{
protected:
int numero;
double solde;
static int numeroComptes;
public:
Compte(){
numeroComptes++;
numero=numeroComptes;
}
int getNumero(){return numero;}
virtual void deposer(double montant)=0;
virtual bool virer(double montant,Compte& destinataire)
{return 0;}
bool retirer(double montant);
double consulterSolde();
virtual void afficher(ostream&);
friend ostream& operator<<(ostream& out,Compte& compte);
};
int Compte::numeroComptes=0;
//************** retirer ****************
bool Compte::retirer(double montant){
if(montant<solde){
solde-=montant;
return 1;
}
else
return 0;
}
//************* consulterSolde ****************
double Compte::consulterSolde(){
return solde;
}
//************* afficher ****************
void Compte::afficher(ostream& out)
{
cout<<"\tNumero: ";
out<<numero;
cout<<"\tSolde: ";
out<<solde;
}
//*************** operator<< *******************
ostream& operator<<(ostream& out,Compte& compte)
{
compte.afficher(out);
return out;
}
//-------------------------------------
//-------------CompteCourant-----------
//-------------------------------------
class CompteCourant:public Compte{
public:
CompteCourant(double s=MIN_MONTANT_OUVERTURE):
Compte(){solde=s;}
void deposer(double montant);
bool virer(double montant,Compte& destinataire);
void afficher(ostream&);
friend ostream& operator<<(ostream&,CompteCourant&);
};
//*************** deposer *******************
void CompteCourant::deposer(double montant){
solde+=montant;
}
//*************** virer *******************
bool CompteCourant::virer(double montant,Compte& destinataire){
if(montant+COMMISION_VIREMENT>solde)return 0;
solde-=montant+COMMISION_VIREMENT;
destinataire.deposer(montant);
return 1;
}
//************* afficher ****************
void CompteCourant::afficher(ostream& out)
{
cout<<"Compte courant";
Compte::afficher(out);
cout<<endl;
}
//*************** operator<< *******************
ostream& operator<<(ostream& out,CompteCourant& compte)
{
compte.afficher(out);
return out;
}
//-------------------------------------
//-------------CompteEparne-----------
//-------------------------------------
class CompteEpargne:public Compte{
private:
float taux;
public:
CompteEpargne(double s=MIN_MONTANT_OUVERTURE,float t=TAUX_EPARGNE):
Compte(){solde=s;taux=t;}
void deposer(double montant);
void afficher(ostream&);
friend ostream& operator<<(ostream&,CompteEpargne&);
};
//*************** deposer *******************
void CompteEpargne::deposer(double montant){
solde+=montant+montant*taux;
}
//************* afficher ****************
void CompteEpargne::afficher(ostream& out)
{
cout<<"Compte epargne" ;
Compte::afficher(out);
cout<<"\tTaux: ";
out<<taux;
cout<<endl;
}
//*************** operator<< *******************
ostream& operator<<(ostream& out,CompteEpargne& compte)
{
compte.afficher(out);
return out;
}
//-------------------------------------
//------------- Client ----------------
//-------------------------------------
class Noeud{
Compte* info;
// Noeud* suivant;
public:
Noeud* suivant;
Noeud(Compte& c, Noeud*s)
{info = &c; suivant = s;}
Compte& Info(){return *info;}
Noeud* Suivant(){return suivant;}
};
class Client{
String nom;
String prenom;
Noeud* comptes;
Noeud* dernier;
int nbComptes;
public:
Client(String n="",String p="")
{nom=n;
prenom=p;
nbComptes=0;
comptes=0;
dernier=0;
}
~Client(){
Noeud *ptr=comptes;
while (ptr != 0){
Noeud* temp=ptr;
ptr = ptr->suivant;
delete temp;
}
}
String Nom();
String Prenom();
Compte& getCompte(int numero);
void setCompte(Compte& compte);
int getNbCompte();
friend ostream& operator<<(ostream& out,const Client& client);
};
//*************** getNom *******************
String Client::Nom(){
return nom;
}
//*************** getPrenom ****************
String Client::Prenom(){
return prenom;
}
//*************** setCompte *****************
void Client::setCompte(Compte& compte){
Noeud* ptr;
ptr=new Noeud(compte,0);
if(comptes==0)
comptes = ptr;
else
dernier->suivant=ptr;
dernier=ptr;
nbComptes++;
}
//*************** getCompte *****************
Compte& Client::getCompte(int numero){
assert(numero>=0&&numero<nbComptes);
Noeud* ptr=comptes;
for(int i=0;i<nbComptes;i++,ptr=ptr->Suivant())
if(ptr->Info().getNumero()==numero)return ptr->Info();
}
//*************** getNbCompte *****************
int Client::getNbCompte(){
return nbComptes;
}
//************* operator<< ****************
ostream& operator<<(ostream& out,const Client& client)
{
cout<<"Le client: ";
out<<client.nom;
cout<<"\t";
out<<client.prenom;
cout<<endl;
int nb=client.nbComptes;
Noeud* ptr=client.comptes;
for(int i=0;i<nb;i++)
{
out<<ptr->Info();
ptr=ptr->Suivant();
}
return out;
}
/*************************************
Test de la classe Client
*************************************/
int main()
{
Client client1("SOUHAR","Abdelghani");
CompteCourant compteCourant1;
CompteCourant compteCourant2(1000.00);
CompteEpargne compteEpargne1;
CompteEpargne compteEpargne2(900.00,3.5);
client1.setCompte(compteCourant1);
client1.setCompte(compteCourant2);
client1.setCompte(compteEpargne1);
client1.setCompte(compteEpargne2);
cout<<client1;
cout<<endl;
cout<<client1.getCompte(3);
return 0;
}