PHP: A bank with custom errors

Build a bank where every broken rule is an exception with a clear message.

What to build

A program that runs bank commands and reports errors.

Requirements

  • Commands (one per line, read until the input ends): open NAME, deposit NAME N, withdraw NAME N, transfer FROM TO N and balance NAME.
  • A successful change prints ok. balance prints NAME: B.
  • Errors print error: MESSAGE: account NAME exists (open twice), no account NAME (unknown name), amount must be positive (N is 0 or less) and insufficient funds (not enough money). A failed command changes nothing.

Input

Any number of command lines.

Output

One line per command.

Constraints

At most 100 lines; amounts below 100000; names are single words.

Example

Input:

open ann
open ann
deposit ann 100
balance ann

Output:

ok
error: account ann exists
ok
ann: 100

Why it works

Each rule is checked in one place, throws a clear error, and the loop turns every error into one message without stopping.

Examples

Example 1 · Input
open ann
open ann
deposit ann 100
balance ann
Expected output
ok
error: account ann exists
ok
ann: 100
Example 2 · Input
open a
open b
deposit a 50
transfer a b 20
balance a
balance b
Expected output
ok
ok
ok
ok
a: 30
b: 20

Hints

Hint 1

Write one exception class for each broken rule, or one class with different messages.

Hint 2

Catch Exception in the loop and print ~error: ~ followed by the message; print ~ok~ when nothing was thrown.

Solution

Show one correct solution (try it yourself first)
<?php
class Bank
{
    private array $accounts = [];

    private function check(string $name): void
    {
        if (!isset($this->accounts[$name])) {
            throw new RuntimeException("no account $name");
        }
    }

    private function positive(int $amount): void
    {
        if ($amount <= 0) {
            throw new InvalidArgumentException("amount must be positive");
        }
    }

    public function open(string $name): void
    {
        if (isset($this->accounts[$name])) {
            throw new RuntimeException("account $name exists");
        }
        $this->accounts[$name] = 0;
    }

    public function deposit(string $name, int $amount): void
    {
        $this->check($name);
        $this->positive($amount);
        $this->accounts[$name] += $amount;
    }

    public function withdraw(string $name, int $amount): void
    {
        $this->check($name);
        $this->positive($amount);
        if ($this->accounts[$name] < $amount) {
            throw new RuntimeException("insufficient funds");
        }
        $this->accounts[$name] -= $amount;
    }

    public function transfer(string $from, string $to, int $amount): void
    {
        $this->check($from);
        $this->check($to);
        $this->positive($amount);
        $this->withdraw($from, $amount);
        $this->deposit($to, $amount);
    }

    public function balance(string $name): int
    {
        $this->check($name);
        return $this->accounts[$name];
    }
}

$bank = new Bank();
while (($line = fgets(STDIN)) !== false) {
    $parts = explode(" ", trim($line));
    if ($parts[0] === "") {
        continue;
    }
    try {
        switch ($parts[0]) {
            case "open":
                $bank->open($parts[1]);
                echo "ok\n";
                break;
            case "deposit":
                $bank->deposit($parts[1], (int) $parts[2]);
                echo "ok\n";
                break;
            case "withdraw":
                $bank->withdraw($parts[1], (int) $parts[2]);
                echo "ok\n";
                break;
            case "transfer":
                $bank->transfer($parts[1], $parts[2], (int) $parts[3]);
                echo "ok\n";
                break;
            case "balance":
                $b = $bank->balance($parts[1]);
                echo $parts[1], ": ", $b, "\n";
                break;
        }
    } catch (Exception $e) {
        echo "error: ", $e->getMessage(), "\n";
    }
}

▶ Open this solution in the compiler

Lesson: Exceptions →