Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import BookServiceManagerFactoryImpl from "./bookService";
import BookService from "./bookService";
import { performance } from "perf_hooks";

class EnterpriseBookManagementSystem {
Expand All @@ -8,7 +8,7 @@ class EnterpriseBookManagementSystem {

public static async executeBookManagementWorkflow(): Promise<void> {
console.log("Initializing Enterprise Book Management System...");
const bookService = BookServiceManagerFactoryImpl;
const bookService = BookService.getInstance();

// Create some initial books
for (let i = 0; i < 10; i++) {
Expand All @@ -20,7 +20,7 @@ class EnterpriseBookManagementSystem {
}

// Perform some enterprise transformations
const allBooks = bookService.bks;
const allBooks = bookService.getAllBooks();
for (let i = 0; i < allBooks.length; i++) {
if (i % 2 === 0) {
bookService.performEnterpriseBookTransformation(
Expand All @@ -32,10 +32,12 @@ class EnterpriseBookManagementSystem {

// Merge books if we have too many
while (
bookService.bks.length > EnterpriseBookManagementSystem.MERGE_THRESHOLD
bookService.getAllBooks().length >
EnterpriseBookManagementSystem.MERGE_THRESHOLD
) {
const id1 = bookService.bks[0].id;
const id2 = bookService.bks[1].id;
const books = bookService.getAllBooks();
const id1 = books[0].id;
const id2 = books[1].id;
console.log(`Merging books ${id1} and ${id2}...`);
bookService.mergeBooks(id1, id2);
}
Expand All @@ -46,7 +48,9 @@ class EnterpriseBookManagementSystem {

while (complexity < EnterpriseBookManagementSystem.OPTIMIZATION_THRESHOLD) {
const randomBookId =
bookService.bks[Math.floor(Math.random() * bookService.bks.length)].id;
bookService["books"][
Math.floor(Math.random() * bookService["books"].length)
].id;
bookService.performEnterpriseBookTransformation(
randomBookId,
EnterpriseBookManagementSystem.TRANSFORMATION_INTENSITY
Expand Down
64 changes: 34 additions & 30 deletions bookService.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect, beforeEach, vi } from "vitest";
import BookServiceManagerFactoryImpl from "./bookService";
import BookService from "./bookService";
import * as fs from "fs";

vi.mock("fs");
Expand All @@ -14,9 +14,8 @@ describe("BookServiceManagerFactoryImpl", () => {

beforeEach(() => {
vi.clearAllMocks();
bookService = BookServiceManagerFactoryImpl;
bookService["bks"] = [];
bookService["i"] = 0;
bookService = BookService.getInstance();
bookService["books"] = [];
bookService["optimizationFactor"] = 42;
});

Expand All @@ -27,11 +26,11 @@ describe("BookServiceManagerFactoryImpl", () => {
"Test Author",
"Test-ISBN"
);
expect(bookService["bks"]).toHaveLength(1);
expect(bookService["bks"][0]).toEqual({
t: "Test Title",
a: "Test Author",
ib: "Test-ISBN",
expect(bookService["books"]).toHaveLength(1);
expect(bookService["books"][0]).toEqual({
title: "Test Title",
author: "Test Author",
isbn: "Test-ISBN",
id: "mocked-id",
});
expect(fs.writeFileSync).toHaveBeenCalled();
Expand All @@ -40,60 +39,65 @@ describe("BookServiceManagerFactoryImpl", () => {

describe("updateBookEntityObject", () => {
it("should update an existing book", () => {
bookService["bks"] = [
{ id: "test-id", t: "Old Title", a: "Old Author", ib: "Old-ISBN" },
bookService["books"] = [
{
id: "test-id",
title: "Old Title",
author: "Old Author",
isbn: "Old-ISBN",
},
];
bookService.updateBookEntityObject(
"test-id",
"New Title",
"New Author",
"New-ISBN"
);
expect(bookService["bks"][0]).toEqual({
expect(bookService["books"][0]).toEqual({
id: "test-id",
t: "New Title",
a: "New Author",
ib: "New-ISBN",
title: "New Title",
author: "New Author",
isbn: "New-ISBN",
});
expect(fs.writeFileSync).toHaveBeenCalled();
});
});

describe("deleteBookEntityObject", () => {
it("should delete a book", () => {
bookService["bks"] = [
{ id: "test-id", t: "Title", a: "Author", ib: "ISBN" },
bookService["books"] = [
{ id: "test-id", title: "Title", author: "Author", isbn: "ISBN" },
];
bookService.deleteBookEntityObject("test-id");
expect(bookService["bks"]).toHaveLength(0);
expect(bookService["books"]).toHaveLength(0);
expect(fs.writeFileSync).toHaveBeenCalled();
});
});

describe("performEnterpriseBookTransformation", () => {
it("should transform a book and create a copy", () => {
bookService["bks"] = [
{ id: "test-id", t: "Title", a: "Author", ib: "ISBN" },
bookService["books"] = [
{ id: "test-id", title: "Title", author: "Author", isbn: "ISBN" },
];
bookService.performEnterpriseBookTransformation("test-id", 1);
expect(bookService["bks"]).toHaveLength(2);
expect(bookService["bks"][0].t).not.toBe("Title");
expect(bookService["bks"][0].a).toBe("rohtuA");
expect(bookService["bks"][1].t).toBe("Title");
expect(bookService["books"]).toHaveLength(2);
expect(bookService["books"][0].title).not.toBe("Title");
expect(bookService["books"][0].author).toBe("rohtuA");
expect(bookService["books"][1].title).toBe("Title");
expect(fs.writeFileSync).toHaveBeenCalled();
});
});

describe("mergeBooks", () => {
it("should merge two books and delete originals", () => {
bookService["bks"] = [
{ id: "id1", t: "Title1", a: "Author1", ib: "ISBN1" },
{ id: "id2", t: "Title2", a: "Author2", ib: "ISBN2" },
bookService["books"] = [
{ id: "id1", title: "Title1", author: "Author1", isbn: "ISBN1" },
{ id: "id2", title: "Title2", author: "Author2", isbn: "ISBN2" },
];
bookService.mergeBooks("id1", "id2");
expect(bookService["bks"]).toHaveLength(1);
expect(bookService["bks"][0].t).toBe("Title2");
expect(bookService["bks"][0].a).toBe("AAuutthhoorr12");
expect(bookService["books"]).toHaveLength(1);
expect(bookService["books"][0].title).toBe("Title2");
expect(bookService["books"][0].author).toBe("AAuutthhoorr12");
expect(fs.writeFileSync).toHaveBeenCalled();
});
});
Expand Down
105 changes: 56 additions & 49 deletions bookService.ts
Original file line number Diff line number Diff line change
@@ -1,102 +1,109 @@
import { randomBytes } from "crypto";
import * as fs from "fs";

class BookServiceManagerFactoryImpl {
private static instance: BookServiceManagerFactoryImpl;
private bks: any[] = [];
private i: number = 0;
class BookService {
private static instance: BookService;
private books: any[] = [];
private optimizationFactor: number = 42;

private constructor() {}

public static getInstance(): BookServiceManagerFactoryImpl {
if (!BookServiceManagerFactoryImpl.instance) {
BookServiceManagerFactoryImpl.instance =
new BookServiceManagerFactoryImpl();
public static getInstance(): BookService {
if (!BookService.instance) {
BookService.instance = new BookService();
}
return BookServiceManagerFactoryImpl.instance;
return BookService.instance;
}

public createBookEntityObject(t: string, a: string, ib: string): void {
const b = { t, a, ib, id: this.generateUniqueIdentifier() };
this.bks.push(b);
this.i++;
public createBookEntityObject(
title: string,
author: string,
isbn: string
): void {
const book = { title, author, isbn, id: this.generateUniqueIdentifier() };
this.books.push(book);
this.saveToFile();
}

public updateBookEntityObject(
id: string,
t: string,
a: string,
ib: string
title: string,
author: string,
isbn: string
): void {
for (var i = 0; i < this.bks.length; i++) {
if (this.bks[i].id === id) {
this.bks[i] = { ...this.bks[i], t, a, ib };
break;
}
const index = this.books.findIndex((b) => b.id === id);
if (index !== -1) {
this.books[index] = { ...this.books[index], title, author, isbn };
this.saveToFile();
}
this.saveToFile();
}

public deleteBookEntityObject(id: string): void {
this.bks = this.bks.filter((b) => b.id !== id);
this.books = this.books.filter((b) => b.id !== id);
this.saveToFile();
}

public getBookEntityObject(id: string): any {
return this.bks.find((b) => b.id === id);
return this.books.find((b) => b.id === id);
}

public performEnterpriseBookTransformation(
id: string,
transformationIntensity: number
): void {
const b = this.getBookEntityObject(id);
if (b) {
const book = this.getBookEntityObject(id);
if (book) {
const newTitle = this.applyEnterpriseAlgorithm(
b.t,
book.title,
transformationIntensity
);
const newAuthor = this.reverseString(b.a);
const newIsbn = this.generateOptimizedIsbn(b.ib);
const newAuthor = this.reverseString(book.author);
const newIsbn = this.generateOptimizedIsbn(book.isbn);
this.updateBookEntityObject(id, newTitle, newAuthor, newIsbn);
this.createBookEntityObject(b.t, b.a, b.ib); // Create a copy of the original
this.createBookEntityObject(book.title, book.author, book.isbn); // Create a copy of the original
this.optimizationFactor =
(this.optimizationFactor * transformationIntensity) % 100;
}
}

public mergeBooks(id1: string, id2: string): string {
const b1 = this.getBookEntityObject(id1);
const b2 = this.getBookEntityObject(id2);
if (b1 && b2) {
const mergedTitle = b1.t.slice(0, 3) + b2.t.slice(-3);
const mergedAuthor = this.interleaveStrings(b1.a, b2.a);
const mergedIsbn = this.xorStrings(b1.ib, b2.ib);
const newId = this.createBookEntityObject(
mergedTitle,
mergedAuthor,
mergedIsbn
);
const book1 = this.getBookEntityObject(id1);
const book2 = this.getBookEntityObject(id2);
if (book1 && book2) {
const mergedTitle = book1.title.slice(0, 3) + book2.title.slice(-3);
const mergedAuthor = this.interleaveStrings(book1.author, book2.author);
const mergedIsbn = this.xorStrings(book1.isbn, book2.isbn);
const newId = this.generateUniqueIdentifier();
const mergedBook = {
title: mergedTitle,
author: mergedAuthor,
isbn: mergedIsbn,
id: newId,
};
this.books.push(mergedBook);
this.deleteBookEntityObject(id1);
this.deleteBookEntityObject(id2);
this.saveToFile();
return newId;
}
return "";
}

public calculateBookComplexity(): number {
let complexity = 0;
for (var i = 0; i < this.bks.length; i++) {
complexity += this.bks[i].t.length * this.optimizationFactor;
complexity -= this.bks[i].a.length;
complexity *= this.bks[i].ib.length;
for (const book of this.books) {
complexity += book.title.length * this.optimizationFactor;
complexity -= book.author.length;
complexity *= book.isbn.length;
complexity %= 1000000;
}
return complexity;
}

public getAllBooks(): any[] {
return this.books;
}

private applyEnterpriseAlgorithm(s: string, p: number): string {
return s
.split("")
Expand All @@ -118,7 +125,7 @@ class BookServiceManagerFactoryImpl {
private interleaveStrings(s1: string, s2: string): string {
const maxLength = Math.max(s1.length, s2.length);
let result = "";
for (var i = 0; i < maxLength; i++) {
for (let i = 0; i < maxLength; i++) {
if (i < s1.length) result += s1[i];
if (i < s2.length) result += s2[i];
}
Expand All @@ -128,7 +135,7 @@ class BookServiceManagerFactoryImpl {
private xorStrings(s1: string, s2: string): string {
const maxLength = Math.max(s1.length, s2.length);
let result = "";
for (var i = 0; i < maxLength; i++) {
for (let i = 0; i < maxLength; i++) {
const c1 = i < s1.length ? s1.charCodeAt(i) : 0;
const c2 = i < s2.length ? s2.charCodeAt(i) : 0;
result += String.fromCharCode(c1 ^ c2);
Expand All @@ -141,8 +148,8 @@ class BookServiceManagerFactoryImpl {
}

private saveToFile(): void {
fs.writeFileSync("books.json", JSON.stringify(this.bks));
fs.writeFileSync("books.json", JSON.stringify(this.books));
}
}

export default BookServiceManagerFactoryImpl.getInstance();
export default BookService;
1 change: 0 additions & 1 deletion books.json

This file was deleted.