-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (115 loc) · 3.3 KB
/
index.js
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
/* eslint-disable max-classes-per-file */
/* eslint-disable no-unused-vars */
/* eslint-disable no-console */
class Library {
constructor() {
this.books = [];
this.users = [];
this.loans = [];
}
addBook(title, author, copies) {
this.books.push({ title, author, copies });
}
addUser(name, id, email) {
this.users.push({ name, id, email });
}
loanBook(userId, bookTitle) {
const user = this.users.find((u) => u.id === userId);
const book = this.books.find((b) => b.title === bookTitle);
if (user && book && book.copies > 0) {
this.loans.push({ userId, bookTitle, date: new Date() });
book.copies -= 1;
} else {
console.log('No se puede procesar el préstamo.');
}
}
returnBook(userId, bookTitle) {
const loanIndex = this.loans.findIndex(
(loan) => loan.userId === userId && loan.bookTitle === bookTitle,
);
if (loanIndex > -1) {
this.loans.splice(loanIndex, 1);
const book = this.books.find((b) => b.title === bookTitle);
book.copies += 1;
} else {
console.log('No se puede procesar la devolución.');
}
}
}
// Dificultad extra
// Clase para gestionar libros
class Book {
constructor(title, author, copies) {
this.title = title;
this.author = author;
this.copies = copies;
}
}
class BookManager {
constructor() {
this.books = [];
}
addBook(title, author, copies) {
this.books.push(new Book(title, author, copies));
}
findBook(title) {
return this.books.find((book) => book.title === title);
}
}
// Clase para gestionar usuarios
class User {
constructor(name, id, email) {
this.name = name;
this.id = id;
this.email = email;
}
}
class UserManager {
constructor() {
this.users = [];
}
addUser(name, id, email) {
this.users.push(new User(name, id, email));
}
findUser(id) {
return this.users.find((user) => user.id === id);
}
}
// Clase para gestionar préstamos
class LoanManager {
constructor() {
this.loans = [];
}
loanBook(userManager, bookManager, userId, bookTitle) {
const user = userManager.findUser(userId);
const book = bookManager.findBook(bookTitle);
if (user && book && book.copies > 0) {
this.loans.push({ userId, bookTitle, date: new Date() });
book.copies -= 1;
console.log(`Préstamo procesado: ${bookTitle} para ${user.name}`);
} else {
console.log('No se puede procesar el préstamo.');
}
}
returnBook(userManager, bookManager, userId, bookTitle) {
const loanIndex = this.loans.findIndex(
(loan) => loan.userId === userId && loan.bookTitle === bookTitle,
);
if (loanIndex > -1) {
this.loans.splice(loanIndex, 1);
const book = bookManager.findBook(bookTitle);
book.copies += 1;
console.log(`Devolución procesada: ${bookTitle} de ${userId}`);
} else {
console.log('No se puede procesar la devolución.');
}
}
}
// Uso del sistema refactorizado
const bookManager = new BookManager();
const userManager = new UserManager();
const loanManager = new LoanManager();
bookManager.addBook('El Quijote', 'Miguel de Cervantes', 3);
userManager.addUser('Juan Pérez', 1, 'juan.perez@example.com');
loanManager.loanBook(userManager, bookManager, 1, 'El Quijote');
loanManager.returnBook(userManager, bookManager, 1, 'El Quijote');