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