Skip to content

Commit 538665f

Browse files
committed
#26 - Javascript
1 parent 3fffe68 commit 538665f

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

Roadmap/26 - SOLID SRP/javascript/parababire.js

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class User {
116116
this.mail = mail
117117
}
118118
}
119-
119+
// Gestión de prestamos
120120
class Loans {
121121
constructor() {
122122
this.loans = []
@@ -150,41 +150,40 @@ class Library {
150150
constructor() {
151151
this.books = []
152152
this.users = []
153-
this.loans = Loans()
153+
this.loans_service = new Loans()
154154
}
155155
add_book (book) {
156156
return this.books.push(book)
157157
}
158158
add_user (user) {
159159
return this.users.push(user)
160160
}
161-
loan_book (user_id, book_title) { // Pulir
162-
for (const book of this.books) {
163-
if (book.Title == book_title && book.Copies > 0) {
164-
book.Copies -= 1
165-
this.loans.push({
166-
User_Id: user_id,
167-
Book_Title: book_title
168-
})
169-
return true
170-
}
171-
return false
161+
loan_book (user_id, book_title) {
162+
let user = this.users.find((user) => user.id == user_id)
163+
let book = this.books.find((book) => book.title == book_title)
164+
if (user && book) {
165+
return this.loans_service.loan_book(user, book)
172166
}
167+
return false
173168
}
169+
174170
return_book (user_id, book_title) {
175-
for (const loan of this.loans) {
176-
if (loan.User_Id == user_id && loan.Book_Title == book_title) {
177-
this.loans = this.loans.filter(function (el) {
178-
return el.User_Id != user_id
179-
})
180-
for (const book of this.books) {
181-
if (book.Title == book_title) {
182-
book.Copies += 1
183-
}
184-
}
185-
return true
186-
}
187-
return false
171+
let user = this.users.find((user) => user.id == user_id)
172+
let book = this.books.find((book) => book.title == book_title)
173+
if (user && book) {
174+
return this.loans_service.return_book(user, book)
188175
}
176+
return false
189177
}
190-
}
178+
}
179+
180+
let someBook = new Book('100 Años de Soledad', 'Gabo', 10)
181+
let principalLibrary = new Library()
182+
principalLibrary.add_book(someBook)
183+
184+
let someUser = new User('Ángel', 15154142, 'anarvamon@gmail.com')
185+
principalLibrary.add_user(someUser)
186+
187+
principalLibrary.loan_book(15154142, '100 Años de Soledad')
188+
console.log(principalLibrary)
189+
console.log(principalLibrary.loans_service)

0 commit comments

Comments
 (0)