summaryrefslogtreecommitdiff
path: root/pbt/ts/src/bank.ts
blob: e5619f4320b9c92862ebc89209f0f8fe1f9a3a4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import {Option, fromNullable} from 'fp-ts/lib/Option';

// keep it simple
export type Account = string;

export interface Bank {
  balance: (account: Account) => Option<number>;

  /// Deposit amount into account
  ///
  /// This function increases the balance of the given account by the
  /// specified amount. It's a `void` function hence implicitly
  /// stateful: It's expected to change some internal state of the
  /// implementing class.
  ///
  /// @param account The account to deposit into
  /// @param amt The amount to deposit
  deposit: (account: Account, amt: number) => void;
  withdraw: (account: Account, amt: number) => void;
}

export class SimpleBank implements Bank {
  private accounts: Map<Account, number>;

  constructor(accounts: Map<Account, number>) {
    // clone the content of the map
    this.accounts = new Map(accounts);
  }

  balance(account: Account): Option<number> {
    return fromNullable(this.accounts.get(account));
  }

  deposit(account: Account, amt: number): void {
    throw new Error('Method not implemented.');
  }

  withdraw(account: Account, amt: number): void {
    throw new Error('Method not implemented.');
  }
}

export type Operation = GetBalance | Withdraw | Deposit;

export type GetBalance = {op: 'GetBalance'; account: Account};
export type Withdraw = {op: 'Withdraw'; account: Account; amount: number};
export type Deposit = {op: 'Deposit'; account: Account; amount: number};