Advertisement
Guest User

Untitled

a guest
Apr 11th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.77 KB | None | 0 0
  1. <?php
  2. abstract class user {
  3.   private $fails = 0;
  4.   protected $password;
  5.   protected $username;
  6.  
  7.   abstract public function setPassword($password);
  8.   abstract public function setUsername($username);
  9.   abstract protected function checkLogin($username, $password);
  10.   final public function login($username, $password){
  11.     if($this->fails < 3){
  12.       if(!$this->checkLogin($username, $password)){
  13.         $this->fails++;
  14.       } else {
  15.         $this->fails = 0;
  16.         echo "Acc logged in\n";
  17.       }
  18.     } else {
  19.       echo "Acc blocked\n";
  20.     }
  21.   }
  22. }
  23.  
  24. class admin extends user {
  25.   public function setPassword($password){
  26.     if(strlen($password)>=10){
  27.       echo "Pass: ok\n";
  28.     } else {
  29.       echo "Pass: not ok\n";
  30.     }
  31.     $this->password = $password;
  32.   }
  33.   public function setUsername($username){$this->username = $username;}
  34.   protected function checkLogin($username, $password){
  35.     return ($this->username == $username && $this->password == $password);
  36.   }
  37. }
  38.  
  39. class client extends user {
  40.   public function setPassword($password){
  41.     if(strlen($password)>=8){
  42.       echo "Pass: ok\n";
  43.     } else {
  44.       echo "Pass: not ok\n";
  45.     }
  46.     $this->password = $password;
  47.   }
  48.   public function setUsername($username){$this->username = $username;}
  49.  
  50.   public function getUsername(){return $this->username;}
  51.   public function getPassword(){return $this->password;}
  52.  
  53.   protected function checkLogin($username, $password){
  54.     return ($this->username == $username && $this->password == $password);
  55.   }
  56. }
  57.  
  58. class UserSet implements Iterator{
  59.   private $position = 0;
  60.   private $userSet;
  61.  
  62.   public function __construct() {
  63.       $this->position = 0;
  64.   }
  65.  
  66.   public function rewind() {
  67.       $this->position = 0;
  68.   }
  69.  
  70.   public function current() {
  71.       return $this->userSet[$this->position];
  72.   }
  73.  
  74.   public function key() {
  75.       return $this->position;
  76.   }
  77.  
  78.   public function next() {
  79.       ++$this->position;
  80.   }
  81.  
  82.   public function valid() {
  83.       return isset($this->userSet[$this->position]);
  84.   }
  85.  
  86.   public function add(Client $obj){
  87.     $this->userSet[] = $obj;
  88.   }
  89. }
  90.  
  91. function checkLogin($password, UserSet $set){
  92.   foreach($set as $obj){
  93.     if($obj->getPassword() == $password){
  94.       echo $obj->getUsername() . "\n";
  95.     }
  96.   }
  97. }
  98.  
  99. $obj1 = new client();
  100.  
  101. $obj1->setUsername('Admin');
  102. $obj1->setPassword('1234');
  103.  
  104. $obj2 = new client();
  105.  
  106. $obj2->setUsername('Client1');
  107. $obj2->setPassword('1234');
  108.  
  109. $obj3 = new client();
  110.  
  111. $obj3->setUsername('Client2');
  112. $obj3->setPassword('12345');
  113.  
  114. $obj4 = new client();
  115.  
  116. $obj4->setUsername('Client3');
  117. $obj4->setPassword('12345');
  118.  
  119. $objSet =  new UserSet();
  120.  
  121. $objSet->add($obj1);
  122. $objSet->add($obj2);
  123. $objSet->add($obj3);
  124. $objSet->add($obj4);
  125.  
  126. checkLogin('12345', $objSet);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement