Skip to content

Commit f0b07a7

Browse files
committed
Improve examples
1 parent f7b29af commit f0b07a7

5 files changed

Lines changed: 24 additions & 13 deletions

File tree

JavaScript/1-execute.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ class Income extends AccountCommand {
2323
}
2424
}
2525

26-
class BankAccount { // Receiver or Target
26+
class BankAccount {
27+
// Receiver or Target
2728
constructor(name) {
2829
this.name = name;
2930
this.balance = 0;
3031
}
3132
}
3233

33-
class Bank { // Invoker
34+
class Bank {
35+
// Invoker
3436
constructor() {
3537
this.commands = [];
3638
}
@@ -48,7 +50,7 @@ class Bank { // Invoker
4850
output.push({
4951
operation: command.constructor.name,
5052
account: command.account.name,
51-
amount: command.amount
53+
amount: command.amount,
5254
});
5355
}
5456
console.table(output);

JavaScript/2-undo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Bank {
6767
output.push({
6868
operation: command.constructor.name,
6969
account: command.account.name,
70-
amount: command.amount
70+
amount: command.amount,
7171
});
7272
}
7373
console.table(output);

JavaScript/3-anemic.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ class Bank {
2323
operation(account, amount) {
2424
const operation = amount < 0 ? 'Withdraw' : 'Income';
2525
const command = new AccountCommand(
26-
operation, account.name, Math.abs(amount)
26+
operation,
27+
account.name,
28+
Math.abs(amount),
2729
);
2830
this.commands.push(command);
2931
account.balance += amount;

JavaScript/4-operations.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,20 @@ class Bank {
4343
const operation = amount < 0 ? 'Withdraw' : 'Income';
4444
const execute = operations[operation];
4545
const command = new AccountCommand(
46-
operation, account.name, Math.abs(amount)
46+
operation,
47+
account.name,
48+
Math.abs(amount),
4749
);
48-
const allowed = operations.Allowed(command);
50+
const check = operations.Allowed;
51+
const allowed = check(command);
4952
if (!allowed) {
5053
const target = BankAccount.collection.get(command.account);
51-
throw new Error(
52-
'Command is not allowed:\n' + JSON.stringify(command) +
53-
'\non ' + JSON.stringify(target)
54-
);
54+
const msg = [
55+
'Command is not allowed',
56+
'do ' + JSON.stringify(command),
57+
'on ' + JSON.stringify(target),
58+
];
59+
throw new Error(msg.join('\n'));
5560
}
5661
this.commands.push(command);
5762
execute(command);
@@ -70,7 +75,7 @@ bank.operation(account1, 1000);
7075
bank.operation(account1, -50);
7176
const account2 = new BankAccount('Antoninus Pius');
7277
bank.operation(account2, 500);
73-
bank.operation(account2, -100);
78+
bank.operation(account2, -10000);
7479
bank.operation(account2, 150);
7580
bank.showOperations();
7681
console.table([account1, account2]);

JavaScript/5-undo.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ class Bank {
5454
const operation = amount < 0 ? 'Withdraw' : 'Income';
5555
const { execute } = operations[operation];
5656
const command = new AccountCommand(
57-
operation, account.name, Math.abs(amount)
57+
operation,
58+
account.name,
59+
Math.abs(amount),
5860
);
5961
this.commands.push(command);
6062
execute(command);

0 commit comments

Comments
 (0)