|
| 1 | +/* |
| 2 | + * EJERCICIO: |
| 3 | + * Explora el concepto de herencia según tu lenguaje. Crea un ejemplo que |
| 4 | + * implemente una superclase Animal y un par de subclases Perro y Gato, |
| 5 | + * junto con una función que sirva para imprimir el sonido que emite cada Animal. |
| 6 | + * |
| 7 | + * DIFICULTAD EXTRA (opcional): |
| 8 | + * Implementa la jerarquía de una empresa de desarrollo formada por Empleados que |
| 9 | + * pueden ser Gerentes, Gerentes de Proyectos o Programadores. |
| 10 | + * Cada empleado tiene un identificador y un nombre. |
| 11 | + * Dependiendo de su labor, tienen propiedades y funciones exclusivas de su |
| 12 | + * actividad, y almacenan los empleados a su cargo. |
| 13 | + */ |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +public class JimsimroDev { |
| 20 | + |
| 21 | + public static void main(String[] args) { |
| 22 | + var perro = new Perro("max"); |
| 23 | + perro.sonido(); |
| 24 | + |
| 25 | + var gato = new Gato("garfield"); |
| 26 | + System.out.println(gato.getNombre()); |
| 27 | + gato.sonido(); |
| 28 | + |
| 29 | + // Puedo crear un arreglo pero como el arreglo solo acepta datos del mismo tipo |
| 30 | + // uso la clase Padre JimsimroDev |
| 31 | + Animal animales[] = new Animal[4]; // Aquie le digo que puedo guardar 4 animales |
| 32 | + // Ahora asigno valor a cada posicoin del arreglo |
| 33 | + animales[0] = perro; |
| 34 | + animales[1] = gato; |
| 35 | + animales[2] = new Perro("Dante"); |
| 36 | + animales[3] = new Perro("Gilver"); |
| 37 | + System.out.println(animales[2].toString()); |
| 38 | + // Lo puedo recorre con un for o usando Arrays.toString() para mostrarlos |
| 39 | + for (int a = 0; a < animales.length; a++) { |
| 40 | + System.out.println(animales[a].toString()); |
| 41 | + } |
| 42 | + System.out.println(Arrays.toString(animales)); |
| 43 | + |
| 44 | + System.out.println("::::::::::EXTRA::::::::::::::"); |
| 45 | + // EXTRA |
| 46 | + var gerente = new Gerente(1L, "Jhoan"); |
| 47 | + |
| 48 | + var gerenteDeProyectos = new GerenteProyecto(2L, "Isabel", "Proyecto 1"); |
| 49 | + var gerenteDeProyectos1 = new GerenteProyecto(3L, "Keren", "Proyecto 2"); |
| 50 | + |
| 51 | + var desarrollador = new Programador(4L, "Jesus", "PHP"); |
| 52 | + var desarrollador1 = new Programador(5L, "Jarvis", "Ruby"); |
| 53 | + var desarrollador2 = new Programador(6L, "Carmen", "perl"); |
| 54 | + var desarrollador3 = new Programador(7L, "Jimmis", "Java"); |
| 55 | + |
| 56 | + System.out.println("::::::::::EXTRA::::::::::::::"); |
| 57 | + System.out.println("::::::::::GERENTE::::::::::::"); |
| 58 | + gerente.agregarEmpleado(gerenteDeProyectos); |
| 59 | + gerente.agregarEmpleado(gerenteDeProyectos1); |
| 60 | + gerente.coordinarImplementacion(); |
| 61 | + gerente.mostrarNombreYPuesto(); |
| 62 | + gerente.print(); |
| 63 | + |
| 64 | + System.out.println("::::::::::GERENTE DE PROYECTOS::::::::::::::"); |
| 65 | + gerenteDeProyectos.agregarEmpleado(desarrollador); |
| 66 | + gerenteDeProyectos.agregarEmpleado(desarrollador1); |
| 67 | + gerenteDeProyectos.agregarEmpleado(desarrollador2); |
| 68 | + gerenteDeProyectos.agregarEmpleado(desarrollador3); |
| 69 | + gerenteDeProyectos.mostrarNombreYPuesto(); |
| 70 | + gerenteDeProyectos.coordinarImplementacion(); |
| 71 | + gerenteDeProyectos.print(); |
| 72 | + |
| 73 | + System.out.println("::::::::::DESARROLLADOR::::::::::::::"); |
| 74 | + desarrollador.mostrarNombreYPuesto(); |
| 75 | + desarrollador.agregarEmpleado(desarrollador3); |
| 76 | + desarrollador.print(); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +class Animal { |
| 81 | + private String nombre; |
| 82 | + |
| 83 | + public String getNombre() { |
| 84 | + return this.nombre; |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + public Animal(String nombre) { |
| 89 | + this.nombre = nombre; |
| 90 | + } |
| 91 | + |
| 92 | + public Animal() { |
| 93 | + } |
| 94 | + |
| 95 | + public void sonido() { |
| 96 | + System.out.println("Soindio generico"); |
| 97 | + } |
| 98 | + |
| 99 | +} |
| 100 | + |
| 101 | +class Perro extends Animal { |
| 102 | + |
| 103 | + public Perro(String nombre) { |
| 104 | + super(nombre); |
| 105 | + } |
| 106 | + |
| 107 | + // Para sobrescribir el metodo en tiempo de ejecucion en java se usa la |
| 108 | + // anotacion @Overrideo |
| 109 | + @Override |
| 110 | + public void sonido() { |
| 111 | + System.out.println("El perro ladra: ¡Guau!"); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public String toString() { |
| 116 | + return "Nombre: " + getNombre(); |
| 117 | + } |
| 118 | + |
| 119 | +} |
| 120 | + |
| 121 | +class Gato extends Animal { |
| 122 | + |
| 123 | + public Gato(String nombre) { |
| 124 | + super(nombre); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void sonido() { |
| 129 | + System.out.println("El gato maulla: Miau!"); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public String toString() { |
| 134 | + return "Nombre: " + getNombre(); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// Extra |
| 139 | +class Empleado { |
| 140 | + private Long id; |
| 141 | + private String puesto; |
| 142 | + private String nombre; |
| 143 | + private List<Empleado> empleadosACargo; |
| 144 | + |
| 145 | + public Empleado() { |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + public Empleado(Long id, String puesto, String nombre) { |
| 150 | + this.id = id; |
| 151 | + this.puesto = puesto; |
| 152 | + this.nombre = nombre; |
| 153 | + this.empleadosACargo = new ArrayList<>(); |
| 154 | + } |
| 155 | + |
| 156 | + public void agregarEmpleado(Empleado empleado) { |
| 157 | + if (this instanceof Gerente || this instanceof GerenteProyecto) { |
| 158 | + empleadosACargo.add(empleado); |
| 159 | + } else { |
| 160 | + System.out.println("Solo gerente y gerente de Proyectos pueden tener empleados a cargo"); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + public void print() { |
| 165 | + System.out.println("Empleados a cargo"); |
| 166 | + for (Empleado empleado : empleadosACargo) { |
| 167 | + System.out.print(empleado.getNombre()); |
| 168 | + System.out.print(","); |
| 169 | + } |
| 170 | + System.out.println(); |
| 171 | + } |
| 172 | + |
| 173 | + public void mostrarNombreYPuesto() { |
| 174 | + System.out.println(getPuesto() + getNombre()); |
| 175 | + } |
| 176 | + |
| 177 | + public Long getId() { |
| 178 | + return this.id; |
| 179 | + } |
| 180 | + |
| 181 | + public String getNombre() { |
| 182 | + return this.nombre; |
| 183 | + } |
| 184 | + |
| 185 | + public void setNombre(String nombre) { |
| 186 | + this.nombre = nombre; |
| 187 | + } |
| 188 | + |
| 189 | + public List<Empleado> getEmpleadosACargo() { |
| 190 | + return this.empleadosACargo; |
| 191 | + } |
| 192 | + |
| 193 | + public String getPuesto() { |
| 194 | + return puesto; |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +class Gerente extends Empleado { |
| 199 | + |
| 200 | + public Gerente() { |
| 201 | + |
| 202 | + } |
| 203 | + |
| 204 | + public Gerente(Long id, String nombre) { |
| 205 | + super(id, " Gerente ", nombre); |
| 206 | + } |
| 207 | + |
| 208 | + public void coordinarImplementacion() { |
| 209 | + System.out.printf("%s Coordinando la Implementación de los proyecto informatico\n", getNombre()); |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +class Programador extends Empleado { |
| 214 | + private String lenguaje; |
| 215 | + |
| 216 | + public Programador(Long id, String nombre, String lenguaje) { |
| 217 | + super(id, " Programador ", nombre); |
| 218 | + this.lenguaje = lenguaje; |
| 219 | + } |
| 220 | + |
| 221 | + public void codeando() { |
| 222 | + System.out.printf("%s Esta Desrrollando en el %s\n ", getNombre(), lenguaje); |
| 223 | + } |
| 224 | + |
| 225 | + public String getlenguaje() { |
| 226 | + return lenguaje; |
| 227 | + } |
| 228 | + |
| 229 | + public void setlenguaje(String lenguaje) { |
| 230 | + this.lenguaje = lenguaje; |
| 231 | + } |
| 232 | +} |
| 233 | + |
| 234 | +class GerenteProyecto extends Empleado { |
| 235 | + private String proyecto; |
| 236 | + |
| 237 | + public GerenteProyecto(Long id, String nombre, String proyecto) { |
| 238 | + super(id, " Gerente de Proyectos ", nombre); |
| 239 | + this.proyecto = proyecto; |
| 240 | + } |
| 241 | + |
| 242 | + public void coordinarImplementacion() { |
| 243 | + System.out.printf("%s Coordinando la Implementación su proyecto informatico\n", getNombre()); |
| 244 | + } |
| 245 | + |
| 246 | + public String getProyecto() { |
| 247 | + return this.proyecto; |
| 248 | + } |
| 249 | + |
| 250 | + public void setProyecto(String proyecto) { |
| 251 | + this.proyecto = proyecto; |
| 252 | + } |
| 253 | +} |
0 commit comments