Skip to content

Commit 327e1d5

Browse files
authored
Merge pull request mouredev#6152 from simonguzman/main
#26 - Java #27 - Java
2 parents b79ac68 + 054a27a commit 327e1d5

File tree

2 files changed

+615
-0
lines changed

2 files changed

+615
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,354 @@
1+
2+
import java.io.FileWriter;
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class simonguzman {
9+
public static void main(String[] args) {
10+
//incorrectSrp();
11+
//correctSrp();
12+
//additionalExerciseNoSrp();
13+
additionalExerciseSrp();
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+
134+
/****************************** Ejercicio adicional(Sin srp) ******************************/
135+
public static void additionalExerciseNoSrp(){
136+
Library library = new Library();
137+
138+
library.registerBook("1984", "George Orwell", 3);
139+
library.registerUser("John Doe", "001", "[email protected]");
140+
141+
library.borrowBook("001", "1984");
142+
library.returnBook("001", "1984");
143+
}
144+
145+
static class Library{
146+
private List<Book> books = new ArrayList<>();
147+
private List<User> users = new ArrayList<>();
148+
private Map<User, List<Book>> borrowedBooks = new HashMap<>();
149+
150+
public void registerBook(String title, String author, int copies){
151+
books.add(new Book(title, author, copies));
152+
System.out.println("Libro registrado: "+title);
153+
}
154+
155+
public void registerUser(String name, String id, String email){
156+
users.add(new User(name, id, email));
157+
System.out.println("Usuario registrado: "+name);
158+
}
159+
160+
public void borrowBook(String userId, String bookTitle){
161+
User user = users.stream().filter(u -> u.getId().equals(userId)).findFirst().orElse(null);
162+
Book book = books.stream().filter(b -> b.getTitle().equals(bookTitle)).findFirst().orElse(null);
163+
164+
if(user != null && book != null && book.getCopies() > 0){
165+
borrowedBooks.computeIfAbsent(user, k -> new ArrayList<>()).add(book);
166+
book.setCopies(book.getCopies()-1);
167+
System.out.println("Libro prestado: " + bookTitle + " a " + user.getName());
168+
}else{
169+
System.out.println("No se pudo procesar el prestamo.");
170+
}
171+
}
172+
173+
public void returnBook(String userId, String bookTitle){
174+
User user = users.stream().filter(u -> u.getId().equals(userId)).findFirst().orElse(null);
175+
List<Book> borrowed = borrowedBooks.get(user);
176+
177+
if(borrowed != null){
178+
Book book = borrowed.stream().filter(b -> b.getTitle().equals(bookTitle)).findFirst().orElse(null);
179+
if(book != null){
180+
borrowed.remove(book);
181+
book.setCopies(book.getCopies() + 1);
182+
System.out.println("Libro devuelto: "+bookTitle);
183+
}
184+
}else{
185+
System.out.println("ERROR: No se pudo devolver el libro.");
186+
}
187+
}
188+
}
189+
190+
static class Book{
191+
private String title;
192+
private String author;
193+
private int copies;
194+
195+
public Book(){
196+
197+
}
198+
199+
public Book(String title, String author, int copies){
200+
this.title = title;
201+
this.author = author;
202+
this.copies = copies;
203+
}
204+
205+
public String getTitle() {
206+
return title;
207+
}
208+
209+
public int getCopies() {
210+
return copies;
211+
}
212+
213+
public void setCopies(int copies) {
214+
this.copies = copies;
215+
}
216+
217+
}
218+
219+
static class User{
220+
private String name;
221+
private String id;
222+
private String email;
223+
224+
public User(){
225+
226+
}
227+
228+
public User(String name, String id, String email){
229+
this.name = name;
230+
this.id = id;
231+
this.email = email;
232+
}
233+
234+
public String getName() {
235+
return name;
236+
}
237+
238+
public String getId() {
239+
return id;
240+
}
241+
242+
public String getEmail() {
243+
return email;
244+
}
245+
}
246+
247+
/****************************** ejemplo con srp(Correcto) ******************************/
248+
public static void correctSrp() {
249+
InvoiceSrp invoice = new InvoiceSrp("Laptop",2,1200.00);
250+
251+
InvoicePrinter printer = new InvoicePrinter();
252+
printer.printInvoice(invoice);
253+
254+
InvoiceSaver saver = new InvoiceSaver();
255+
saver.saveToFile(invoice);
256+
}
257+
static class InvoiceSrp{
258+
private String product;
259+
private int quantity;
260+
private double price;
261+
262+
public InvoiceSrp(){
263+
264+
}
265+
266+
public InvoiceSrp(String product, int quantity, double price){
267+
this.product = product;
268+
this.quantity = quantity;
269+
this.price = price;
270+
}
271+
272+
public double calculateTotal(){
273+
return quantity * price;
274+
}
275+
276+
public String getProduct() {
277+
return product;
278+
}
279+
280+
public int getQuantity() {
281+
return quantity;
282+
}
283+
284+
public double getPrice() {
285+
return price;
286+
}
287+
}
288+
289+
static class InvoicePrinter {
290+
public void printInvoice(InvoiceSrp invoice){
291+
System.out.println("Producto: "+invoice.getProduct());
292+
System.out.println("Cantidad: "+invoice.getQuantity());
293+
System.out.println("Precio: "+invoice.getPrice());
294+
System.out.println("Total: "+invoice.calculateTotal());
295+
}
296+
}
297+
298+
static class InvoiceSaver{
299+
public void saveToFile(InvoiceSrp invoice){
300+
try (FileWriter writer = new FileWriter("invoice.txt")){
301+
writer.write("Producto: " + invoice.getProduct() + "\n");
302+
writer.write("Cantidad: " + invoice.getQuantity() + "\n");
303+
writer.write("Precio unitario: " + invoice.getPrice() + "\n");
304+
writer.write("Total: " + invoice.calculateTotal() + "\n");
305+
} catch (Exception e) {
306+
System.out.println("ERROR: no se pudo generar la factura..."+e.getMessage());
307+
}
308+
}
309+
}
310+
/****************************** ejemplo sin srp(Incorrecto) ******************************/
311+
public static void incorrectSrp(){
312+
Invoice invoice = new Invoice("Laptop",2,1200.00);
313+
invoice.printInvoice();
314+
invoice.saveToFile();
315+
}
316+
317+
static class Invoice{
318+
private String product;
319+
private int quantity;
320+
private double price;
321+
322+
public Invoice(){
323+
324+
}
325+
326+
public Invoice(String product, int quantity, double price){
327+
this.product = product;
328+
this.quantity = quantity;
329+
this.price = price;
330+
}
331+
332+
public double calculateTotal(){
333+
return quantity * price;
334+
}
335+
336+
public void printInvoice(){
337+
System.out.println("Producto: "+product);
338+
System.out.println("Cantidad: "+quantity);
339+
System.out.println("Precio: "+price);
340+
System.out.println("Total: "+calculateTotal());
341+
}
342+
343+
public void saveToFile(){
344+
try (FileWriter writer = new FileWriter("invoice.txt")){
345+
writer.write("Producto: " + product + "\n");
346+
writer.write("Cantidad: " + quantity + "\n");
347+
writer.write("Precio Unitario: " + price + "\n");
348+
writer.write("Total: " + calculateTotal() + "\n");
349+
} catch (Exception e) {
350+
System.out.println("Error al guardar la factura: "+e.getMessage());
351+
}
352+
}
353+
}
354+
}

0 commit comments

Comments
 (0)