Skip to content

Commit 82d0ab7

Browse files
committed
Improve examples
1 parent f0b07a7 commit 82d0ab7

2 files changed

Lines changed: 26 additions & 26 deletions

File tree

JavaScript/4-operations.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,27 @@ class AccountCommand {
99
}
1010

1111
class BankAccount {
12+
static accounts = new Map();
13+
1214
constructor(name) {
1315
this.name = name;
1416
this.balance = 0;
15-
BankAccount.collection.set(name, this);
17+
BankAccount.accounts.set(name, this);
1618
}
1719
}
1820

19-
BankAccount.collection = new Map();
20-
21-
const operations = {
22-
Withdraw: (command) => {
23-
const account = BankAccount.collection.get(command.account);
21+
const OPERATIONS = {
22+
withdraw: (command) => {
23+
const account = BankAccount.accounts.get(command.account);
2424
account.balance -= command.amount;
2525
},
26-
Income: (command) => {
27-
const account = BankAccount.collection.get(command.account);
26+
income: (command) => {
27+
const account = BankAccount.accounts.get(command.account);
2828
account.balance += command.amount;
2929
},
30-
Allowed: (command) => {
31-
if (command.operation === 'Income') return true;
32-
const account = BankAccount.collection.get(command.account);
30+
allowed: (command) => {
31+
if (command.operation === 'income') return true;
32+
const account = BankAccount.accounts.get(command.account);
3333
return account.balance >= command.amount;
3434
},
3535
};
@@ -40,17 +40,17 @@ class Bank {
4040
}
4141

4242
operation(account, amount) {
43-
const operation = amount < 0 ? 'Withdraw' : 'Income';
44-
const execute = operations[operation];
43+
const operation = amount < 0 ? 'withdraw' : 'income';
44+
const execute = OPERATIONS[operation];
4545
const command = new AccountCommand(
4646
operation,
4747
account.name,
4848
Math.abs(amount),
4949
);
50-
const check = operations.Allowed;
50+
const check = OPERATIONS.allowed;
5151
const allowed = check(command);
5252
if (!allowed) {
53-
const target = BankAccount.collection.get(command.account);
53+
const target = BankAccount.accounts.get(command.account);
5454
const msg = [
5555
'Command is not allowed',
5656
'do ' + JSON.stringify(command),
@@ -75,7 +75,7 @@ bank.operation(account1, 1000);
7575
bank.operation(account1, -50);
7676
const account2 = new BankAccount('Antoninus Pius');
7777
bank.operation(account2, 500);
78-
bank.operation(account2, -10000);
78+
bank.operation(account2, -10000); // -10000
7979
bank.operation(account2, 150);
8080
bank.showOperations();
8181
console.table([account1, account2]);

JavaScript/5-undo.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ class AccountCommand {
99
}
1010

1111
class BankAccount {
12+
static accounts = new Map();
13+
1214
constructor(name) {
1315
this.name = name;
1416
this.balance = 0;
15-
BankAccount.collection.set(name, this);
17+
BankAccount.accounts.set(name, this);
1618
}
1719

1820
static find(name) {
19-
return BankAccount.collection.get(name);
21+
return BankAccount.accounts.get(name);
2022
}
2123
}
2224

23-
BankAccount.collection = new Map();
24-
25-
const operations = {
26-
Withdraw: {
25+
const OPERATIONS = {
26+
withdraw: {
2727
execute: (command) => {
2828
const account = BankAccount.find(command.account);
2929
account.balance -= command.amount;
@@ -33,7 +33,7 @@ const operations = {
3333
account.balance += command.amount;
3434
},
3535
},
36-
Income: {
36+
income: {
3737
execute: (command) => {
3838
const account = BankAccount.find(command.account);
3939
account.balance += command.amount;
@@ -51,8 +51,8 @@ class Bank {
5151
}
5252

5353
operation(account, amount) {
54-
const operation = amount < 0 ? 'Withdraw' : 'Income';
55-
const { execute } = operations[operation];
54+
const operation = amount < 0 ? 'withdraw' : 'income';
55+
const { execute } = OPERATIONS[operation];
5656
const command = new AccountCommand(
5757
operation,
5858
account.name,
@@ -66,7 +66,7 @@ class Bank {
6666
for (let i = 0; i < count; i++) {
6767
const command = this.commands.pop();
6868
const { operation } = command;
69-
const { undo } = operations[operation];
69+
const { undo } = OPERATIONS[operation];
7070
undo(command);
7171
}
7272
}

0 commit comments

Comments
 (0)