-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample08.ts
More file actions
272 lines (227 loc) · 8.93 KB
/
example08.ts
File metadata and controls
272 lines (227 loc) · 8.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
interface FinancialData {
revenue: number;
expenses: number;
period: string;
transactions: Transaction[];
}
interface Transaction {
date: string;
amount: number;
description: string;
}
abstract class ReportGenerator {
protected data: FinancialData;
protected profit: number = 0;
protected profitMargin: number = 0;
constructor(data: FinancialData) {
this.data = data;
}
public generate(): void {
console.log("[INFO] Початок генерації звіту...");
// Hook перед підготовкою даних
this.beforePrepareData();
this.prepareData();
// Hook після підготовки даних
this.afterPrepareData();
// Hook перед рендерингом
if (this.shouldRenderReport()) {
this.renderHeader();
this.renderBody();
this.renderFooter();
// Hook перед збереженням
if (this.shouldSaveReport()) {
this.save();
}
}
// Hook після всіх операцій
this.afterReportGeneration();
console.log("[INFO] Звіт згенеровано успішно");
}
// Hook-методи, які підкласи можуть перевизначати
protected beforePrepareData(): void {
// За замовчуванням нічого не робить
}
protected afterPrepareData(): void {
// За замовчуванням нічого не робить
}
protected shouldRenderReport(): boolean {
// За замовчуванням завжди рендеримо
return true;
}
protected shouldSaveReport(): boolean {
// За замовчуванням завжди зберігаємо
return true;
}
protected afterReportGeneration(): void {
// За замовчуванням нічого не робить
}
protected prepareData(): void {
console.log("[INFO] Підготовка фінансових даних за період " + this.data.period);
this.calculateMetrics();
}
private calculateMetrics(): void {
this.profit = this.data.revenue - this.data.expenses;
this.profitMargin = (this.profit / this.data.revenue) * 100;
}
protected abstract renderHeader(): void;
protected abstract renderBody(): void;
protected abstract renderFooter(): void;
protected abstract save(): void;
}
class MarkdownReportGenerator extends ReportGenerator {
private skipSave: boolean = false;
private content: string = "";
constructor(data: FinancialData) {
super(data);
}
// Приклад використання hook-методу
protected beforePrepareData(): void {
console.log("[INFO] Підготовка до генерації Markdown...");
}
// Приклад умовного збереження
protected shouldSaveReport(): boolean {
return !this.skipSave;
}
// Можливість встановити пропуск збереження
public setSkipSave(skip: boolean): void {
this.skipSave = skip;
}
protected renderHeader(): void {
this.content = `# Фінансовий звіт за ${this.data.period}\n\n`;
this.content += `## Загальні показники\n`;
this.content += `* Дохід: ${this.data.revenue} грн\n`;
this.content += `* Витрати: ${this.data.expenses} грн\n`;
this.content += `* Прибуток: ${this.profit} грн\n`;
this.content += `* Маржа: ${this.profitMargin.toFixed(2)}%\n\n`;
}
protected renderBody(): void {
this.content += `## Транзакції\n\n`;
this.data.transactions.forEach(transaction => {
const type = transaction.amount > 0 ? "+" : "-";
this.content += `* ${transaction.date}: ${type}${Math.abs(transaction.amount)} грн - ${transaction.description}\n`;
});
this.content += "\n";
}
protected renderFooter(): void {
const balance = this.data.transactions.reduce((sum, t) => sum + t.amount, 0);
this.content += `## Підсумок\n`;
this.content += `Баланс за період: ${balance} грн\n\n`;
this.content += `---\n`;
this.content += `Звіт згенеровано: ${new Date().toLocaleString()}\n`;
}
protected save(): void {
const fileName = `report-${this.data.period}.md`;
const fs = require('fs');
fs.writeFileSync(fileName, this.content);
console.log(`[INFO] Звіт збережено у файл: ${fileName}`);
}
}
class HtmlReportGenerator extends ReportGenerator {
private includeStyles: boolean = true;
private content: string = "";
constructor(data: FinancialData) {
super(data);
}
// Приклад використання hook-методу для кастомізації стилів
protected afterPrepareData(): void {
if (!this.includeStyles) {
console.log("[INFO] Генерація HTML без стилів...");
}
}
protected renderHeader(): void {
this.content = `<!DOCTYPE html>
<html>
<head>
<title>Фінансовий звіт за ${this.data.period}</title>`;
if (this.includeStyles) {
this.content += `
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
.positive { color: green; }
.negative { color: red; }
.summary { background: #f5f5f5; padding: 20px; }
</style>`;
}
this.content += `
</head>
<body>
<h1>Фінансовий звіт за ${this.data.period}</h1>
<div class="summary">
<h2>Загальні показники</h2>
<p>Дохід: ${this.data.revenue} грн</p>
<p>Витрати: ${this.data.expenses} грн</p>
<p>Прибуток: ${this.profit} грн</p>
<p>Маржа: ${this.profitMargin.toFixed(2)}%</p>
</div>`;
}
protected renderBody(): void {
this.content += `
<h2>Транзакції</h2>
<table border="1" cellpadding="10">
<tr>
<th>Дата</th>
<th>Сума</th>
<th>Опис</th>
</tr>`;
this.data.transactions.forEach(transaction => {
const className = transaction.amount > 0 ? "positive" : "negative";
this.content += `
<tr>
<td>${transaction.date}</td>
<td class="${className}">${transaction.amount > 0 ? "+" : ""}${transaction.amount} грн</td>
<td>${transaction.description}</td>
</tr>`;
});
this.content += `
</table>`;
}
protected renderFooter(): void {
const balance = this.data.transactions.reduce((sum, t) => sum + t.amount, 0);
this.content += `
<div class="summary">
<h2>Підсумок</h2>
<p>Баланс за період: ${balance} грн</p>
<hr>
<small>Звіт згенеровано: ${new Date().toLocaleString()}</small>
</div>
</body>
</html>`;
}
protected save(): void {
const fileName = `report-${this.data.period}.html`;
const fs = require('fs');
fs.writeFileSync(fileName, this.content);
console.log(`[INFO] Звіт збережено у файл: ${fileName}`);
}
// Метод для налаштування стилів
public setIncludeStyles(include: boolean): void {
this.includeStyles = include;
}
}
// Тестові дані
const testData: FinancialData = {
revenue: 150000,
expenses: 85000,
period: "2024-Q1",
transactions: [
{ date: "2024-01-15", amount: 45000, description: "Продаж продукту А" },
{ date: "2024-02-01", amount: -30000, description: "Оплата оренди" },
{ date: "2024-02-15", amount: 65000, description: "Продаж продукту B" },
{ date: "2024-03-01", amount: -25000, description: "Зарплати" }
]
};
// Тестування з використанням hook-методів
console.log("=== Генерація Markdown звіту (зі збереженням) ===");
const markdownReport = new MarkdownReportGenerator(testData);
markdownReport.generate();
console.log("\n=== Генерація Markdown звіту (без збереження) ===");
const markdownReportNoSave = new MarkdownReportGenerator(testData);
markdownReportNoSave.setSkipSave(true);
markdownReportNoSave.generate();
console.log("\n=== Генерація HTML звіту (зі стилями) ===");
const htmlReport = new HtmlReportGenerator(testData);
htmlReport.generate();
console.log("\n=== Генерація HTML звіту (без стилів) ===");
const htmlReportNoStyles = new HtmlReportGenerator(testData);
htmlReportNoStyles.setIncludeStyles(false);
htmlReportNoStyles.generate();