@@ -11,59 +11,53 @@ const addAccount = (name: string): Account => {
1111 return account ;
1212} ;
1313
14+ type OperationType = 'withdraw' | 'income' ;
15+
1416interface Command {
15- operation : string ;
17+ operation : OperationType ;
1618 account : string ;
1719 amount : number ;
1820}
1921
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 > ;
2128
22- const OPERATIONS : Record < OperationType , {
23- execute : ( command : Command ) => void ;
24- undo : ( command : Command ) => void ;
25- } > = {
29+ const OPERATIONS : Operations = {
2630 withdraw : {
2731 execute : ( command : Command ) : void => {
2832 const account = accounts . get ( command . account ) ;
29- if ( account ) {
30- account . balance -= command . amount ;
31- }
33+ if ( account ) account . balance -= command . amount ;
3234 } ,
3335 undo : ( command : Command ) : void => {
3436 const account = accounts . get ( command . account ) ;
35- if ( account ) {
36- account . balance += command . amount ;
37- }
37+ if ( account ) account . balance += command . amount ;
3838 } ,
3939 } ,
4040 income : {
4141 execute : ( command : Command ) : void => {
4242 const account = accounts . get ( command . account ) ;
43- if ( account ) {
44- account . balance += command . amount ;
45- }
43+ if ( account ) account . balance += command . amount ;
4644 } ,
4745 undo : ( command : Command ) : void => {
4846 const account = accounts . get ( command . account ) ;
49- if ( account ) {
50- account . balance -= command . amount ;
51- }
47+ if ( account ) account . balance -= command . amount ;
5248 } ,
5349 } ,
5450} ;
5551
5652class Bank {
57- private commands : Command [ ] = [ ] ;
53+ private commands : Array < Command > = [ ] ;
5854
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' ;
6157 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 } ;
6761 this . commands . push ( command ) ;
6862 execute ( command ) ;
6963 }
0 commit comments