Advertisement
HristoBaychev

bankacc

Apr 21st, 2023
1,175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.62 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Създайте клас, наречен "BankAccount" с private атрибути за номер на сметка, баланс и
  5. лихвен процент. Добавете публични методи за депозиране и теглене на пари от сметката
  6. и private метод за изчисляване на лихвата въз основа на текущия баланс и лихвен
  7. процент. Комбинирайте с клас Person и Calculator :)
  8. */
  9.  
  10.  
  11. class Person{
  12.     protected $name;
  13.     protected $age;
  14.     protected $email;
  15.  
  16.  
  17.     public function __construct(string $name, int $age, string $email)
  18.     {
  19.         $this->name = $name;
  20.         $this->age = $age;
  21.         $this->email = $email;
  22.     }
  23.  
  24.     public function setName($name){
  25.         $this->name = $name;
  26.     }
  27.     public function setAge($age){
  28.         $this->age = $age;
  29.     }
  30.     public function setEmail($email){
  31.         $this->email = $email;
  32.     }
  33.  
  34.     public function getName(){
  35.         return $this->name;
  36.     }
  37.     public function getAge(){
  38.         return $this->age;
  39.     }
  40.     public function getEmail(){
  41.         return $this->email;
  42.     }
  43. }  
  44.  
  45.  
  46.  
  47.  
  48. class InfoBankAccount extends Person{
  49.     private $numberAcc;
  50.    
  51.     private $deposit;
  52.     private $balance = 0;
  53.     private $withdrawal;
  54.     private $monthDeposit;
  55.  
  56.     private $interestRate;
  57.     private $interestCalculation;
  58.  
  59.     public function __construct(string $name, int $age, string $email, float $deposit, float $withdrawal, float $interestRate, int $monthDeposit){
  60.         parent::__construct($name, $age, $email);
  61.         $this->deposit = $deposit;
  62.         $this->withdrawal = $withdrawal;
  63.         $this->interestRate = $interestRate;
  64.         $this->monthDeposit = $monthDeposit;
  65.  
  66.  
  67.     }
  68.  
  69.     public function BankAcc(){
  70.         return $this->numberAcc = uniqid();
  71.     }
  72.  
  73.  
  74.     public function setDeposit($deposit){
  75.         $this->deposit = $deposit;
  76.     }
  77.     public function setInterestRate($interestRate){
  78.         $this->interestRate = $interestRate;
  79.     }
  80.     public function setMonthDeposit($monthDeposit){
  81.         $this->monthDeposit = $monthDeposit;
  82.     }
  83.     public function setWithdrawal($withdrawal){
  84.         $this->withdrawal = $withdrawal;
  85.     }
  86.  
  87.  
  88.  
  89.     public function getDeposit(){
  90.         if ($this->deposit <= 0){
  91.             throw new Exception("Your deposit can`t be 0");
  92.         }
  93.         else{
  94.             return $this->deposit;
  95.         }
  96.     }
  97.     public function getInterestRate(){
  98.         return $this->interestRate * 100;
  99.     }
  100.     public function getMonthDeposit(){
  101.         return $this->monthDeposit;
  102.     }
  103.     public function getWithdrawal(){
  104.         return $this->withdrawal;
  105.     }
  106.  
  107.  
  108.  
  109.     public function balance(){
  110.         $this->balance = $this->deposit + $this->balance;
  111.         return $this->balance;
  112.  
  113.     }
  114.  
  115.     public function interestCalculation(){
  116.         $interest = $this->balance * ($this->interestRate / 12) * $this->monthDeposit;
  117.         return $interest;
  118.  
  119.     }
  120.  
  121.     public function calcWithdrawal(){
  122.         if ($this->balance > $this->withdrawal){
  123.         $this->balance = $this->balance - $this->withdrawal;
  124.         return $this->balance;
  125.         }
  126.         else{
  127.             throw new Exception("You don`t have enough money!");
  128.         }
  129.     }
  130.     public function __toString(){
  131.         return "Name: " . $this->name . ", Age: " . $this->age . ", Email: " . $this->email . ", Account number: " . $this->numberAcc . PHP_EOL;
  132.     }
  133.    
  134. }
  135.  
  136. $persons = [
  137.     new InfoBankAccount('Hristo', 35, 'hriston@gmail.com', 1000, 500, 0.02, 6),
  138.     new InfoBankAccount('Petar', 25, 'petar@gmail.com', 100, 500, 0.02, 6),
  139.     new InfoBankAccount('Kircho', 30, 'kircho@gmail.com', 0, 0, 0.02, 6),
  140.     new InfoBankAccount('Rumen', 32, 'rumen@gmail.com', 20000, 10, 0.02, 6)
  141. ];
  142.  
  143. foreach ($persons as $person) {
  144.     echo $person->getName() . ' has account number ' . $person->BankAcc() . ' with a balance of $' . $person->balance() . PHP_EOL;
  145.     echo 'Interest rate: ' . $person->getInterestRate() . '%' . PHP_EOL;
  146.     echo 'Interest earned after 6 months: $' . $person->interestCalculation() . PHP_EOL;
  147.     try {
  148.         echo "Your deposit is: " . $person->getDeposit().PHP_EOL;
  149.     } catch (Exception $e) {
  150.         echo 'Caught exception: ',  $e->getMessage(), "\n";
  151.         continue;
  152.     }
  153.     try {
  154.         echo 'After withdrawing $' . $person->getWithdrawal() . ', the new balance is $' . $person->calcWithdrawal() . PHP_EOL;
  155.     } catch (Exception $e) {
  156.         echo 'Caught exception: ',  $e->getMessage(), "\n";
  157.         continue;
  158.     }
  159. }
  160.  
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement