File tree 5 files changed +18
-0
lines changed
5 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ class AccountCommand {
5
5
this . account = account ;
6
6
this . amount = amount ;
7
7
}
8
+
8
9
execute ( ) {
9
10
throw new Error ( 'Command is not implemented' ) ;
10
11
}
@@ -33,12 +34,14 @@ class Bank { // Invoker
33
34
constructor ( ) {
34
35
this . commands = [ ] ;
35
36
}
37
+
36
38
operation ( account , amount ) {
37
39
const Command = amount < 0 ? Withdraw : Income ;
38
40
const command = new Command ( account , Math . abs ( amount ) ) ;
39
41
command . execute ( ) ;
40
42
this . commands . push ( command ) ;
41
43
}
44
+
42
45
showOperations ( ) {
43
46
const output = [ ] ;
44
47
for ( const command of this . commands ) {
Original file line number Diff line number Diff line change @@ -5,9 +5,11 @@ class AccountCommand {
5
5
this . account = account ;
6
6
this . amount = amount ;
7
7
}
8
+
8
9
execute ( ) {
9
10
throw new Error ( 'Command.execute() is not implemented' ) ;
10
11
}
12
+
11
13
undo ( ) {
12
14
throw new Error ( 'Command.undo() is not implemented' ) ;
13
15
}
@@ -17,6 +19,7 @@ class Withdraw extends AccountCommand {
17
19
execute ( ) {
18
20
this . account . balance -= this . amount ;
19
21
}
22
+
20
23
undo ( ) {
21
24
this . account . balance += this . amount ;
22
25
}
@@ -26,6 +29,7 @@ class Income extends AccountCommand {
26
29
execute ( ) {
27
30
this . account . balance += this . amount ;
28
31
}
32
+
29
33
undo ( ) {
30
34
this . account . balance -= this . amount ;
31
35
}
@@ -42,18 +46,21 @@ class Bank {
42
46
constructor ( ) {
43
47
this . commands = [ ] ;
44
48
}
49
+
45
50
operation ( account , amount ) {
46
51
const Command = amount < 0 ? Withdraw : Income ;
47
52
const command = new Command ( account , Math . abs ( amount ) ) ;
48
53
command . execute ( ) ;
49
54
this . commands . push ( command ) ;
50
55
}
56
+
51
57
undo ( count ) {
52
58
for ( let i = 0 ; i < count ; i ++ ) {
53
59
const command = this . commands . pop ( ) ;
54
60
command . undo ( ) ;
55
61
}
56
62
}
63
+
57
64
showOperations ( ) {
58
65
const output = [ ] ;
59
66
for ( const command of this . commands ) {
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ class Bank {
19
19
constructor ( ) {
20
20
this . commands = [ ] ;
21
21
}
22
+
22
23
operation ( account , amount ) {
23
24
const operation = amount < 0 ? 'Withdraw' : 'Income' ;
24
25
const command = new AccountCommand (
@@ -27,6 +28,7 @@ class Bank {
27
28
this . commands . push ( command ) ;
28
29
account . balance += amount ;
29
30
}
31
+
30
32
showOperations ( ) {
31
33
console . table ( this . commands ) ;
32
34
}
Original file line number Diff line number Diff line change @@ -38,6 +38,7 @@ class Bank {
38
38
constructor ( ) {
39
39
this . commands = [ ] ;
40
40
}
41
+
41
42
operation ( account , amount ) {
42
43
const operation = amount < 0 ? 'Withdraw' : 'Income' ;
43
44
const execute = operations [ operation ] ;
@@ -55,6 +56,7 @@ class Bank {
55
56
this . commands . push ( command ) ;
56
57
execute ( command ) ;
57
58
}
59
+
58
60
showOperations ( ) {
59
61
console . table ( this . commands ) ;
60
62
}
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ class BankAccount {
14
14
this . balance = 0 ;
15
15
BankAccount . collection . set ( name , this ) ;
16
16
}
17
+
17
18
static find ( name ) {
18
19
return BankAccount . collection . get ( name ) ;
19
20
}
@@ -48,6 +49,7 @@ class Bank {
48
49
constructor ( ) {
49
50
this . commands = [ ] ;
50
51
}
52
+
51
53
operation ( account , amount ) {
52
54
const operation = amount < 0 ? 'Withdraw' : 'Income' ;
53
55
const { execute } = operations [ operation ] ;
@@ -57,6 +59,7 @@ class Bank {
57
59
this . commands . push ( command ) ;
58
60
execute ( command ) ;
59
61
}
62
+
60
63
undo ( count ) {
61
64
for ( let i = 0 ; i < count ; i ++ ) {
62
65
const command = this . commands . pop ( ) ;
@@ -65,6 +68,7 @@ class Bank {
65
68
undo ( command ) ;
66
69
}
67
70
}
71
+
68
72
showOperations ( ) {
69
73
console . table ( this . commands ) ;
70
74
}
You can’t perform that action at this time.
0 commit comments