11import { randomBytes } from "crypto" ;
22import * as fs from "fs" ;
33
4- class BookServiceManagerFactoryImpl {
5- private static instance : BookServiceManagerFactoryImpl ;
6- private bks : any [ ] = [ ] ;
7- private i : number = 0 ;
4+ class BookService {
5+ private static instance : BookService ;
6+ private books : any [ ] = [ ] ;
87 private optimizationFactor : number = 42 ;
98
109 private constructor ( ) { }
1110
12- public static getInstance ( ) : BookServiceManagerFactoryImpl {
13- if ( ! BookServiceManagerFactoryImpl . instance ) {
14- BookServiceManagerFactoryImpl . instance =
15- new BookServiceManagerFactoryImpl ( ) ;
11+ public static getInstance ( ) : BookService {
12+ if ( ! BookService . instance ) {
13+ BookService . instance = new BookService ( ) ;
1614 }
17- return BookServiceManagerFactoryImpl . instance ;
15+ return BookService . instance ;
1816 }
1917
20- public createBookEntityObject ( t : string , a : string , ib : string ) : void {
21- const b = { t, a, ib, id : this . generateUniqueIdentifier ( ) } ;
22- this . bks . push ( b ) ;
23- this . i ++ ;
18+ public createBookEntityObject (
19+ title : string ,
20+ author : string ,
21+ isbn : string
22+ ) : void {
23+ const book = { title, author, isbn, id : this . generateUniqueIdentifier ( ) } ;
24+ this . books . push ( book ) ;
2425 this . saveToFile ( ) ;
2526 }
2627
2728 public updateBookEntityObject (
2829 id : string ,
29- t : string ,
30- a : string ,
31- ib : string
30+ title : string ,
31+ author : string ,
32+ isbn : string
3233 ) : 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- }
34+ const index = this . books . findIndex ( ( b ) => b . id === id ) ;
35+ if ( index !== - 1 ) {
36+ this . books [ index ] = { ...this . books [ index ] , title, author, isbn } ;
37+ this . saveToFile ( ) ;
3838 }
39- this . saveToFile ( ) ;
4039 }
4140
4241 public deleteBookEntityObject ( id : string ) : void {
43- this . bks = this . bks . filter ( ( b ) => b . id !== id ) ;
42+ this . books = this . books . filter ( ( b ) => b . id !== id ) ;
4443 this . saveToFile ( ) ;
4544 }
4645
4746 public getBookEntityObject ( id : string ) : any {
48- return this . bks . find ( ( b ) => b . id === id ) ;
47+ return this . books . find ( ( b ) => b . id === id ) ;
4948 }
5049
5150 public performEnterpriseBookTransformation (
5251 id : string ,
5352 transformationIntensity : number
5453 ) : void {
55- const b = this . getBookEntityObject ( id ) ;
56- if ( b ) {
54+ const book = this . getBookEntityObject ( id ) ;
55+ if ( book ) {
5756 const newTitle = this . applyEnterpriseAlgorithm (
58- b . t ,
57+ book . title ,
5958 transformationIntensity
6059 ) ;
61- const newAuthor = this . reverseString ( b . a ) ;
62- const newIsbn = this . generateOptimizedIsbn ( b . ib ) ;
60+ const newAuthor = this . reverseString ( book . author ) ;
61+ const newIsbn = this . generateOptimizedIsbn ( book . isbn ) ;
6362 this . updateBookEntityObject ( id , newTitle , newAuthor , newIsbn ) ;
64- this . createBookEntityObject ( b . t , b . a , b . ib ) ; // Create a copy of the original
63+ this . createBookEntityObject ( book . title , book . author , book . isbn ) ; // Create a copy of the original
6564 this . optimizationFactor =
6665 ( this . optimizationFactor * transformationIntensity ) % 100 ;
6766 }
6867 }
6968
7069 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- ) ;
70+ const book1 = this . getBookEntityObject ( id1 ) ;
71+ const book2 = this . getBookEntityObject ( id2 ) ;
72+ if ( book1 && book2 ) {
73+ const mergedTitle = book1 . title . slice ( 0 , 3 ) + book2 . title . slice ( - 3 ) ;
74+ const mergedAuthor = this . interleaveStrings ( book1 . author , book2 . author ) ;
75+ const mergedIsbn = this . xorStrings ( book1 . isbn , book2 . isbn ) ;
76+ const newId = this . generateUniqueIdentifier ( ) ;
77+ const mergedBook = {
78+ title : mergedTitle ,
79+ author : mergedAuthor ,
80+ isbn : mergedIsbn ,
81+ id : newId ,
82+ } ;
83+ this . books . push ( mergedBook ) ;
8284 this . deleteBookEntityObject ( id1 ) ;
8385 this . deleteBookEntityObject ( id2 ) ;
86+ this . saveToFile ( ) ;
8487 return newId ;
8588 }
8689 return "" ;
8790 }
8891
8992 public calculateBookComplexity ( ) : number {
9093 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 ;
94+ for ( const book of this . books ) {
95+ complexity += book . title . length * this . optimizationFactor ;
96+ complexity -= book . author . length ;
97+ complexity *= book . isbn . length ;
9598 complexity %= 1000000 ;
9699 }
97100 return complexity ;
98101 }
99102
103+ public getAllBooks ( ) : any [ ] {
104+ return this . books ;
105+ }
106+
100107 private applyEnterpriseAlgorithm ( s : string , p : number ) : string {
101108 return s
102109 . split ( "" )
@@ -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,7 +135,7 @@ 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 ++ ) {
138+ for ( let i = 0 ; i < maxLength ; i ++ ) {
132139 const c1 = i < s1 . length ? s1 . charCodeAt ( i ) : 0 ;
133140 const c2 = i < s2 . length ? s2 . charCodeAt ( i ) : 0 ;
134141 result += String . fromCharCode ( c1 ^ c2 ) ;
@@ -141,8 +148,8 @@ class BookServiceManagerFactoryImpl {
141148 }
142149
143150 private saveToFile ( ) : void {
144- fs . writeFileSync ( "books.json" , JSON . stringify ( this . bks ) ) ;
151+ fs . writeFileSync ( "books.json" , JSON . stringify ( this . books ) ) ;
145152 }
146153}
147154
148- export default BookServiceManagerFactoryImpl . getInstance ( ) ;
155+ export default BookService ;
0 commit comments