-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbank.php
More file actions
384 lines (218 loc) · 7.94 KB
/
Copy pathbank.php
File metadata and controls
384 lines (218 loc) · 7.94 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
interface BankAccountInterface
{
public function deposite($amount): float;
public function withdrow($amount): float;
public function getbalance(): float;
}
abstract class BankAccount implements BankAccountInterface
{
public $account_ID;
public $customer_name;
public $balance;
public function __construct($account_ID, $customer_name, $balance)
{
$this->account_ID = $account_ID;
$this->customer_name = $customer_name;
$this->balance = $balance;
}
public function deposite($amount): float
{
$this->balance = $this->balance + $amount;
return $amount;
}
public function getbalance(): float
{
return $this->balance;
}
abstract public function withdrow($amount): float;
static public function logTransection($transction_type, $account_id, $amount)
{
echo "this is the type of tis transction {" . $transction_type . "} and this is the account id {" . $account_id . "} and this is the amount {" . $amount . "} and this is the time of transection {" . time() . "} please confirme \n";
}
public function perfromTransctions(BankAccountInterface $banktransction, $transction_type, $amount)
{
if ($transction_type == "withdrow") {
$withdorw_amount = $banktransction->withdrow($amount);
echo $withdorw_amount;
}
if ("deposite" == $transction_type) {
$deposite_amount = $banktransction->deposite($amount);
echo $deposite_amount;
}
}
}
class SavingsAccount extends BankAccount
{
public function withdrow($amount): float
{
if ($this->balance > 0) {
if (($this->balance - $amount) > 50) {
echo "this is the amout u want to withdorw :" . $amount;
$this->balance = $this->balance - $amount;
}
} else
echo "the balance is insufficient." . "\n";
parent::logTransection($this->customer_name, $this->account_ID, $amount);
return $amount;
}
}
class CheckingAccount extends BankAccount
{
public function withdrow($amount): float
{
//https://www.php.net/manual/en/function.mt-rand.php
//Returns the maximum random value returned by a call to mt_rand() without arguments,
// which is the maximum value that can be used for its max parameter
// without the result being scaled up (and therefore less random).
#this method will will generate random number between 0 and 1;
$randomnum = mt_rand() / mt_getrandmax();
echo "this is the fee charged each time u make withdrow" . $this->balance = $this->balance * $randomnum;
$this->balance = $this->balance - $amount;
return $amount;
}
}
class AccountManager
{
public function transferTotransfer(BankAccount $bankAccount1, BankAccount $bankAccount2, $amount)
{
echo " will transfer money from " . $bankAccount1->account_ID . " into " . $bankAccount2->account_ID;
if ($bankAccount1->balance == 0) {
echo "you couldn't transfer money there is no money in thiss account .....pls check your balance " . $bankAccount1->balance;
} else {
echo "\n" . "trasfering from account1 into account2.....";
echo "\n";
BankAccount::logTransection("transfering", $bankAccount1->account_ID, $amount);
BankAccount::logTransection("reciving", $bankAccount2->account_ID, $amount);
$withdrow_amount = $bankAccount1->withdrow($amount);
echo $withdrow_amount;
$deposit_amount = $bankAccount2->deposite($amount);
echo $deposit_amount;
}
}
}
interface PaymentMethodInterface
{
public function processPayment(float $amount): void;// using the type declartion and define the returned value (PRO-level)
}
class VisaPayment implements PaymentMethodInterface
{
public $card_number;
public $CVV;
public function __construct($card_number, $CVV)
{
$this->card_number = $card_number;
$this->CVV = $CVV;
}
public function test_input_visa($input_CVV, $card_number): array
{
$input = trim($input_CVV);
$card_number = trim($card_number);
$input = stripslashes($input);
if (is_numeric($card_number)) {
return [$input, $card_number];
} else {
echo "this is unvalid card number\n";
return [$input, NUll];
}
}
public function processPayment(float $amount): void
{
echo "this is cash payment.......and this the amount " . $amount . "\n";
}
public function visa_validation($card_number, $CVV)
{
//first of all check if the same data are re-entered
if ($this->card_number == $card_number and $this->CVV == $CVV) {
$data = $this->test_input_visa($card_number, $CVV);
echo "your information is confermed.....\n";
} else
echo "you are entered wrong info pls try again.....\n";
}
public function make_payment($amount, $card_number, $CVV)
{
$this->visa_validation($card_number, $CVV);
$this->processPayment($amount);
}
}
class PayPalPayment implements PaymentMethodInterface
{
// Include properties for PayPal email.
// processPayment() should simulate PayPal account validation and payment.
public $paypalEmail;
public $password;
public function processPayment(float $amount): void
{
echo "this is paypal payment.......and this the amount " . $amount . "\n";
}
function test_input_paypal($paypalEmail, $password): array
{
$password = trim($password);
$password = stripslashes($password);
$paypalEmail = trim($paypalEmail);
$paypalEmail = stripslashes($paypalEmail);
return [$password, $paypalEmail];
}
public function paypalValidation($paypalEmail, $password)
{
//check if the entered is equal into the orgin.
if ($this->paypalEmail == $paypalEmail and $this->password == $password) {
$data = $this->test_input_paypal($paypalEmail, $password);
echo "your information entered confermed..............\n";
} else
echo "you entered wrong information pls try agian.......\n";
}
public function make_payment($amount, $paypalEmail, $password)
{
$this->paypalValidation($paypalEmail, $password);
$this->processPayment($amount);
}
}
class CashPayment implements PaymentMethodInterface
{
public function processPayment($amount): void
{
echo "this is Cash payment.......and this the amount " . $amount . "\n";
}
public function make_payment($amount)
{
$this->processPayment($amount);
}
}
class PaymentProcessor
{
public $payment_type;
// dependency injection
public function __construct(PaymentMethodInterface $payment_type)
{
$this->payment_type = $payment_type;
}
public function executePayment(PaymentMethodInterface $paymentmethod, $amount)
{
$paymentmethod->processPayment($amount);
}
}
//Payment Factory
class PaymentFactory
{
static public function getPaymentMethod($payment_type, $card_number, $CVV): PaymentMethodInterface
{
if ($payment_type == "visa") {
return new VisaPayment($card_number, $CVV);
}
if ($payment_type == "paypal") {
return new PayPalPayment();
}
if ($payment_type == "cash") {
return new CashPayment();
} else
return Null;
}
}
$account1 = new SavingsAccount(account_ID: "123", customer_name: "aliali", balance: 10000);
$account2 = new SavingsAccount(account_ID: "124", customer_name: "mohammed", balance: 1000);
$accountmanger = new AccountManager();
$accountmanger->transferTotransfer($account1, $account2, 3000);
echo "\n\n" . $account1->balance;
echo "\n\n" . $account2->balance;
?>