|
| 1 | +import java.util.*; |
| 2 | + |
| 3 | +/** |
| 4 | + * #26 SOLID: PRINCIPIO DE RESPONSABILIDAD ÚNICA (SRP) |
| 5 | + * |
| 6 | + * @author martinbohorquez |
| 7 | + */ |
| 8 | +public class martinbohorquez { |
| 9 | + static List<User> users = new ArrayList<>(); |
| 10 | + static List<Usuario> usuarios = new ArrayList<>(); |
| 11 | + |
| 12 | + public static void main(String[] args) { |
| 13 | + incorrectSrp(); |
| 14 | + correctSrp(); |
| 15 | + /* |
| 16 | + * DIFICULTAD EXTRA |
| 17 | + */ |
| 18 | + addExerciseNoSrp(); |
| 19 | + |
| 20 | + } |
| 21 | + |
| 22 | + private static void addExerciseNoSrp() { |
| 23 | + Library library = new Library(); |
| 24 | + library.addBook("Libro 1", "Autor 1", 2); |
| 25 | + library.addBook("Libro 2", "Autor 2", 1); |
| 26 | + System.out.printf("La lista de libros es: %s%n", library.books); |
| 27 | + library. addUser( "User 1", 1, "[email protected]"); |
| 28 | + library. addUser( "User 2", 2, "[email protected]"); |
| 29 | + library. addUser( "User 3", 3, "[email protected]"); |
| 30 | + System.out.printf("La lista de usuarios es: %s%n", library.users); |
| 31 | + library.loanBook(2, "Libro 1"); |
| 32 | + library.loanBook(3, "Libro 2"); |
| 33 | + library.loanBook(1, "Libro 2"); |
| 34 | + library.loanBook(1, "Libro 1"); |
| 35 | + System.out.printf("La lista de prestámos es: %s%n", library.loans); |
| 36 | + library.returnBook(2, "Libro 2"); |
| 37 | + library.returnBook(2, "Libro 1"); |
| 38 | + System.out.printf("La lista de prestámos es: %s%n", library.loans); |
| 39 | + } |
| 40 | + |
| 41 | + private static void addExerciseSrp() { |
| 42 | + Biblioteca biblioteca = new Biblioteca(); |
| 43 | + Book book1 = new Book("Libro 1", "Autor 1", 2); |
| 44 | + Book book2 = new Book("Libro 2", "Autor 2", 1); |
| 45 | + biblioteca.addBook(book1); |
| 46 | + biblioteca.addBook(book2); |
| 47 | + System.out.printf("La lista de libros es: %s%n", biblioteca.books); |
| 48 | + Usero user1 = new Usero( "User 1", 1, "[email protected]"); |
| 49 | + Usero user2 = new Usero( "User 2", 2, "[email protected]"); |
| 50 | + Usero user3 = new Usero( "User 3", 3, "[email protected]"); |
| 51 | + biblioteca.addUser(user1); |
| 52 | + biblioteca.addUser(user2); |
| 53 | + biblioteca.addUser(user3); |
| 54 | + System.out.printf("La lista de usuarios es: %s%n", biblioteca.users); |
| 55 | + biblioteca.loanBook(2, "Libro 1"); |
| 56 | + biblioteca.loanBook(3, "Libro 2"); |
| 57 | + biblioteca.loanBook(1, "Libro 2"); |
| 58 | + biblioteca.loanBook(1, "Libro 1"); |
| 59 | + System.out.printf("La lista de prestámos es: %s%n", biblioteca.loans); |
| 60 | + biblioteca.returnBook(2, "Libro 2"); |
| 61 | + biblioteca.returnBook(2, "Libro 1"); |
| 62 | + System.out.printf("La lista de prestámos es: %s%n", biblioteca.loans); |
| 63 | + } |
| 64 | + |
| 65 | + private static void incorrectSrp() { |
| 66 | + User user1 = new User( "Jose", "[email protected]"); |
| 67 | + User user2 = new User( "Luis", "[email protected]"); |
| 68 | + user1.saveDatabase(); |
| 69 | + user2.saveDatabase(); |
| 70 | + user1.sendEmail("Mensaje enviado desde la una instancia que no aplica el principio de SRP!"); |
| 71 | + user2.sendEmail("Mensaje 2 enviado desde la una instancia que no aplica el principio de SRP!"); |
| 72 | + } |
| 73 | + |
| 74 | + private static void correctSrp() { |
| 75 | + Usuario usuario1 = new Usuario( "Jose", "[email protected]"); |
| 76 | + Usuario usuario2 = new Usuario( "Luis", "[email protected]"); |
| 77 | + UsuarioService usuarioService = new UsuarioService(); |
| 78 | + usuarioService.saveDatabase(usuario1) |
| 79 | + .saveDatabase(usuario2); |
| 80 | + EmailService emailService = new EmailService(); |
| 81 | + emailService.sendEmail(usuario1, |
| 82 | + "Mensaje enviado desde la una instancia que aplica el principio de SRP!"); |
| 83 | + emailService.sendEmail(usuario2, |
| 84 | + "Mensaje 2 enviado desde la una instancia que aplica el principio de SRP!"); |
| 85 | + } |
| 86 | + |
| 87 | + static class User { |
| 88 | + private final String name; |
| 89 | + private final String email; |
| 90 | + |
| 91 | + User(String name, String email) { |
| 92 | + this.name = name; |
| 93 | + this.email = email; |
| 94 | + } |
| 95 | + |
| 96 | + protected void saveDatabase() { |
| 97 | + users.add(this); |
| 98 | + System.out.printf("Se ha creado el usuario con nombre %s%n", name); |
| 99 | + } |
| 100 | + |
| 101 | + protected void sendEmail(String message) { |
| 102 | + System.out.printf("Se envía correo desde '%s':%n%s%n", email, message); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + static class Usuario { |
| 107 | + private final String name; |
| 108 | + private final String email; |
| 109 | + |
| 110 | + Usuario(String name, String email) { |
| 111 | + this.name = name; |
| 112 | + this.email = email; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + static class UsuarioService { |
| 117 | + protected UsuarioService saveDatabase(Usuario usuario) { |
| 118 | + usuarios.add(usuario); |
| 119 | + System.out.printf("Se ha creado el usuario con nombre %s (usando SRP)%n", usuario.name); |
| 120 | + return this; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + static class EmailService { |
| 125 | + protected void sendEmail(Usuario usuario, String message) { |
| 126 | + System.out.printf("Se envía correo desde '%s':%n%s%n", usuario.email, message); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + static class Library { |
| 131 | + private List<Map<String, Object>> books; |
| 132 | + private List<Map<String, Object>> users; |
| 133 | + private List<Map<String, Object>> loans; |
| 134 | + |
| 135 | + public Library() { |
| 136 | + this.books = new ArrayList<>(); |
| 137 | + this.users = new ArrayList<>(); |
| 138 | + this.loans = new ArrayList<>(); |
| 139 | + } |
| 140 | + |
| 141 | + private void addBook(String title, String author, Integer copies) { |
| 142 | + Map<String, Object> book = new HashMap<>(); |
| 143 | + book.put("title", title); |
| 144 | + book.put("author", author); |
| 145 | + book.put("copies", copies); |
| 146 | + books.add(book); |
| 147 | + } |
| 148 | + |
| 149 | + private void addUser(String name, Integer id, String email) { |
| 150 | + Map<String, Object> user = new HashMap<>(); |
| 151 | + user.put("name", name); |
| 152 | + user.put("id", id); |
| 153 | + user.put("email", email); |
| 154 | + users.add(user); |
| 155 | + } |
| 156 | + |
| 157 | + private void loanBook(Integer userId, String bookTitle) { |
| 158 | + books.stream() |
| 159 | + .filter(b -> b.get("title").equals(bookTitle) && (Integer) b.get("copies") > 0) |
| 160 | + .findFirst() |
| 161 | + .ifPresent(b -> { |
| 162 | + b.put("copies", (Integer) b.get("copies") - 1); |
| 163 | + Map<String, Object> loan = new HashMap<>(); |
| 164 | + loan.put("userId", userId); |
| 165 | + loan.put("bookTitle", bookTitle); |
| 166 | + loans.add(loan); |
| 167 | + }); |
| 168 | + } |
| 169 | + |
| 170 | + private void returnBook(Integer userId, String bookTitle) { |
| 171 | + loans.stream() |
| 172 | + .filter(l -> l.get("userId").equals(userId) && l.get("bookTitle").equals(bookTitle)) |
| 173 | + .findFirst() |
| 174 | + .ifPresent(l -> { |
| 175 | + loans.remove(l); |
| 176 | + books.stream() |
| 177 | + .filter(b -> b.get("title").equals(bookTitle)) |
| 178 | + .findFirst() |
| 179 | + .ifPresent(b -> b.put("copies", (Integer) b.get("copies") + 1)); |
| 180 | + }); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + static class Biblioteca { |
| 185 | + private List<Book> books; |
| 186 | + private List<Usero> users; |
| 187 | + private Loan loans; |
| 188 | + |
| 189 | + public Biblioteca() { |
| 190 | + this.books = new ArrayList<>(); |
| 191 | + this.users = new ArrayList<>(); |
| 192 | + this.loans = new Loan(); |
| 193 | + } |
| 194 | + |
| 195 | + private void addBook(Book book) { |
| 196 | + books.add(book); |
| 197 | + } |
| 198 | + |
| 199 | + private void addUser(Usero user) { |
| 200 | + users.add(user); |
| 201 | + } |
| 202 | + |
| 203 | + private void loanBook(Integer userId, String bookTitle) { |
| 204 | + Optional<Usero> user = users.stream().filter(u -> u.id.equals(userId)).findAny(); |
| 205 | + Optional<Book> book = books.stream().filter(b -> b.title.equals(bookTitle)).findAny(); |
| 206 | + if (user.isPresent() && book.isPresent()) loans.loanBook(user.get(), book.get()); |
| 207 | + |
| 208 | + } |
| 209 | + |
| 210 | + private void returnBook(Integer userId, String bookTitle) { |
| 211 | + Optional<Usero> user = users.stream().filter(u -> u.id.equals(userId)).findAny(); |
| 212 | + Optional<Book> book = books.stream().filter(b -> b.title.equals(bookTitle)).findAny(); |
| 213 | + if (user.isPresent() && book.isPresent()) loans.returnBook(user.get(), book.get()); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + static class Book { |
| 218 | + private String title; |
| 219 | + private String author; |
| 220 | + private Integer copies; |
| 221 | + |
| 222 | + public Book(String title, String author, Integer copies) { |
| 223 | + this.title = title; |
| 224 | + this.author = author; |
| 225 | + this.copies = copies; |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + static class Usero { |
| 230 | + private String name; |
| 231 | + private Integer id; |
| 232 | + private String email; |
| 233 | + |
| 234 | + public Usero(String name, Integer id, String email) { |
| 235 | + this.name = name; |
| 236 | + this.id = id; |
| 237 | + this.email = email; |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + static class Loan { |
| 242 | + private List<Map<String, Object>> loans; |
| 243 | + |
| 244 | + public Loan() { |
| 245 | + List<Loan> loans = new ArrayList<>(); |
| 246 | + } |
| 247 | + |
| 248 | + private void loanBook(Usero user, Book book) { |
| 249 | + if (book.copies > 0) { |
| 250 | + book.copies--; |
| 251 | + Map<String, Object> loan = new HashMap<>(); |
| 252 | + loan.put("userId", user.id); |
| 253 | + loan.put("bookTitle", book.title); |
| 254 | + loans.add(loan); |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + private void returnBook(Usero user, Book book) { |
| 259 | + loans.stream() |
| 260 | + .filter(loan -> loan.get("userId").equals(user.id) && loan.get("bookTitle").equals(book.title)) |
| 261 | + .findFirst() |
| 262 | + .ifPresent(loan -> { |
| 263 | + loans.remove(loan); |
| 264 | + book.copies++; |
| 265 | + }); |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | +} |
0 commit comments