Skip to content

Commit e20567a

Browse files
authored
Merge pull request mouredev#7365 from danhingar/ejercicio26
#26 - Java
2 parents 30cdd60 + ceb915f commit e20567a

File tree

1 file changed

+310
-0
lines changed

1 file changed

+310
-0
lines changed
Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.Optional;
6+
7+
public class danhingar {
8+
9+
// Incorrecto
10+
public class User {
11+
12+
private String name;
13+
private String email;
14+
15+
public User(String name, String email) {
16+
this.email = email;
17+
this.name = name;
18+
}
19+
20+
public void save(User user) {
21+
22+
}
23+
24+
public void sendEmail(User user) {
25+
26+
}
27+
28+
}
29+
30+
// Correcto
31+
public class User1 {
32+
33+
private String name;
34+
private String email;
35+
36+
public User1(String name, String email) {
37+
this.name = name;
38+
this.email = email;
39+
}
40+
41+
}
42+
43+
public class UserService {
44+
45+
public void save(User1 user1) {
46+
47+
}
48+
49+
}
50+
51+
public class EmailService {
52+
53+
public void sendEmail(String email, String message) {
54+
55+
}
56+
}
57+
58+
public static void main(String[] args) {
59+
// WITHOUT SRP
60+
Library library = new Library();
61+
library.addUser(1, "Daniel", "[email protected]");
62+
library.addUser(2, "Pepe", "[email protected]");
63+
System.out.println(library.toString());
64+
library.addBook("Libro 1", "Autor 1", 5);
65+
library.addBook("Libro 2", "Autor 2", 3);
66+
System.out.println(library.toString());
67+
library.loanBook(1, "Libro 1");
68+
library.loanBook(2, "Libro 1");
69+
System.out.println(library.toString());
70+
library.returnBook(2, "Libro 1");
71+
System.out.println(library.toString());
72+
73+
// WITH SRP
74+
Library2 library2 = new Library2();
75+
library2.addUser(new User2(1, "Daniel", "[email protected]"));
76+
library2.addUser(new User2(2, "Pepe", "[email protected]"));
77+
System.out.println(library2.toString());
78+
library2.addBook(new Book("Libro 1", "Autor 1", 5));
79+
library2.addBook(new Book("Libro 2", "Autor 2", 3));
80+
System.out.println(library2.toString());
81+
library2.loanBook(1, "Libro 1");
82+
library2.loanBook(2, "Libro 1");
83+
System.out.println(library2.toString());
84+
library2.returnBook(2, "Libro 1");
85+
System.out.println(library2.toString());
86+
}
87+
88+
}
89+
90+
// Extra
91+
92+
// Incorrecto
93+
94+
class Library {
95+
private List<Map<String, Object>> users;
96+
private List<Map<String, Object>> books;
97+
private Map<Integer, List<String>> loans;
98+
99+
public Library() {
100+
this.users = new ArrayList<>();
101+
this.books = new ArrayList<>();
102+
this.loans = new HashMap<>();
103+
}
104+
105+
public void addBook(String title, String author, Integer copies) {
106+
Map<String, Object> book = new HashMap<>();
107+
book.put("title", title);
108+
book.put("author", author);
109+
book.put("copies", copies);
110+
books.add(book);
111+
}
112+
113+
public void addUser(Integer id, String name, String email) {
114+
Map<String, Object> user = new HashMap<>();
115+
user.put("id", id);
116+
user.put("name", name);
117+
user.put("email", email);
118+
users.add(user);
119+
}
120+
121+
public void loanBook(Integer userId, String bookTitle) {
122+
for (Map<String, Object> book : books) {
123+
if (book.get("title").equals(bookTitle) && (Integer) book.get("copies") > 0) {
124+
book.put("copies", (Integer) book.get("copies") - 1);
125+
List<String> booksUser = loans.get(userId) != null ? loans.get(userId) : new ArrayList<>();
126+
booksUser.add(bookTitle);
127+
loans.put(userId, booksUser);
128+
}
129+
}
130+
}
131+
132+
public void returnBook(Integer userId, String bookTitle) {
133+
boolean exist = this.loans.get(userId).stream().anyMatch(b -> b.equals(bookTitle));
134+
if (exist) {
135+
this.books.stream().filter(b -> b.get("title").equals(bookTitle))
136+
.forEach(b -> b.put("copies", (Integer) b.get("copies") + 1));
137+
this.loans.get(userId).remove(bookTitle);
138+
}
139+
}
140+
141+
@Override
142+
public String toString() {
143+
return "Library [users=" + users.toString() + ", books=" + books.toString() + ", loans=" + loans.toString() + "]";
144+
}
145+
}
146+
147+
// Correcto
148+
149+
class Library2 {
150+
151+
private List<User2> users;
152+
private List<Book> books;
153+
private Loan loans;
154+
155+
public Library2() {
156+
users = new ArrayList<>();
157+
books = new ArrayList<>();
158+
loans = new Loan();
159+
}
160+
161+
public void addBook(Book book) {
162+
books.add(book);
163+
}
164+
165+
public void addUser(User2 user) {
166+
users.add(user);
167+
}
168+
169+
public void loanBook(Integer userId, String bookTitle) {
170+
Optional<User2> user = users.stream().filter(u -> u.getId().equals(userId)).findFirst();
171+
Optional<Book> book = books.stream().filter(b -> b.getTitle().equals(bookTitle)).findFirst();
172+
if (user.isPresent() && book.isPresent()) {
173+
loans.loanBook(user.get(), book.get());
174+
}
175+
176+
}
177+
178+
public void returnBook(Integer userId, String bookTitle) {
179+
Optional<User2> user = users.stream().filter(u -> u.getId().equals(userId)).findFirst();
180+
Optional<Book> book = books.stream().filter(b -> b.getTitle().equals(bookTitle)).findFirst();
181+
if (user.isPresent() && book.isPresent()) {
182+
loans.returnBook(user.get(), book.get());
183+
}
184+
}
185+
186+
@Override
187+
public String toString() {
188+
return "Library [users=" + users + ", books=" + books.toString() + ", loans=" + loans + "]";
189+
}
190+
191+
}
192+
193+
class Book {
194+
195+
private String author;
196+
private String title;
197+
private Integer copies;
198+
199+
public Book(String title, String author, Integer copies) {
200+
this.title = title;
201+
this.author = author;
202+
this.copies = copies;
203+
}
204+
205+
public String getAuthor() {
206+
return author;
207+
}
208+
209+
public void setAuthor(String author) {
210+
this.author = author;
211+
}
212+
213+
public String getTitle() {
214+
return title;
215+
}
216+
217+
public void setTitle(String title) {
218+
this.title = title;
219+
}
220+
221+
public Integer getCopies() {
222+
return copies;
223+
}
224+
225+
public void setCopies(Integer copies) {
226+
this.copies = copies;
227+
}
228+
229+
@Override
230+
public String toString() {
231+
return "Book [author=" + author + ", title=" + title + ", copies=" + copies + "]";
232+
}
233+
234+
}
235+
236+
class User2 {
237+
private String name;
238+
private Integer id;
239+
private String email;
240+
241+
public User2(Integer id, String name, String email) {
242+
this.name = name;
243+
this.id = id;
244+
this.email = email;
245+
}
246+
247+
public String getName() {
248+
return name;
249+
}
250+
251+
public void setName(String name) {
252+
this.name = name;
253+
}
254+
255+
public Integer getId() {
256+
return id;
257+
}
258+
259+
public void setId(Integer id) {
260+
this.id = id;
261+
}
262+
263+
public String getEmail() {
264+
return email;
265+
}
266+
267+
public void setEmail(String email) {
268+
this.email = email;
269+
}
270+
271+
}
272+
273+
class Loan {
274+
275+
private Map<Integer, List<String>> loans;
276+
277+
public Loan() {
278+
this.loans = new HashMap<>();
279+
}
280+
281+
public void loanBook(User2 user, Book book) {
282+
if (book.getCopies() > 0) {
283+
book.setCopies(book.getCopies() - 1);
284+
List<String> booksUser = loans.get(user.getId()) != null ? loans.get(user.getId()) : new ArrayList<>();
285+
booksUser.add(book.getTitle());
286+
loans.put(user.getId(), booksUser);
287+
}
288+
289+
}
290+
291+
public void returnBook(User2 user, Book book) {
292+
boolean exist = this.loans.get(user.getId()).stream().anyMatch(b -> b.equals(book.getTitle()));
293+
if (exist) {
294+
book.setCopies(book.getCopies() + 1);
295+
this.loans.get(user.getId()).remove(book.getTitle());
296+
}
297+
}
298+
299+
public Map<Integer, List<String>> getLoans() {
300+
return loans;
301+
}
302+
303+
@Override
304+
public String toString() {
305+
return "Loan [loans=" + loans.toString() + "]";
306+
}
307+
308+
309+
310+
}

0 commit comments

Comments
 (0)