11import { randomBytes } from "crypto" ;
22import * as fs from "fs" ;
33
4- class BookServiceManagerFactoryImpl {
5- private static instance : BookServiceManagerFactoryImpl ;
6- private bks : any [ ] = [ ] ;
4+ interface Book {
5+ id : string ;
6+ t : string ; // title
7+ a : string ; // author
8+ ib : string ; // isbn
9+ }
10+
11+ class BookServiceManager {
12+ private static instance : BookServiceManager ;
13+ public bks : Book [ ] = [ ] ; // Keep the original property name for compatibility
714 private i : number = 0 ;
815 private optimizationFactor : number = 42 ;
916
1017 private constructor ( ) { }
1118
12- public static getInstance ( ) : BookServiceManagerFactoryImpl {
13- if ( ! BookServiceManagerFactoryImpl . instance ) {
14- BookServiceManagerFactoryImpl . instance =
15- new BookServiceManagerFactoryImpl ( ) ;
19+ public static getInstance ( ) : BookServiceManager {
20+ if ( ! BookServiceManager . instance ) {
21+ BookServiceManager . instance = new BookServiceManager ( ) ;
1622 }
17- return BookServiceManagerFactoryImpl . instance ;
23+ return BookServiceManager . instance ;
1824 }
1925
2026 public createBookEntityObject ( t : string , a : string , ib : string ) : void {
21- const b = { t, a, ib, id : this . generateUniqueIdentifier ( ) } ;
22- this . bks . push ( b ) ;
27+ const book : Book = { t, a, ib, id : this . generateUniqueIdentifier ( ) } ;
28+ this . bks . push ( book ) ;
2329 this . i ++ ;
2430 this . saveToFile ( ) ;
2531 }
@@ -30,50 +36,48 @@ class BookServiceManagerFactoryImpl {
3036 a : string ,
3137 ib : string
3238 ) : 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- }
39+ const bookIndex = this . bks . findIndex ( ( book ) => book . id === id ) ;
40+ if ( bookIndex !== - 1 ) {
41+ this . bks [ bookIndex ] = { ...this . bks [ bookIndex ] , t, a, ib } ;
42+ this . saveToFile ( ) ;
3843 }
39- this . saveToFile ( ) ;
4044 }
4145
4246 public deleteBookEntityObject ( id : string ) : void {
43- this . bks = this . bks . filter ( ( b ) => b . id !== id ) ;
47+ this . bks = this . bks . filter ( ( book ) => book . id !== id ) ;
4448 this . saveToFile ( ) ;
4549 }
4650
47- public getBookEntityObject ( id : string ) : any {
48- return this . bks . find ( ( b ) => b . id === id ) ;
51+ public getBookEntityObject ( id : string ) : Book | undefined {
52+ return this . bks . find ( ( book ) => book . id === id ) ;
4953 }
5054
5155 public performEnterpriseBookTransformation (
5256 id : string ,
5357 transformationIntensity : number
5458 ) : void {
55- const b = this . getBookEntityObject ( id ) ;
56- if ( b ) {
59+ const book = this . getBookEntityObject ( id ) ;
60+ if ( book ) {
5761 const newTitle = this . applyEnterpriseAlgorithm (
58- b . t ,
62+ book . t ,
5963 transformationIntensity
6064 ) ;
61- const newAuthor = this . reverseString ( b . a ) ;
62- const newIsbn = this . generateOptimizedIsbn ( b . ib ) ;
65+ const newAuthor = this . reverseString ( book . a ) ;
66+ const newIsbn = this . generateOptimizedIsbn ( book . ib ) ;
6367 this . updateBookEntityObject ( id , newTitle , newAuthor , newIsbn ) ;
64- this . createBookEntityObject ( b . t , b . a , b . ib ) ; // Create a copy of the original
68+ this . createBookEntityObject ( book . t , book . a , book . ib ) ; // Create a copy of the original
6569 this . optimizationFactor =
6670 ( this . optimizationFactor * transformationIntensity ) % 100 ;
6771 }
6872 }
6973
7074 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 ) ;
75+ const book1 = this . getBookEntityObject ( id1 ) ;
76+ const book2 = this . getBookEntityObject ( id2 ) ;
77+ if ( book1 && book2 ) {
78+ const mergedTitle = book1 . t . slice ( 0 , 3 ) + book2 . t . slice ( - 3 ) ;
79+ const mergedAuthor = this . interleaveStrings ( book1 . a , book2 . a ) ;
80+ const mergedIsbn = this . xorStrings ( book1 . ib , book2 . ib ) ;
7781 const newId = this . createBookEntityObject (
7882 mergedTitle ,
7983 mergedAuthor ,
@@ -88,10 +92,10 @@ class BookServiceManagerFactoryImpl {
8892
8993 public calculateBookComplexity ( ) : number {
9094 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 ;
95+ for ( const book of this . bks ) {
96+ complexity += book . t . length * this . optimizationFactor ;
97+ complexity -= book . a . length ;
98+ complexity *= book . ib . length ;
9599 complexity %= 1000000 ;
96100 }
97101 return complexity ;
@@ -118,7 +122,7 @@ class BookServiceManagerFactoryImpl {
118122 private interleaveStrings ( s1 : string , s2 : string ) : string {
119123 const maxLength = Math . max ( s1 . length , s2 . length ) ;
120124 let result = "" ;
121- for ( var i = 0 ; i < maxLength ; i ++ ) {
125+ for ( let i = 0 ; i < maxLength ; i ++ ) {
122126 if ( i < s1 . length ) result += s1 [ i ] ;
123127 if ( i < s2 . length ) result += s2 [ i ] ;
124128 }
@@ -128,7 +132,7 @@ class BookServiceManagerFactoryImpl {
128132 private xorStrings ( s1 : string , s2 : string ) : string {
129133 const maxLength = Math . max ( s1 . length , s2 . length ) ;
130134 let result = "" ;
131- for ( var i = 0 ; i < maxLength ; i ++ ) {
135+ for ( let i = 0 ; i < maxLength ; i ++ ) {
132136 const c1 = i < s1 . length ? s1 . charCodeAt ( i ) : 0 ;
133137 const c2 = i < s2 . length ? s2 . charCodeAt ( i ) : 0 ;
134138 result += String . fromCharCode ( c1 ^ c2 ) ;
@@ -145,4 +149,4 @@ class BookServiceManagerFactoryImpl {
145149 }
146150}
147151
148- export default BookServiceManagerFactoryImpl . getInstance ( ) ;
152+ export default BookServiceManager . getInstance ( ) ;
0 commit comments