|
| 1 | +package main |
| 2 | + |
| 3 | +import "fmt" |
| 4 | + |
| 5 | +// * EJERCICIO: |
| 6 | +// * Explora el "Principio SOLID de Responsabilidad Única (Single Responsibility |
| 7 | +// * Principle, SRP)" y crea un ejemplo simple donde se muestre su funcionamiento |
| 8 | +// * de forma correcta e incorrecta. |
| 9 | +// * |
| 10 | +// * DIFICULTAD EXTRA (opcional): |
| 11 | +// * Desarrolla un sistema de gestión para una biblioteca. El sistema necesita |
| 12 | +// * manejar diferentes aspectos como el registro de libros, la gestión de usuarios |
| 13 | +// * y el procesamiento de préstamos de libros. |
| 14 | +// * Requisitos: |
| 15 | +// * 1. Registrar libros: El sistema debe permitir agregar nuevos libros con |
| 16 | +// * información básica como título, autor y número de copias disponibles. |
| 17 | +// * 2. Registrar usuarios: El sistema debe permitir agregar nuevos usuarios con |
| 18 | +// * información básica como nombre, número de identificación y correo electrónico. |
| 19 | +// * 3. Procesar préstamos de libros: El sistema debe permitir a los usuarios |
| 20 | +// * tomar prestados y devolver libros. |
| 21 | +// * Instrucciones: |
| 22 | +// * 1. Diseña una clase que no cumple el SRP: Crea una clase Library que maneje |
| 23 | +// * los tres aspectos mencionados anteriormente (registro de libros, registro de |
| 24 | +// * usuarios y procesamiento de préstamos). |
| 25 | +// * 2. Refactoriza el código: Separa las responsabilidades en diferentes clases |
| 26 | +// * siguiendo el Principio de Responsabilidad Única. |
| 27 | + |
| 28 | +type Book struct { |
| 29 | + title string |
| 30 | + author string |
| 31 | + copie int |
| 32 | +} |
| 33 | + |
| 34 | +// ********************************* |
| 35 | + |
| 36 | +type User struct { |
| 37 | + name string |
| 38 | + id int |
| 39 | + email string |
| 40 | +} |
| 41 | + |
| 42 | +// *********************** |
| 43 | + |
| 44 | +type Loan struct { |
| 45 | + loans map[string][]string |
| 46 | +} |
| 47 | + |
| 48 | +func (l *Loan) CreateLoan(u string, b string) { |
| 49 | + l.loans[b] = append(l.loans[b], u) |
| 50 | +} |
| 51 | + |
| 52 | +func (l *Loan) DeleteLoan(b string) { |
| 53 | + if len((l.loans[b])) == 1 { |
| 54 | + delete(l.loans, b) |
| 55 | + |
| 56 | + } else { |
| 57 | + l.loans[b] = l.loans[b][1:] |
| 58 | + } |
| 59 | + |
| 60 | +} |
| 61 | + |
| 62 | +// *********************** |
| 63 | +type Library struct { |
| 64 | + books []Book |
| 65 | + users []User |
| 66 | + loans Loan |
| 67 | +} |
| 68 | + |
| 69 | +func (l *Library) AddLoan(b *Book, u User) { |
| 70 | + if b.copie == 0 { |
| 71 | + fmt.Printf("Libro %v No hay copias disponibles para Prestar : copias %v\n", b.title, b.copie) |
| 72 | + return |
| 73 | + } else { |
| 74 | + b.copie -= 1 |
| 75 | + } |
| 76 | + |
| 77 | + if l.loans.loans == nil { |
| 78 | + l.loans.loans = map[string][]string{} |
| 79 | + } |
| 80 | + |
| 81 | + l.loans.CreateLoan(u.name, b.title) |
| 82 | +} |
| 83 | + |
| 84 | +func (l *Library) DeleteLoan(b *Book) { |
| 85 | + |
| 86 | + l.loans.DeleteLoan(b.title) |
| 87 | + b.copie += 1 |
| 88 | +} |
| 89 | + |
| 90 | +func (l *Library) AddUser(u User) { |
| 91 | + |
| 92 | + l.users = append(l.users, u) |
| 93 | +} |
| 94 | + |
| 95 | +func (l *Library) AddBook(b Book) { |
| 96 | + |
| 97 | + l.books = append(l.books, b) |
| 98 | +} |
| 99 | + |
| 100 | +// ********************************** |
| 101 | + |
| 102 | +type PrintService struct { |
| 103 | +} |
| 104 | + |
| 105 | +func (p PrintService) PrintBook(b Book) { |
| 106 | + fmt.Printf("Libro ** Titulo : %v Autor %v, Copias %v\n", b.title, b.author, b.copie) |
| 107 | +} |
| 108 | + |
| 109 | +func (p PrintService) PrintUser(u User) { |
| 110 | + fmt.Printf("Usuario ** Nombre : %s, Id %v, email %s\n", u.name, u.id, u.email) |
| 111 | +} |
| 112 | + |
| 113 | +func (p PrintService) ListUser(lu []User) { |
| 114 | + fmt.Println("Lista de Usuarios") |
| 115 | + for _, v := range lu { |
| 116 | + p.PrintUser(v) |
| 117 | + } |
| 118 | + |
| 119 | +} |
| 120 | + |
| 121 | +func (p PrintService) ListBook(lb []Book) { |
| 122 | + fmt.Println("Lista de Libros") |
| 123 | + for _, v := range lb { |
| 124 | + p.PrintBook(v) |
| 125 | + } |
| 126 | +} |
| 127 | +func (p PrintService) ListLoan(ln Loan) { |
| 128 | + fmt.Println("Lista de Prestamos") |
| 129 | + for i, v := range ln.loans { |
| 130 | + fmt.Printf("Libro: %v : ", i) |
| 131 | + for _, l := range v { |
| 132 | + fmt.Printf("%v, ", l) |
| 133 | + } |
| 134 | + } |
| 135 | + fmt.Println() |
| 136 | +} |
| 137 | + |
| 138 | +func main() { |
| 139 | + B1 := Book{"b1", "www", 3} |
| 140 | + U1 := User{"U1", 111, "dd@dsds"} |
| 141 | + B2 := Book{"B2", "www", 3} |
| 142 | + U2 := User{"U2", 111, "dd@dsds"} |
| 143 | + L := Library{} |
| 144 | + L.AddBook(B1) |
| 145 | + L.AddBook(B2) |
| 146 | + L.AddUser(U1) |
| 147 | + L.AddUser(U2) |
| 148 | + L.AddLoan(&B1, U1) |
| 149 | + L.AddLoan(&B2, U2) |
| 150 | + PrintService{}.ListBook(L.books) |
| 151 | + PrintService{}.ListUser(L.users) |
| 152 | + PrintService{}.PrintBook(B1) |
| 153 | + PrintService{}.PrintUser(U1) |
| 154 | + PrintService{}.ListLoan(L.loans) |
| 155 | + PrintService{}.ListLoan(L.loans) |
| 156 | + L.AddLoan(&B1, U1) |
| 157 | + L.AddLoan(&B2, U1) |
| 158 | + L.AddLoan(&B1, U1) |
| 159 | + L.AddLoan(&B1, U1) |
| 160 | + L.AddLoan(&B2, U1) |
| 161 | + L.AddLoan(&B2, U1) |
| 162 | + PrintService{}.ListLoan(L.loans) |
| 163 | + PrintService{}.PrintBook(B1) |
| 164 | + PrintService{}.PrintBook(B2) |
| 165 | + L.DeleteLoan(&B2) // elimina el primer prestado, no es objetivo este paso en este ejercicio |
| 166 | + PrintService{}.ListLoan(L.loans) |
| 167 | + PrintService{}.PrintBook(B1) |
| 168 | + PrintService{}.PrintBook(B2) |
| 169 | + |
| 170 | +} |
0 commit comments