@@ -11,59 +11,53 @@ const addAccount = (name: string): Account => {
11
11
return account ;
12
12
} ;
13
13
14
+ type OperationType = 'withdraw' | 'income' ;
15
+
14
16
interface Command {
15
- operation : string ;
17
+ operation : OperationType ;
16
18
account : string ;
17
19
amount : number ;
18
20
}
19
21
20
- type OperationType = 'withdraw' | 'income' ;
22
+ interface Operation {
23
+ execute : ( command : Command ) => void ;
24
+ undo : ( command : Command ) => void ;
25
+ } ;
26
+
27
+ type Operations = Record < OperationType , Operation > ;
21
28
22
- const OPERATIONS : Record < OperationType , {
23
- execute : ( command : Command ) => void ;
24
- undo : ( command : Command ) => void ;
25
- } > = {
29
+ const OPERATIONS : Operations = {
26
30
withdraw : {
27
31
execute : ( command : Command ) : void => {
28
32
const account = accounts . get ( command . account ) ;
29
- if ( account ) {
30
- account . balance -= command . amount ;
31
- }
33
+ if ( account ) account . balance -= command . amount ;
32
34
} ,
33
35
undo : ( command : Command ) : void => {
34
36
const account = accounts . get ( command . account ) ;
35
- if ( account ) {
36
- account . balance += command . amount ;
37
- }
37
+ if ( account ) account . balance += command . amount ;
38
38
} ,
39
39
} ,
40
40
income : {
41
41
execute : ( command : Command ) : void => {
42
42
const account = accounts . get ( command . account ) ;
43
- if ( account ) {
44
- account . balance += command . amount ;
45
- }
43
+ if ( account ) account . balance += command . amount ;
46
44
} ,
47
45
undo : ( command : Command ) : void => {
48
46
const account = accounts . get ( command . account ) ;
49
- if ( account ) {
50
- account . balance -= command . amount ;
51
- }
47
+ if ( account ) account . balance -= command . amount ;
52
48
} ,
53
49
} ,
54
50
} ;
55
51
56
52
class Bank {
57
- private commands : Command [ ] = [ ] ;
53
+ private commands : Array < Command > = [ ] ;
58
54
59
- operation ( account : Account , amount : number ) : void {
60
- const operation = amount < 0 ? 'withdraw' : 'income' ;
55
+ operation ( account : Account , value : number ) : void {
56
+ const operation = value < 0 ? 'withdraw' : 'income' ;
61
57
const { execute } = OPERATIONS [ operation ] ;
62
- const command : Command = {
63
- operation,
64
- account : account . name ,
65
- amount : Math . abs ( amount ) ,
66
- } ;
58
+ const amount = Math . abs ( value ) ;
59
+ const { name } = account ;
60
+ const command : Command = { operation, account : name , amount } ;
67
61
this . commands . push ( command ) ;
68
62
execute ( command ) ;
69
63
}
0 commit comments