Skip to content

Commit b9bb03d

Browse files
Merge pull request #2 from EmilianoAngel/solutions
#26 - Dart
2 parents e523938 + 1c2e47a commit b9bb03d

File tree

1 file changed

+150
-1
lines changed

1 file changed

+150
-1
lines changed
Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,150 @@
1-
// test
1+
// Incorrecto
2+
// class User {
3+
// String name;
4+
// String email;
5+
6+
// User(this.name, this.email);
7+
8+
// void saveToDatabase(String name, String email) {
9+
// print('User $name with email $email saved to database.');
10+
// }
11+
12+
// void sendWelcomeEmail(String email) {
13+
// print('Welcome email sent to $email');
14+
// }
15+
16+
// }
17+
18+
// Corecto
19+
20+
class User {
21+
String name;
22+
String email;
23+
24+
User(this.name, this.email);
25+
}
26+
27+
class UserSeervice {
28+
void saveToDatabase(String name, String email) {
29+
print('User $name with email $email saved to database.');
30+
}
31+
}
32+
33+
class EmailService {
34+
void sendWelcomeEmail(String email) {
35+
print('Welcome email sent to $email');
36+
}
37+
}
38+
39+
// Ejercicio de biblioteca
40+
41+
// Icorrecto
42+
// class Biblioteca {
43+
// List<Map<String, dynamic>> books = [];
44+
// List<Map<String, dynamic>> users = [];
45+
// List<String> loans = [];
46+
47+
// void addBook(String title, String author, int copies) {
48+
// books.add({'title': title, 'author': author, 'copies': copies});
49+
// print('Book "$title" added to the library.');
50+
// }
51+
52+
// void addUser(String name, String userId, int email) {
53+
// users.add({'name': name, 'userId': userId, 'email': email});
54+
// print('User "$userId" added to the database.');
55+
// }
56+
57+
// void loanBook(String userId, String bookTitle) {
58+
// var user = users.firstWhere((u) => u['userId'] == userId, orElse: () => {});
59+
// var book = books.firstWhere((b) => b['title'] == bookTitle, orElse: () => {});
60+
61+
// if (user.isEmpty) {
62+
// print('User not found.');
63+
// return;
64+
// }
65+
66+
// if (book.isEmpty || book['copies'] <= 0) {
67+
// print('Book not available.');
68+
// return;
69+
// }
70+
71+
// book['copies'] -= 1;
72+
// loans.add('User $userId loaned "$bookTitle"');
73+
// print('Book "$bookTitle" loaned to user $userId.');
74+
// }
75+
76+
// void returnBook(String userId, String bookTitle) {
77+
// var book = books.firstWhere((b) => b['title'] == bookTitle, orElse: () => {});
78+
79+
// if (book.isEmpty) {
80+
// print('Book not found in the library.');
81+
// return;
82+
// }
83+
84+
// book['copies'] += 1;
85+
// loans.remove('User $userId loaned "$bookTitle"');
86+
// print('Book "$bookTitle" returned by user $userId.');
87+
// }
88+
// }
89+
90+
// Correcto
91+
class Book {
92+
String title;
93+
String author;
94+
int copies;
95+
96+
Book(this.title, this.author, this.copies);
97+
}
98+
99+
class LibraryUser {
100+
String name;
101+
String userId;
102+
String email;
103+
104+
LibraryUser(this.name, this.userId, this.email);
105+
}
106+
107+
class Loan {
108+
List<Map<String, dynamic>> loans = [];
109+
110+
void loanBook(LibraryUser user, Book book) {
111+
if (book.copies <= 0) {
112+
print('Book not available.');
113+
return;
114+
}
115+
116+
book.copies -= 1;
117+
loans.add({'userId': user.userId, 'bookTitle': book.title});
118+
print('Book "${book.title}" loaned to user ${user.userId}.');
119+
}
120+
121+
void returnBook(LibraryUser user, Book book) {
122+
book.copies += 1;
123+
loans.removeWhere((loan) => loan['userId'] == user.userId && loan['bookTitle'] == book.title);
124+
print('Book "${book.title}" returned by user ${user.userId}.');
125+
}
126+
}
127+
128+
class Library {
129+
List<Book> books = [];
130+
List<LibraryUser> users = [];
131+
Loan loanService = Loan();
132+
133+
void addBook(Book book) {
134+
books.add(book);
135+
print('Book "$book.title" added to the library.');
136+
}
137+
138+
void addUser(LibraryUser user) {
139+
users.add(user);
140+
print('User "${user.userId}" added to the database.');
141+
}
142+
143+
loanBook(LibraryUser user, Book book) {
144+
loanService.loanBook(user, book);
145+
}
146+
147+
returnBook(LibraryUser user, Book book) {
148+
loanService.returnBook(user, book);
149+
}
150+
}

0 commit comments

Comments
 (0)