/
flyweight_extrinsic_states.html
339 lines (289 loc) · 9.37 KB
/
flyweight_extrinsic_states.html
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Flyweight Design Pattern Example</title>
<style>
/* CSS styles here */
</style>
</head>
<body>
<h1>Flyweight Design Pattern Example</h1>
<div id="output"></div>
<h2>Add Book Record</h2>
<form id="addRecordForm">
<label for="title">Title:</label>
<input type="text" id="title" required /><br />
<label for="author">Author:</label>
<input type="text" id="author" required /><br />
<label for="genre">Genre:</label>
<input type="text" id="genre" required /><br />
<label for="pageCount">Page Count:</label>
<input type="number" id="pageCount" required /><br />
<label for="publisherID">Publisher ID:</label>
<input type="text" id="publisherID" required /><br />
<label for="ISBN">ISBN:</label>
<input type="text" id="ISBN" required /><br />
<button type="submit">Add Book Record</button>
</form>
<h2>Update Checkout Status</h2>
<form id="updateStatusForm">
<label for="bookID">Book ID:</label>
<input type="text" id="bookID" required /><br />
<label for="newStatus">Availability:</label>
<select id="newStatus" required>
<option value="true">Available</option>
<option value="false">Not Available</option>
</select><br />
<label for="checkoutDate">Checkout Date:</label>
<input type="date" id="checkoutDate" required /><br />
<label for="checkoutMember">Checkout Member:</label>
<input type="text" id="checkoutMember" required /><br />
<label for="newReturnDate">New Return Date:</label>
<input type="date" id="newReturnDate" required /><br />
<button type="submit">Update Checkout Status</button>
</form>
<h2>Extend Checkout Period</h2>
<form id="extendPeriodForm">
<label for="bookIDExtend">Book ID:</label>
<input type="text" id="bookIDExtend" required /><br />
<label for="newReturnDateExtend">New Return Date:</label>
<input type="date" id="newReturnDateExtend" required /><br />
<button type="submit">Extend Checkout Period</button>
</form>
<h2>Check Past Due</h2>
<form id="checkPastDueForm">
<label for="bookIDCheck">Book ID:</label>
<input type="text" id="bookIDCheck" required /><br />
<button type="submit">Check Past Due</button>
</form>
<script>
// Book class
class Book {
constructor(
id,
title,
author,
genre,
pageCount,
publisherID,
ISBN,
checkoutDate,
checkoutMember,
dueReturnDate,
availability
) {
this.id = id;
this.title = title;
this.author = author;
this.genre = genre;
this.pageCount = pageCount;
this.publisherID = publisherID;
this.ISBN = ISBN;
this.checkoutDate = checkoutDate;
this.checkoutMember = checkoutMember;
this.dueReturnDate = dueReturnDate;
this.availability = availability;
}
getTitle() {
return this.title;
}
getAuthor() {
return this.author;
}
getISBN() {
return this.ISBN;
}
updateCheckoutStatus(
bookID,
newStatus,
checkoutDate,
checkoutMember,
newReturnDate
) {
this.id = bookID;
this.availability = newStatus;
this.checkoutDate = checkoutDate;
this.checkoutMember = checkoutMember;
this.dueReturnDate = newReturnDate;
}
extendCheckoutPeriod(bookID, newReturnDate) {
this.id = bookID;
this.dueReturnDate = newReturnDate;
}
isPastDue(bookID) {
const currentDate = new Date();
return (
currentDate.getTime() > Date.parse(this.dueReturnDate)
);
}
}
// Book Record Manager Singleton
const bookRecordDatabase = {};
class BookRecordManager {
addBookRecord({
id,
title,
author,
genre,
pageCount,
publisherID,
ISBN,
checkoutDate,
checkoutMember,
dueReturnDate,
availability
}) {
const book = new Book(
id,
title,
author,
genre,
pageCount,
publisherID,
ISBN,
checkoutDate,
checkoutMember,
dueReturnDate,
availability
);
console.log(book);
bookRecordDatabase[id] = {
checkoutMember,
checkoutDate,
dueReturnDate,
availability,
book
};
}
updateCheckoutStatus({
bookID,
newStatus,
checkoutDate,
checkoutMember,
newReturnDate
}) {
const record = bookRecordDatabase[bookID];
record.availability = newStatus;
record.checkoutDate = checkoutDate;
record.checkoutMember = checkoutMember;
record.dueReturnDate = newReturnDate;
}
extendCheckoutPeriod(bookID, newReturnDate) {
bookRecordDatabase[bookID].dueReturnDate = newReturnDate;
}
isPastDue(bookID) {
const currentDate = new Date();
return (
currentDate.getTime() > Date.parse(bookRecordDatabase[bookID].dueReturnDate)
);
}
}
// Book Factory Singleton
const bookFactory = {
createBook({
id,
title,
author,
genre,
pageCount,
publisherID,
ISBN
}) {
const existingBook = bookRecordDatabase[id];
if (existingBook) {
return existingBook.book;
} else {
const book = new Book(
id,
title,
author,
genre,
pageCount,
publisherID,
ISBN
);
bookRecordDatabase[id] = {
checkoutMember: null,
checkoutDate: null,
dueReturnDate: null,
availability: true,
book
};
return book;
}
}
};
// Event listeners
const addRecordForm = document.getElementById('addRecordForm');
const updateStatusForm = document.getElementById('updateStatusForm');
const extendPeriodForm = document.getElementById('extendPeriodForm');
const checkPastDueForm = document.getElementById('checkPastDueForm');
const output = document.getElementById('output');
addRecordForm.addEventListener('submit', (event) => {
event
event.preventDefault();
const id = Math.random().toString(36).substr(2, 9);
const title = addRecordForm.elements.title.value;
const author = addRecordForm.elements.author.value;
const genre = addRecordForm.elements.genre.value;
const pageCount = parseInt(addRecordForm.elements.pageCount.value);
const publisherID = addRecordForm.elements.publisherID.value;
const ISBN = addRecordForm.elements.ISBN.value;
bookRecordManager.addBookRecord({
id,
title,
author,
genre,
pageCount,
publisherID,
ISBN,
checkoutDate: null,
checkoutMember: null,
dueReturnDate: null,
availability: true
});
output.innerHTML = 'Book record added successfully.';
addRecordForm.reset();
});
updateStatusForm.addEventListener('submit', (event) => {
event.preventDefault();
const bookID = updateStatusForm.elements.bookID.value;
const newStatus = updateStatusForm.elements.newStatus.value === 'true';
const checkoutDate = updateStatusForm.elements.checkoutDate.value;
const checkoutMember = updateStatusForm.elements.checkoutMember.value;
const newReturnDate = updateStatusForm.elements.newReturnDate.value;
bookRecordManager.updateCheckoutStatus({
bookID,
newStatus,
checkoutDate,
checkoutMember,
newReturnDate
});
output.innerHTML = 'Checkout status updated successfully.';
updateStatusForm.reset();
});
extendPeriodForm.addEventListener('submit', (event) => {
event.preventDefault();
const bookID = extendPeriodForm.elements.bookIDExtend.value;
const newReturnDate = extendPeriodForm.elements.newReturnDateExtend.value;
bookRecordManager.extendCheckoutPeriod(bookID, newReturnDate);
output.innerHTML = 'Checkout period extended successfully.';
extendPeriodForm.reset();
});
checkPastDueForm.addEventListener('submit', (event) => {
event.preventDefault();
const bookID = checkPastDueForm.elements.bookIDCheck.value;
const isPastDue = bookRecordManager.isPastDue(bookID);
if (isPastDue) {
output.innerHTML = 'The book is past due.';
} else {
output.innerHTML = 'The book is not past due.';
}
checkPastDueForm.reset();
});
// Create a BookRecordManager instance
const bookRecordManager = new BookRecordManager();
</script>
</body>
</html>