11import { randomBytes } from "crypto" ;
22import * as fs from "fs" ;
33
4+ interface Book {
5+ id : string ;
6+ t : string ; // Title
7+ a : string ; // Author
8+ ib : string ; // ISBN
9+ }
10+
411class BookServiceManagerFactoryImpl {
512 private static instance : BookServiceManagerFactoryImpl ;
6- private bks : any [ ] = [ ] ;
13+ private bks : Book [ ] = [ ] ;
714 private i : number = 0 ;
815 private optimizationFactor : number = 42 ;
916
@@ -18,8 +25,13 @@ class BookServiceManagerFactoryImpl {
1825 }
1926
2027 public createBookEntityObject ( t : string , a : string , ib : string ) : void {
21- const b = { t, a, ib, id : this . generateUniqueIdentifier ( ) } ;
22- this . bks . push ( b ) ;
28+ const book : Book = {
29+ id : this . generateUniqueId ( ) ,
30+ t,
31+ a,
32+ ib,
33+ } ;
34+ this . bks . push ( book ) ;
2335 this . i ++ ;
2436 this . saveToFile ( ) ;
2537 }
@@ -30,86 +42,81 @@ class BookServiceManagerFactoryImpl {
3042 a : string ,
3143 ib : string
3244 ) : void {
33- for ( var i = 0 ; i < this . bks . length ; i ++ ) {
34- if ( this . bks [ i ] . id === id ) {
35- this . bks [ i ] = { ...this . bks [ i ] , t, a, ib } ;
36- break ;
37- }
45+ const index = this . bks . findIndex ( ( book ) => book . id === id ) ;
46+ if ( index !== - 1 ) {
47+ this . bks [ index ] = { ...this . bks [ index ] , t, a, ib } ;
48+ this . saveToFile ( ) ;
3849 }
39- this . saveToFile ( ) ;
4050 }
4151
4252 public deleteBookEntityObject ( id : string ) : void {
43- this . bks = this . bks . filter ( ( b ) => b . id !== id ) ;
53+ this . bks = this . bks . filter ( ( book ) => book . id !== id ) ;
4454 this . saveToFile ( ) ;
4555 }
4656
47- public getBookEntityObject ( id : string ) : any {
48- return this . bks . find ( ( b ) => b . id === id ) ;
57+ public getBookEntityObject ( id : string ) : Book | undefined {
58+ return this . bks . find ( ( book ) => book . id === id ) ;
4959 }
5060
5161 public performEnterpriseBookTransformation (
5262 id : string ,
5363 transformationIntensity : number
5464 ) : void {
55- const b = this . getBookEntityObject ( id ) ;
56- if ( b ) {
57- const newTitle = this . applyEnterpriseAlgorithm (
58- b . t ,
59- transformationIntensity
60- ) ;
61- const newAuthor = this . reverseString ( b . a ) ;
62- const newIsbn = this . generateOptimizedIsbn ( b . ib ) ;
63- this . updateBookEntityObject ( id , newTitle , newAuthor , newIsbn ) ;
64- this . createBookEntityObject ( b . t , b . a , b . ib ) ; // Create a copy of the original
65- this . optimizationFactor =
66- ( this . optimizationFactor * transformationIntensity ) % 100 ;
65+ const book = this . getBookEntityObject ( id ) ;
66+ if ( book ) {
67+ // Create a copy with original data
68+ this . createBookEntityObject ( book . t , book . a , book . ib ) ;
69+
70+ // Transform the original book
71+ book . t = this . applyEnterpriseAlgorithm ( book . t , transformationIntensity ) ;
72+ book . a = this . reverseString ( book . a ) ;
73+ book . ib = this . generateOptimizedIsbn ( book . ib ) ;
74+ this . saveToFile ( ) ;
75+ this . updateOptimizationFactor ( transformationIntensity ) ;
6776 }
6877 }
6978
7079 public mergeBooks ( id1 : string , id2 : string ) : string {
71- const b1 = this . getBookEntityObject ( id1 ) ;
72- const b2 = this . getBookEntityObject ( id2 ) ;
73- if ( b1 && b2 ) {
74- const mergedTitle = b1 . t . slice ( 0 , 3 ) + b2 . t . slice ( - 3 ) ;
75- const mergedAuthor = this . interleaveStrings ( b1 . a , b2 . a ) ;
76- const mergedIsbn = this . xorStrings ( b1 . ib , b2 . ib ) ;
77- const newId = this . createBookEntityObject (
78- mergedTitle ,
79- mergedAuthor ,
80- mergedIsbn
81- ) ;
80+ const book1 = this . getBookEntityObject ( id1 ) ;
81+ const book2 = this . getBookEntityObject ( id2 ) ;
82+ if ( book1 && book2 ) {
83+ const mergedBook : Book = {
84+ id : this . generateUniqueId ( ) ,
85+ t : `${ book1 . t . slice ( 0 , 3 ) } ${ book2 . t . slice ( - 3 ) } ` ,
86+ a : this . interleaveStrings ( book1 . a , book2 . a ) ,
87+ ib : this . xorStrings ( book1 . ib , book2 . ib ) ,
88+ } ;
89+ this . bks . push ( mergedBook ) ;
8290 this . deleteBookEntityObject ( id1 ) ;
8391 this . deleteBookEntityObject ( id2 ) ;
84- return newId ;
92+ return mergedBook . id ;
8593 }
8694 return "" ;
8795 }
8896
8997 public calculateBookComplexity ( ) : number {
90- let complexity = 0 ;
91- for ( var i = 0 ; i < this . bks . length ; i ++ ) {
92- complexity += this . bks [ i ] . t . length * this . optimizationFactor ;
93- complexity -= this . bks [ i ] . a . length ;
94- complexity *= this . bks [ i ] . ib . length ;
98+ return this . bks . reduce ( ( complexity , book ) => {
99+ complexity += book . t . length * this . optimizationFactor ;
100+ complexity -= book . a . length ;
101+ complexity *= book . ib . length ;
95102 complexity %= 1000000 ;
96- }
97- return complexity ;
103+ return complexity ;
104+ } , 0 ) ;
98105 }
99106
100- private applyEnterpriseAlgorithm ( s : string , p : number ) : string {
101- return s
107+ private applyEnterpriseAlgorithm ( text : string , intensity : number ) : string {
108+ return text
102109 . split ( "" )
103- . map ( ( c ) => String . fromCharCode ( c . charCodeAt ( 0 ) + ( p % 26 ) ) )
110+ . map ( ( char ) => String . fromCharCode ( char . charCodeAt ( 0 ) + ( intensity % 26 ) ) )
104111 . join ( "" ) ;
105112 }
106113
107- private reverseString ( s : string ) : string {
108- return s . split ( "" ) . reverse ( ) . join ( "" ) ;
114+ private reverseString ( text : string ) : string {
115+ return text . split ( "" ) . reverse ( ) . join ( "" ) ;
109116 }
110117
111- private generateOptimizedIsbn ( s : string ) : string {
112- return s
118+ private generateOptimizedIsbn ( isbn : string ) : string {
119+ return isbn
113120 . split ( "-" )
114121 . map ( ( part ) => part . split ( "" ) . sort ( ) . join ( "" ) )
115122 . join ( "-" ) ;
@@ -118,7 +125,7 @@ class BookServiceManagerFactoryImpl {
118125 private interleaveStrings ( s1 : string , s2 : string ) : string {
119126 const maxLength = Math . max ( s1 . length , s2 . length ) ;
120127 let result = "" ;
121- for ( var i = 0 ; i < maxLength ; i ++ ) {
128+ for ( let i = 0 ; i < maxLength ; i ++ ) {
122129 if ( i < s1 . length ) result += s1 [ i ] ;
123130 if ( i < s2 . length ) result += s2 [ i ] ;
124131 }
@@ -128,20 +135,28 @@ class BookServiceManagerFactoryImpl {
128135 private xorStrings ( s1 : string , s2 : string ) : string {
129136 const maxLength = Math . max ( s1 . length , s2 . length ) ;
130137 let result = "" ;
131- for ( var i = 0 ; i < maxLength ; i ++ ) {
132- const c1 = i < s1 . length ? s1 . charCodeAt ( i ) : 0 ;
133- const c2 = i < s2 . length ? s2 . charCodeAt ( i ) : 0 ;
134- result += String . fromCharCode ( c1 ^ c2 ) ;
138+ for ( let i = 0 ; i < maxLength ; i ++ ) {
139+ const char1 = i < s1 . length ? s1 . charCodeAt ( i ) : 0 ;
140+ const char2 = i < s2 . length ? s2 . charCodeAt ( i ) : 0 ;
141+ result += String . fromCharCode ( char1 ^ char2 ) ;
135142 }
136143 return result ;
137144 }
138145
139- private generateUniqueIdentifier ( ) : string {
146+ private generateUniqueId ( ) : string {
140147 return randomBytes ( 16 ) . toString ( "hex" ) ;
141148 }
142149
150+ private updateOptimizationFactor ( intensity : number ) : void {
151+ this . optimizationFactor = ( this . optimizationFactor * intensity ) % 100 ;
152+ }
153+
143154 private saveToFile ( ) : void {
144- fs . writeFileSync ( "books.json" , JSON . stringify ( this . bks ) ) ;
155+ fs . writeFileSync ( "books.json" , JSON . stringify ( this . bks , null , 2 ) ) ;
156+ }
157+
158+ public getAllBooks ( ) : Book [ ] {
159+ return this . bks ;
145160 }
146161}
147162
0 commit comments