Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions ATM.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

class ATM
{
private $accountRepository;
private $cardRepository;
private $activeCard;
private $confirmedPin;

public function __construct($accountRepository, $cardRepository)
{
$this->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;
}

}
41 changes: 41 additions & 0 deletions Account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

class Account
{
private $accountNumber;
private $amount;

public function __construct($accountNumber, $amount)
{
$this->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");
}

}
54 changes: 54 additions & 0 deletions Card.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

class Card
{
private $cardNumber;
private $hashPin;
private $accountNumber;
private $cardIsActive;
public function __construct($cardNumber, $pin, $accountNumber)
{
$this->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;
}

}
18 changes: 18 additions & 0 deletions Repository/AccountRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

require_once "Repository.php";

class AccountRepository extends Repository
{
private $accounts = [];

public function add($account)
{
$this->accounts[$account->getAccountNumber()] = $account;
}

public function getItem($accountNumber)
{
return $this->accounts[$accountNumber];
}
}
27 changes: 27 additions & 0 deletions Repository/CardRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

require_once "Repository.php";

class CardRepository extends Repository
{
private $accountRepository;
private $cards;

public function __construct($accountRepository)
{
$this->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];
}
}
7 changes: 7 additions & 0 deletions Repository/Repository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

abstract class Repository
{
abstract public function add($item);
abstract public function getItem($key);
}
55 changes: 55 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

require_once "Repository/AccountRepository.php";
require_once "Repository/CardRepository.php";
require_once "Account.php";
require_once "ATM.php";
require_once "Card.php";


// Создание репозитория счетов
$accountRepository = new AccountRepository();

// Создание репозитория карт
$cardRepository = new CardRepository($accountRepository);

// номер счета
$accountNumber = "3000400050006000";
// количество средств на счете
$amount = 10000;

// Добавление счета
$accountRepository->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();