DOM EVENT domaci za Dan 21 OBJEKTI KNJIGE
Domaci-objekti
Zahtevi:
Prikaz podataka o knjigama: Napišite metodu koja vraća niz naslova svih
knjiga dostupnih u biblioteci.
Filtriranje dostupnih knjiga: Napišite metodu koja vraća sve knjige koje
imaju više od 3 primerka dostupnih u biblioteci.
Provera statusa članstva: Napišite metodu koja proverava da li je
određeni član biblioteke sa zadatim memberId još uvek aktivan.
Dodavanje nove knjige: Napišite metodu koja dodaje novu knjigu u
biblioteku. Metoda treba da prihvati parametre
za bookId, title, author, genres, i copies. Nakon dodavanja knjige, metoda
treba da ažurira objekat library.
Ažuriranje broja primeraka: Napišite metodu koja ažurira broj primeraka
knjige sa zadatim bookId. Ako knjiga već postoji u biblioteci, funkcija treba
da poveća broj primeraka za zadatu vrednost. Ako knjiga ne postoji,
funkcija treba da vrati poruku da knjiga nije pronađena.
Transformacija podataka o članovima: Napišite metodu koja kreira novi
niz objekata sa podacima o članovima, ali samo
sa memberId i name poljima.
Ukupno primeraka knjiga: Napišite metodu koja vraća ukupan broj
primeraka svih knjiga u biblioteci.
Prikaz aktivnih članova: Napišite metodu koja vraća niz imena svih
članova čije je članstvo aktivno.
Proveravanje knjiga koje nisu vraćene: Napišite metodu koja vraća niz
naslova knjiga koje nisu vraćene u biblioteku.
Kreiranje novog objekta sa određenim podacima: Napišite metodu koja
vraća novi objekat sa informacijama o biblioteci, ali samo sa podacima
o name, location, i broju članova sa aktivnim članstvom.
Dodatni zahtevi (nisu obavezni):
Loš način korišćenja metoda: Metode koje imaju parametre dodatno
proširite logikom u slučaju lošeg prosleđivanja argumenta ili izostanka
istog.
Nedostatak podataka: Ukoliko zahtevamo neki podatak koji ne postoji,
nemojte samo da vratite praznu listu, objekat ili slično.
U nastavku je objekat sa kojim cete raditi.
const library = {
name: "Central City Library",
location: {
street: "Library Avenue 5",
city: "Metropolis",
state: "NY",
postalCode: "10001",
},
books: [
{
bookId: "B001",
title: "JavaScript: The Good Parts",
author: "Douglas Crockford",
genres: ["Programming", "Technology"],
copies: 5,
borrowed: [
{ memberId: "M001", date: "2023-07-14", returned: false },
{ memberId: "M002", date: "2023-07-20", returned: true },
],
},
{
bookId: "B002",
title: "Clean Code",
author: "Robert C. Martin",
genres: ["Programming", "Best Practices"],
copies: 3,
borrowed: [{ memberId: "M003", date: "2023-08-01", returned: true }],
},
{
bookId: "B003",
title: "The Pragmatic Programmer",
author: "Andrew Hunt",
genres: ["Programming", "Development"],
copies: 4,
borrowed: [],
},
],
members: [
{
memberId: "M001",
name: "John Doe",
email: "john.doe@example.com",
membershipActive: true,
},
{
memberId: "M002",
name: "Jane Smith",
email: "jane.smith@example.com",
membershipActive: false,
},
{
memberId: "M003",
name: "Emily Johnson",
email: "emily.johnson@example.com",
membershipActive: true,
},
],
const library = {
name: "Central City Library",
location: {
street: "Library Avenue 5",
city: "Metropolis",
state: "NY",
postalCode: "10001",
},
books: [
{
bookId: "B001",
title: "JavaScript: The Good Parts",
author: "Douglas Crockford",
genres: ["Programming", "Technology"],
copies: 5,
borrowed: [
{ memberId: "M001", date: "2023-07-14", returned: false },
{ memberId: "M002", date: "2023-07-20", returned: true },
],
},
{
bookId: "B002",
title: "Clean Code",
author: "Robert C. Martin",
genres: ["Programming", "Best Practices"],
copies: 3,
borrowed: [{ memberId: "M003", date: "2023-08-01", returned: true }],
},
{
bookId: "B003",
title: "The Pragmatic Programmer",
author: "Andrew Hunt",
genres: ["Programming", "Development"],
copies: 4,
borrowed: [],
},
],
members: [
{
memberId: "M001",
name: "John Doe",
email: "john.doe@example.com",
membershipActive: true,
},
{
memberId: "M002",
name: "Jane Smith",
email: "jane.smith@example.com",
membershipActive: false,
},
{
memberId: "M003",
name: "Emily Johnson",
email: "emily.johnson@example.com",
membershipActive: true,
},
],
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="main.js" defer></script>
</head>
<body></body>
</html>
"use strict";
const library = {
name: "Central City Library",
location: {
street: "Library Avenue 5",
city: "Metropolis",
state: "NY",
postalCode: "10001",
},
books: [
{
bookId: "B001",
title: "JavaScript: The Good Parts",
author: "Douglas Crockford",
genres: ["Programming", "Technology"],
copies: 5,
borrowed: [
{ memberId: "M001", date: "2023-07-14", returned: false },
{ memberId: "M002", date: "2023-07-20", returned: true },
],
},
{
bookId: "B002",
title: "Clean Code",
author: "Robert C. Martin",
genres: ["Programming", "Best Practices"],
copies: 3,
borrowed: [{ memberId: "M003", date: "2023-08-01", returned: true }],
},
{
bookId: "B003",
title: "The Pragmatic Programmer",
author: "Andrew Hunt",
genres: ["Programming", "Development"],
copies: 4,
borrowed: [],
},
],
members: [
{
memberId: "M001",
name: "John Doe",
email: "john.doe@example.com",
membershipActive: true,
},
{
memberId: "M002",
name: "Jane Smith",
email: "jane.smith@example.com",
membershipActive: false,
},
{
memberId: "M003",
name: "Emily Johnson",
email: "emily.johnson@example.com",
membershipActive: true,
},
],
allBooksTitles: function () {
return this.books.map((book) => book.title);
//II nacin
// function filteredByTitle(book) {
// return book.title;
// }
// return this.books.map(filteredByTitle);
},
moreThanThreeBookCopies: function (n) {
// I nacin - vraca ceo obj book
// return this.books
// .filter((book) => book.copies > n)
//I nacin - vraca samo naslove
// return this.books
// .filter((book) => book.copies > n)
// .map((book) => book.title);
//II nacin
const result = [];
for (let book of this.books) {
if (book.copies > n) {
result.push(book);
}
}
return result;
// return result.map((book) => book.title);
},
isMemberActiv: function (id) {
const member = this.members.find((clan) => clan.memberId === id);
console.log(member);
if (member) {
return `Is membership active for member ${member.name}: ${member.membershipActive}`;
} else {
return `This member is not found!`;
}
},
addNewBook(bookId, title, author, genres, copies) {
return this.books.push({
bookId: bookId,
title: title,
author: author,
genres: genres,
copies: copies,
borrowed: [],
});
},
updateBookCopies: function (id, numOfCopies) {
const book = this.books.find((book) => book.bookId === id);
book ? (book.copies += numOfCopies) : alert("Book is not found!");
},
transformedMembers() {
return this.members.map((member) => {
return {
userId: member.memberId,
userName: member.name,
};
});
},
allBooksCopies() {
// return this.books
// .map((book) => book.copies)
// .reduce((acc, curr) => acc + curr, 0);
//II nacin
let zbir = 0;
for (const book of this.books) {
zbir += book.copies;
}
return zbir;
},
getAllActiveMembers: function () {
return this.members
.filter((member) => member.membershipActive)
.map((member) => member.name);
},
getAllUnreturnedBooks() {
const result = [];
// this.books.forEach((book) => {
// if (book.borrowed.length) {
// book.borrowed.forEach((obj) => {
// if (!obj.returned) result.push(book.title);
// });
// }
// });
// return result;
//II nacin
for (const book of this.books) {
if (book.borrowed.length) {
for (const obj of book.borrowed) {
if (obj.returned) {
console.log(book.title, "is returned");
} else {
result.push(book.title);
}
}
}
}
return result;
},
getLibraryInfo: function () {
return {
libraryName: this.name,
libraryLocation: this.location,
activeMembers: this.getAllActiveMembers().length,
};
},
};
console.log(library.allBooksTitles());
console.log(library.moreThanThreeBookCopies(3));
console.log(library.isMemberActiv("M003"));
library.addNewBook("B005", "How to write C", "John Simons", ["Programming"], 8);
console.log(library);
library.updateBookCopies("B002", 5);
console.log(library);
console.log(library.transformedMembers());
console.log(library.allBooksCopies());
console.log(library.getAllActiveMembers());
console.log(library.getAllUnreturnedBooks());
console.log(library.getLibraryInfo());
"use strict";
const library = {
name: "Central City Library",
location: {
street: "Library Avenue 5",
city: "Metropolis",
state: "NY",
postalCode: "10001",
},
books: [
{
bookId: "B001",
title: "JavaScript: The Good Parts",
author: "Douglas Crockford",
genres: ["Programming", "Technology"],
copies: 5,
borrowed: [
{ memberId: "M001", date: "2023-07-14", returned: false },
{ memberId: "M002", date: "2023-07-20", returned: true },
],
},
{
bookId: "B002",
title: "Clean Code",
author: "Robert C. Martin",
genres: ["Programming", "Best Practices"],
copies: 3,
borrowed: [{ memberId: "M003", date: "2023-08-01", returned: true }],
},
{
bookId: "B003",
title: "The Pragmatic Programmer",
author: "Andrew Hunt",
genres: ["Programming", "Development"],
copies: 4,
borrowed: [],
},
],
members: [
{
memberId: "M001",
name: "John Doe",
email: "john.doe@example.com",
membershipActive: true,
},
{
memberId: "M002",
name: "Jane Smith",
email: "jane.smith@example.com",
membershipActive: false,
},
{
memberId: "M003",
name: "Emily Johnson",
email: "emily.johnson@example.com",
membershipActive: true,
},
],
allBooksTitles: function () {
return this.books.map((book) => book.title);
//II nacin
// function filteredByTitle(book) {
// return book.title;
// }
// return this.books.map(filteredByTitle);
},
moreThanThreeBookCopies: function (n) {
// I nacin - vraca ceo obj book
// return this.books
// .filter((book) => book.copies > n)
//I nacin - vraca samo naslove
// return this.books
// .filter((book) => book.copies > n)
// .map((book) => book.title);
//II nacin
const result = [];
for (let book of this.books) {
if (book.copies > n) {
result.push(book);
}
}
return result;
// return result.map((book) => book.title);
},
isMemberActiv: function (id) {
const member = this.members.find((clan) => clan.memberId === id);
console.log(member);
if (member) {
return `Is membership active for member ${member.name}: ${member.membershipActive}`;
} else {
return `This member is not found!`;
}
},
addNewBook(bookId, title, author, genres, copies) {
return this.books.push({
bookId: bookId,
title: title,
author: author,
genres: genres,
copies: copies,
borrowed: [],
});
},
updateBookCopies: function (id, numOfCopies) {
const book = this.books.find((book) => book.bookId === id);
book ? (book.copies += numOfCopies) : alert("Book is not found!");
},
transformedMembers() {
return this.members.map((member) => {
return {
userId: member.memberId,
userName: member.name,
};
});
},
allBooksCopies() {
// return this.books
// .map((book) => book.copies)
// .reduce((acc, curr) => acc + curr, 0);
//II nacin
let zbir = 0;
for (const book of this.books) {
zbir += book.copies;
}
return zbir;
},
getAllActiveMembers: function () {
return this.members
.filter((member) => member.membershipActive)
.map((member) => member.name);
},
getAllUnreturnedBooks() {
const result = [];
// this.books.forEach((book) => {
// if (book.borrowed.length) {
// book.borrowed.forEach((obj) => {
// if (!obj.returned) result.push(book.title);
// });
// }
// });
// return result;
//II nacin
for (const book of this.books) {
if (book.borrowed.length) {
for (const obj of book.borrowed) {
if (obj.returned) {
console.log(book.title, "is returned");
} else {
result.push(book.title);
}
}
}
}
return result;
},
getLibraryInfo: function () {
return {
libraryName: this.name,
libraryLocation: this.location,
activeMembers: this.getAllActiveMembers().length,
};
},
};
console.log(library.allBooksTitles());
console.log(library.moreThanThreeBookCopies(3));
console.log(library.isMemberActiv("M003"));
library.addNewBook("B005", "How to write C", "John Simons", ["Programming"], 8);
console.log(library);
library.updateBookCopies("B002", 5);
console.log(library);
console.log(library.transformedMembers());
console.log(library.allBooksCopies());
console.log(library.getAllActiveMembers());
console.log(library.getAllUnreturnedBooks());
console.log(library.getLibraryInfo());