diff --git a/ATM.php b/ATM.php new file mode 100644 index 0000000..12b3827 --- /dev/null +++ b/ATM.php @@ -0,0 +1,74 @@ +accountRepository = $accountRepository; + $this->cardRepository = $cardRepository; + } + + public function insertCard($cardNumber) + { + if (!empty($this->activeCard)){ + throw new InvalidArgumentException("Card already exists in ATM"); + } + + $this->activeCard = $this->cardRepository->getItem($cardNumber); + } + + public function enterPin($pin) + { + $this->activeCard->activateCard($pin); + if (!$this->activeCard->getActiveCard()){ + throw new LogicException("Card none active"); + }elseif (!$this->activeCard->checkPin($pin)){ + throw new LogicException("Uncorrect pin"); + } + $this->confirmedPin = true; + } + + public function cashIn($cash) + { + if (!$this->activeCard->getActiveCard()){ + throw new LogicException("Card in none active"); + } + else{ + $account = $this->accountRepository->getItem($this->activeCard->getAccountNumber()); + $account->addMoney($this->activeCard, $cash); + } + } + + public function cashOut($cash) + { + if (!$this->activeCard->getActiveCard()){ + throw new LogicException("Card in none active"); + } + else{ + $account = $this->accountRepository->getItem($this->activeCard->getAccountNumber()); + $account->TakeMoney($this->activeCard, $cash); + } + } + + public function balance() + { + if (!$this->activeCard->getActiveCard()){ + throw new LogicException("Card in none active"); + } else { + $account = $this->accountRepository->getItem($this->activeCard->getAccountNumber()); + return $account->getAmount($this->activeCard); + } + } + + public function finish() + { + $this->activeCard->deactivateCard(); + $this->activeCard = null; + } + +} \ No newline at end of file diff --git a/Account.php b/Account.php new file mode 100644 index 0000000..e08b03f --- /dev/null +++ b/Account.php @@ -0,0 +1,41 @@ +accountNumber = $accountNumber; + $this->amount = $amount; + } + + public function getAccountNumber() + { + return $this->accountNumber; + } + + public function getAmount($card) + { + if ($card->getActiveCard()) + return $this->amount; + } + + public function addMoney($card, $cash) + { + if ($card->getActiveCard()) + $this->amount += $cash; + else + throw new LogicException("Card in none active"); + } + + public function takeMoney($card, $cash) + { + if ($card->getActiveCard()) + $this->amount -= $cash; + else + throw new LogicException("Card in none active"); + } + +} \ No newline at end of file diff --git a/Card.php b/Card.php new file mode 100644 index 0000000..b8aa58b --- /dev/null +++ b/Card.php @@ -0,0 +1,54 @@ +cardNumber = $cardNumber; + $this->hashPin = hash('ripemd160', $pin); + $this->accountNumber = $accountNumber; + $this->cardIsActive = false; + } + + + public function getCardNumber() + { + return $this->cardNumber; + } + + public function getAccountNumber() + { + return $this->accountNumber; + } + + public function checkPin($pin) + { + $givenPin = hash('ripemd160', $pin); + return ($givenPin == $this->hashPin); + } + + public function activateCard($pin) + { + if ($this->checkPin($pin)){ + $this->cardIsActive = true; + } + else{ + throw new InvalidArgumentException("Invalid pin"); + } + } + + public function deactivateCard() + { + $this->cardIsActive = false; + } + + public function getActiveCard() + { + return $this->cardIsActive; + } + +} \ No newline at end of file diff --git a/Repository/AccountRepository.php b/Repository/AccountRepository.php new file mode 100644 index 0000000..0f37928 --- /dev/null +++ b/Repository/AccountRepository.php @@ -0,0 +1,18 @@ +accounts[$account->getAccountNumber()] = $account; + } + + public function getItem($accountNumber) + { + return $this->accounts[$accountNumber]; + } +} \ No newline at end of file diff --git a/Repository/CardRepository.php b/Repository/CardRepository.php new file mode 100644 index 0000000..65c6deb --- /dev/null +++ b/Repository/CardRepository.php @@ -0,0 +1,27 @@ +accountRepository = $accountRepository; + } + + public function add($card) + { + $this->cards[$card->getCardNumber()] = $card; + } + + public function getItem($key) + { + if (!array_key_exists($key, $this->cards)){ + throw new InvalidArgumentException($message="Invalid key"); + } + return $this->cards[$key]; + } +} \ No newline at end of file diff --git a/Repository/Repository.php b/Repository/Repository.php new file mode 100644 index 0000000..d63a4ba --- /dev/null +++ b/Repository/Repository.php @@ -0,0 +1,7 @@ +add(new Account($accountNumber, $amount)); + +// Номер карты +$cardNumber = "4444-5555-5555-5555"; + +// Пинкод +$pin = "1234"; + +// Добавление карты +$cardRepository->add(new Card($cardNumber, $pin, $accountNumber)); + + +//Создание класса банкомата +$atm = new ATM($accountRepository, $cardRepository); + +//Вставляем карту в приемник +$atm->insertCard($cardNumber); + +//Указываем пинкод, если пинкод не подходит, выдаем ошибку +$atm->enterPin($pin); + +//Получение средств, +// Если средств недостаточно, ошибка +// Если карта не вставлена или не указан пинкод, то также ошибка + +$atm->cashOut(1000); +//Снятие средств, ошибки аналогично если карта не вставлена или пинкод не прошел проверку +$atm->cashIn(500); + +// Получение баланса, должно вывести 10000-1000+500 = 9500; +echo $atm->balance(); + +//Завершение работы, забираем карту +$atm->finish(); \ No newline at end of file