A ledger must never get into a wrong state. Parse transactions into a discriminated union, apply them with an exhaustive switch, and reject anything that would break the rules.
What to build
A program that applies transactions to accounts and prints the balances.
Requirements
- Parse each line into a
Txunion with a Result:deposit ACCOUNT AMOUNT,withdraw ACCOUNT AMOUNTortransfer FROM TO AMOUNT, where AMOUNT is a whole number greater than 0. Otherwise printline L: cannot parse(lines numbered from 1). - Apply with a switch on
kindthat ends with anevercheck. A deposit creates the account if needed. A withdrawal or transfer from an account that does not exist printsline L: no account NAME; one that would take a balance below 0 printsline L: insufficient funds in NAME; a transfer to the same account printsline L: same account. A failed transaction changes nothing. - Finally print
NAME: BALANCEfor every account in alphabetical order, thenapplied A of N.
Input
Line 1: N. Then N lines.
Output
The error lines, the balances, then the count.
Constraints
1 ≤ N ≤ 100.
Example
Input:
5
deposit ada 100
transfer ada alan 30
withdraw alan 50
withdraw bob 5
pay ada 1
Output:
line 3: insufficient funds in alan
line 4: no account bob
line 5: cannot parse
ada: 70
alan: 30
applied 2 of 5
Why it works
Raw text is turned into a Tx in one place, the switch handles each kind with only its own fields, the never check catches a forgotten kind, and checking before changing anything means a failed transaction can never leave half its effect behind.
Examples
5 deposit ada 100 transfer ada alan 30 withdraw alan 50 withdraw bob 5 pay ada 1
line 3: insufficient funds in alan line 4: no account bob line 5: cannot parse ada: 70 alan: 30 applied 2 of 5
3 deposit x 0 deposit x 10 transfer x x 5
line 1: cannot parse line 3: same account x: 10 applied 1 of 3
Hints
Hint 1
type Tx = { kind: "deposit"; to: string; amount: number } | { kind: "withdraw"; from: string; amount: number } | { kind: "transfer"; from: string; to: string; amount: number }.
Hint 2
parse returns Result<Tx>; apply returns Result<void> or a message, and changes nothing when it fails.
Solution
Show one correct solution (try it yourself first)
const read = (): string => prompt() ?? "";
type Tx =
| { kind: "deposit"; to: string; amount: number }
| { kind: "withdraw"; from: string; amount: number }
| { kind: "transfer"; from: string; to: string; amount: number };
type Result<T> = { ok: true; value: T } | { ok: false; error: string };
function amountOf(text: string | undefined): number | null {
return text !== undefined && /^\d+$/.test(text) && Number(text) > 0 ? Number(text) : null;
}
function parse(line: string): Result<Tx> {
const p = line.split(" ");
const fail: Result<Tx> = { ok: false, error: "cannot parse" };
if (p[0] === "deposit" && p.length === 3) {
const amount = amountOf(p[2]);
return amount === null ? fail : { ok: true, value: { kind: "deposit", to: p[1], amount } };
}
if (p[0] === "withdraw" && p.length === 3) {
const amount = amountOf(p[2]);
return amount === null ? fail : { ok: true, value: { kind: "withdraw", from: p[1], amount } };
}
if (p[0] === "transfer" && p.length === 4) {
const amount = amountOf(p[3]);
return amount === null ? fail : { ok: true, value: { kind: "transfer", from: p[1], to: p[2], amount } };
}
return fail;
}
const balances = new Map<string, number>();
function take(from: string, amount: number): string | null {
const have = balances.get(from);
if (have === undefined) return "no account " + from;
if (have < amount) return "insufficient funds in " + from;
return null;
}
function apply(tx: Tx): Result<void> {
switch (tx.kind) {
case "deposit":
balances.set(tx.to, (balances.get(tx.to) ?? 0) + tx.amount);
return { ok: true, value: undefined };
case "withdraw": {
const problem = take(tx.from, tx.amount);
if (problem) return { ok: false, error: problem };
balances.set(tx.from, (balances.get(tx.from) ?? 0) - tx.amount);
return { ok: true, value: undefined };
}
case "transfer": {
if (tx.from === tx.to) return { ok: false, error: "same account" };
const problem = take(tx.from, tx.amount);
if (problem) return { ok: false, error: problem };
balances.set(tx.from, (balances.get(tx.from) ?? 0) - tx.amount);
balances.set(tx.to, (balances.get(tx.to) ?? 0) + tx.amount);
return { ok: true, value: undefined };
}
default: {
const unhandled: never = tx;
return unhandled;
}
}
}
const n = Number(read());
let applied = 0;
for (let line = 1; line <= n; line++) {
const parsed = parse(read());
if (!parsed.ok) {
console.log("line " + line + ": " + parsed.error);
continue;
}
const result = apply(parsed.value);
if (result.ok) applied++;
else console.log("line " + line + ": " + result.error);
}
for (const name of [...balances.keys()].sort()) console.log(name + ": " + balances.get(name));
console.log("applied " + applied + " of " + n);