Skip to content

Commit 757051a

Browse files
authored
Merge pull request #8920 from EmilianoAngel/main
#26 - Dart
2 parents 4c3a6a5 + 1bdd3ed commit 757051a

File tree

2 files changed

+269
-0
lines changed

2 files changed

+269
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Incorrecto
2+
// class Form {
3+
4+
// void drawSquare() {
5+
// print('Dibujar un cuadrado');
6+
// }
7+
8+
// void drawCircle() {
9+
// print('Dibujar un círculo');
10+
// }
11+
// }
12+
13+
// Correcto
14+
abstract class Form {
15+
void draw();
16+
}
17+
18+
class Square implements Form {
19+
@override
20+
void draw() {
21+
print('Dibujar un cuadrado');
22+
}
23+
}
24+
25+
class Circle implements Form {
26+
@override
27+
void draw() {
28+
print('Dibujar un círculo');
29+
}
30+
}
31+
32+
class Triangle implements Form {
33+
@override
34+
void draw() {
35+
print('Dibujar un triángulo');
36+
}
37+
}
38+
39+
// Ejercicio de calculadora
40+
41+
abstract class Operation {
42+
double execute(double a, double b) {
43+
return 0;
44+
}
45+
}
46+
47+
class Addition implements Operation {
48+
@override
49+
double execute(double a, double b) {
50+
return a + b;
51+
}
52+
}
53+
54+
class Subtraction implements Operation {
55+
@override
56+
double execute(double a, double b) {
57+
return a - b;
58+
}
59+
}
60+
61+
class Multiplication implements Operation {
62+
@override
63+
double execute(double a, double b) {
64+
return a * b;
65+
}
66+
}
67+
68+
class Division implements Operation {
69+
@override
70+
double execute(double a, double b) {
71+
if (b == 0) {
72+
throw Exception('Division by zero is not allowed.');
73+
}
74+
return a / b;
75+
}
76+
}
77+
78+
class Power implements Operation {
79+
@override
80+
double execute(double a, double b) {
81+
double result = 1;
82+
for(int i = 0; i < b; i++) {
83+
result *= a;
84+
}
85+
return result;
86+
}
87+
}
88+
89+
class Calculator {
90+
final Map<String, dynamic> _operations = {};
91+
92+
void addOperation(String name, Operation operation) {
93+
_operations[name] = operation;
94+
}
95+
double calculate(String name, double a, double b) {
96+
final operation = _operations[name];
97+
if (operation == null) {
98+
throw ArgumentError('Operation $name not supported.');
99+
}
100+
return operation.execute(a,b);
101+
}
102+
}
103+
104+
void main () {
105+
Calculator calculator = Calculator();
106+
107+
calculator.addOperation('addition', Addition());
108+
calculator.addOperation('substraction', Subtraction());
109+
calculator.addOperation('multiplication', Multiplication());
110+
calculator.addOperation('division', Division());
111+
calculator.addOperation('power', Power());
112+
113+
print(calculator.calculate('addition', 5, 3));
114+
print(calculator.calculate('substraction', 6, 3));
115+
print(calculator.calculate('multiplication', 5, 2));
116+
print(calculator.calculate('division', 12, 6));
117+
print(calculator.calculate('power', 2, 3));
118+
119+
}

0 commit comments

Comments
 (0)