-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
237 lines (194 loc) · 6.57 KB
/
Copy pathmain.js
File metadata and controls
237 lines (194 loc) · 6.57 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
function counter() {
let count = 0;
function increment() {
count += 1;
return count;
}
return increment;
}
const generateId = counter();
function Book(title, author, category = "General", year = new Date().getFullYear()) {
this.id = generateId();
this.title = title;
this.author = author;
this.category = category;
this.year = year;
this.location = {shelf: "", floor: 1};
}
function createBook(title, author, category = "General", year = new Date().getFullYear()) {
return {
id: generateId(),
title,
author,
category,
year,
location : {
shelf: "",
floor: 1
}
};
}
const library = [];
// ...books: rest argument that accepts any number of book objects
function addBooks(...books) {
books.forEach(book => library.push(book));
}
// ...args: rest argument that accepts any number of arguments
function randomPicker(...args) {
// Generate a random index based on the length of the args array
return args[Math.floor(Math.random() * args.length)];
}
const logBook = function(book) {
console.log(`
--------------------------
ID: ${book.id}
Title: ${book.title}
Author: ${book.author}
Category: ${book.category}
Year: ${book.year}
Location : Floor ${book.location.floor}, Shelf ${book.location.shelf}
--------------------------`);
};
const getBookById = (id => library.find(book => book.id === id));
const getBooksOlderThan10Years = () => {
const currentYear = new Date().getFullYear();
return library.filter(book => currentYear - book.year > 10);
};
function getTitles() {
return library.map(book => book.title);
};
function filterByCategory(category) {
return library.filter(book => book.category.toLowerCase() === category.toLowerCase());
}
function countBooksByAuthor() {
return library.reduce((accumulator, book) => {
accumulator[book.author] = (accumulator[book.author] || 0) + 1;
return accumulator;
}, {});
}
function printAllBooks() {
console.log(`All Books in the Library`);
console.log(`=========================`);
library.forEach(book => logBook(book));
console.log(`=========================`);
}
function searchBooks(keyword) {
const loweredKeyword = keyword.toLowerCase();
return library.filter(book => book.title.toLowerCase().includes(loweredKeyword) ||
book.author.toLowerCase().includes(loweredKeyword)
);
}
function libraryStats() {
const totalBooks = library.length;
function getDetails() {
const authors = [...new Set(library.map(book => book.author))].join(", ");
const categories = [...new Set(library.map(book => book.category))].join(", ");
function printDetails() {
console.log(`Total Books: ${totalBooks}`);
console.log(`Authors: ${authors}`);
console.log(`Categories: ${categories}`);
}
printDetails();
}
getDetails();
}
const LibraryManagerPrototype = {
info() {
console.log(`Library Manager: ${this.name}, Managed Since: ${this.since}`);
}
};
const LibraryManager = Object.create(LibraryManagerPrototype);
LibraryManager.name = "Ahmed Ali";
LibraryManager.since = 2010;
const defaultSettings = {
maxBooksPerUser: 3,
loanDurationDays: 14,
theme: "light",
language: "English"
}
function applySettings(userSettings = {}) {
return Object.assign({}, defaultSettings, userSettings);
}
console.log("================ Library Management System ================");
console.log("Create Book using createBook() function:");
const book1 = createBook("Romeo and Juliet", "Shakespeare", "Drama",1597);
// access properties using Dot Notation
book1.location.shelf = "B2";
book1.location.floor = 2;
console.log("Create Book using new with Book() Constructor:");
const book2 = new Book("Harry Potter", "J.K. Rowling", "Fantasy", 1997);
// access properties using Bracket Notation
book2["location"]["shelf"] = "A1";
console.log("Create Book using Object Literal Notation");
const book3 = {
id: generateId(),
title: "Math For Beginners",
author: "John Smith",
category: "Education",
year: 2010,
location: {shelf: "C1", floor: 3}
};
console.log("Create Book using Object.create()");
const book4 = Object.create(book3);
book4.id = generateId();
book4.title = "Learn JavaScript";
book4.year = 2015;
book4.location = {shelf: "C2", floor: 3};
console.log("Create Book using Object.assign()");
const book5 = Object.assign({}, book1, {
id : generateId(),
title: "Hamlet",
year: 1603,
location: {shelf: "B1", floor: 2}
});
const book6 = createBook("The Lion King", "Walt Disney", "Fantasy");
book6.location.shelf = "A1";
console.log("Adding all books to Library object");
addBooks(book1, book2, book3, book4, book5, book6);
// Print All Books;
printAllBooks();
console.log("Library Statistics");
libraryStats();
const randomBook = randomPicker(book1, book2, book3, book4, book5, book6);
console.log("Random Book Picked:");
logBook(randomBook);
// prompt user to enter book id, and search for a book with this id
let validatedId;
while(true) {
const rowId = prompt("Enter Book ID: ");
const cleanedId = (rowId ?? "").trim();
if(cleanedId === "") {
alert("Book ID cannot be empty. Re enter a valid value.");
continue;
}
const parsedId = Number(cleanedId);
if(isNaN(parsedId)) {
alert("Book ID must be a number. Re enter a valid value");
continue;
}
if(!Number.isInteger(parsedId)) {
alert("Book ID must be an integer value. Re enter a valid value");
continue;
}
validatedId = parsedId;
break;
}
console.log(`Search Result for Book ID ${validatedId}`);
const foundBook = getBookById(validatedId);
foundBook ? logBook(foundBook) : console.log(`Book is not found`);
console.log("Books Older than 10 years:");
const booksOlderThan10Years = getBooksOlderThan10Years();
booksOlderThan10Years.forEach(book => logBook(book));
console.log("All Books Titles:");
console.log(getTitles().join(", "));
console.log(`Books in "Education" Category:`);
filterByCategory("Education").forEach(book => logBook(book));
console.log("Book Count by Author:");
console.log(countBooksByAuthor());
console.log("Search Results for Author: John smith");
searchBooks("John smith").forEach(book => logBook(book));
// Print Library Manager Information;
LibraryManager.info();
const activeSettings = applySettings({maxBooksPerUser: 4, language: "Arabic"});
console.log("Active Library Settings: ");
console.log(activeSettings);