Skip to content

Commit 6e279ee

Browse files
committed
#26 - JavaScript
1 parent dcaebbd commit 6e279ee

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Invalid way
2+
class User {
3+
constructor(name, email) {
4+
this.name = name;
5+
this.email = email;
6+
}
7+
8+
getUserInfo() {
9+
return {
10+
name: this.name,
11+
email: this.email
12+
};
13+
}
14+
15+
formatUserInfo() {
16+
return `Name: ${this.name}, Email: ${this.email}`;
17+
}
18+
}
19+
20+
const user = new User('Isaac Morales', '[email protected]');
21+
console.log(user.formatUserInfo());
22+
23+
// Correct way
24+
class UserInfo {
25+
constructor(name, email) {
26+
this.name = name;
27+
this.email = email;
28+
}
29+
30+
getUserInfo() {
31+
return {
32+
name: this.name,
33+
email: this.email
34+
};
35+
}
36+
}
37+
38+
class FormatUser {
39+
static format(user) {
40+
return `Name: ${user.name}, Email: ${user.email}`;
41+
}
42+
}
43+
44+
const newUser = new UserInfo('Isaac Cortés', '[email protected]');
45+
console.log(FormatUser.format(newUser));
46+
47+
// Extra Exercise //
48+
class BookManager {
49+
constructor() {
50+
this.books = [];
51+
}
52+
53+
addBook(title, author, copies) {
54+
this.books.push({ title, author, copies });
55+
}
56+
57+
findBook(title) {
58+
return this.books.find(book => book.title === title);
59+
}
60+
}
61+
62+
class UserManager {
63+
constructor() {
64+
this.users = [];
65+
}
66+
67+
addUser(name, id, email) {
68+
this.users.push({ name, id, email });
69+
}
70+
71+
findUser(id) {
72+
return this.users.find(user => user.id === id);
73+
}
74+
}
75+
76+
class LoanManager {
77+
constructor(bookManager, userManager) {
78+
this.bookManager = bookManager;
79+
this.userManager = userManager;
80+
this.loans = [];
81+
}
82+
83+
borrowBook(userId, bookTitle) {
84+
const user = this.userManager.findUser(userId);
85+
const book = this.bookManager.findBook(bookTitle);
86+
87+
if (user && book && book.copies > 0) {
88+
book.copies--;
89+
this.loans.push({ userId, bookTitle });
90+
console.log(`${user.name} borrowed "${book.title}"`);
91+
} else {
92+
console.log(`Book is unavailable or user not found`);
93+
}
94+
}
95+
96+
returnBook(userId, bookTitle) {
97+
const loanIndex = this.loans.findIndex(
98+
loan => loan.userId === userId && loan.bookTitle === bookTitle
99+
);
100+
const book = this.bookManager.findBook(bookTitle);
101+
102+
if (loanIndex !== -1 && book) {
103+
book.copies++;
104+
this.loans.splice(loanIndex, 1);
105+
console.log(`"${book.title}" has been returned`);
106+
} else {
107+
console.log('Loan not found or book not available');
108+
}
109+
}
110+
}
111+
112+
const bookManager = new BookManager();
113+
const userManager = new UserManager();
114+
const loanManager = new LoanManager(bookManager, userManager);
115+
116+
bookManager.addBook('The Great Gatsby', 'F. Scott Fitzgerald', 5);
117+
userManager.addUser('Isaac', 1, '[email protected]');
118+
119+
loanManager.borrowBook(1, 'The Great Gatsby');
120+
loanManager.returnBook(1, 'The Great Gatsby');

0 commit comments

Comments
 (0)