Skip to content

Commit 21e0748

Browse files
committed
Ejercicio adicional sin srp
1 parent 3fc39f1 commit 21e0748

File tree

1 file changed

+119
-1
lines changed

1 file changed

+119
-1
lines changed

Roadmap/26 - SOLID SRP/java/simonguzman.java

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,129 @@
11

22
import java.io.FileWriter;
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
37

48
public class simonguzman {
59
public static void main(String[] args) {
610
//incorrectSrp();
7-
correctSrp();
11+
//correctSrp();
12+
additionalExerciseNoSrp();
813
}
14+
/****************************** Ejercicio adicional(Sin srp) ******************************/
15+
public static void additionalExerciseNoSrp(){
16+
Library library = new Library();
17+
18+
library.registerBook("1984", "George Orwell", 3);
19+
library.registerUser("John Doe", "001", "johndoe@example.com");
20+
21+
library.borrowBook("001", "1984");
22+
library.returnBook("001", "1984");
23+
}
24+
25+
static class Library{
26+
private List<Book> books = new ArrayList<>();
27+
private List<User> users = new ArrayList<>();
28+
private Map<User, List<Book>> borrowedBooks = new HashMap<>();
29+
30+
public void registerBook(String title, String author, int copies){
31+
books.add(new Book(title, author, copies));
32+
System.out.println("Libro registrado: "+title);
33+
}
34+
35+
public void registerUser(String name, String id, String email){
36+
users.add(new User(name, id, email));
37+
System.out.println("Usuario registrado: "+name);
38+
}
39+
40+
public void borrowBook(String userId, String bookTitle){
41+
User user = users.stream().filter(u -> u.getId().equals(userId)).findFirst().orElse(null);
42+
Book book = books.stream().filter(b -> b.getTitle().equals(bookTitle)).findFirst().orElse(null);
43+
44+
if(user != null && book != null && book.getCopies() > 0){
45+
borrowedBooks.computeIfAbsent(user, k -> new ArrayList<>()).add(book);
46+
book.setCopies(book.getCopies()-1);
47+
System.out.println("Libro prestado: " + bookTitle + " a " + user.getName());
48+
}else{
49+
System.out.println("No se pudo procesar el prestamo.");
50+
}
51+
}
52+
53+
public void returnBook(String userId, String bookTitle){
54+
User user = users.stream().filter(u -> u.getId().equals(userId)).findFirst().orElse(null);
55+
List<Book> borrowed = borrowedBooks.get(user);
56+
57+
if(borrowed != null){
58+
Book book = borrowed.stream().filter(b -> b.getTitle().equals(bookTitle)).findFirst().orElse(null);
59+
if(book != null){
60+
borrowed.remove(book);
61+
book.setCopies(book.getCopies() + 1);
62+
System.out.println("Libro devuelto: "+bookTitle);
63+
}
64+
}else{
65+
System.out.println("ERROR: No se pudo devolver el libro.");
66+
}
67+
}
68+
}
69+
70+
static class Book{
71+
private String title;
72+
private String author;
73+
private int copies;
74+
75+
public Book(){
76+
77+
}
78+
79+
public Book(String title, String author, int copies){
80+
this.title = title;
81+
this.author = author;
82+
this.copies = copies;
83+
}
84+
85+
public String getTitle() {
86+
return title;
87+
}
88+
89+
public int getCopies() {
90+
return copies;
91+
}
92+
93+
public void setCopies(int copies) {
94+
this.copies = copies;
95+
}
96+
97+
}
98+
99+
static class User{
100+
private String name;
101+
private String id;
102+
private String email;
103+
104+
public User(){
105+
106+
}
107+
108+
public User(String name, String id, String email){
109+
this.name = name;
110+
this.id = id;
111+
this.email = email;
112+
}
113+
114+
public String getName() {
115+
return name;
116+
}
117+
118+
public String getId() {
119+
return id;
120+
}
121+
122+
public String getEmail() {
123+
return email;
124+
}
125+
}
126+
9127
/****************************** ejemplo con srp(Correcto) ******************************/
10128
public static void correctSrp() {
11129
InvoiceSrp invoice = new InvoiceSrp("Laptop",2,1200.00);

0 commit comments

Comments
 (0)