summaryrefslogtreecommitdiff
path: root/pbt/ts/src/bank.ts
blob: c5adc05d652433b77d658283b91b6f689fbbb96c (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
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.');
  }
}