|
| 1 | +import java.util.LinkedList; |
| 2 | +import java.util.Queue; |
| 3 | +import java.util.Stack; |
| 4 | + |
| 5 | +/** |
| 6 | + * |
| 7 | + *EJERCICIO: |
| 8 | + * Explora el concepto de clase y crea un ejemplo que implemente un inicializador, |
| 9 | + * atributos y una función que los imprima (teniendo en cuenta las posibilidades |
| 10 | + * de tu lenguaje). |
| 11 | + * Una vez implementada, créala, establece sus parámetros, modifícalos e imprímelos |
| 12 | + * utilizando su función. |
| 13 | + * |
| 14 | + * DIFICULTAD EXTRA (opcional): |
| 15 | + * Implementa dos clases que representen las estructuras de Pila y Cola (estudiadas |
| 16 | + * en el ejercicio número 7 de la ruta de estudio) |
| 17 | + * - Deben poder inicializarse y disponer de operaciones para añadir, eliminar, |
| 18 | + * retornar el número de elementos e imprimir todo su contenido. |
| 19 | + * |
| 20 | + * @version v1.0 |
| 21 | + * |
| 22 | + * @since 31/07/2024 |
| 23 | + * |
| 24 | + * @author GlossyPath |
| 25 | + */ |
| 26 | + |
| 27 | + |
| 28 | +public class GlossyPath { |
| 29 | + |
| 30 | + public static void main(String[] args) { |
| 31 | + |
| 32 | + Pila<Empresa> pilaEmpresas = new Pila<>(); |
| 33 | + |
| 34 | + Cola<Empresa> colaEmpresas = new Cola(); |
| 35 | + |
| 36 | + Empresa empresa = new Empresa(); |
| 37 | + |
| 38 | + empresa.setCantidadEmpleados(10); |
| 39 | + empresa.setEsNueva(true); |
| 40 | + empresa.setNombre("Rolan S.L"); |
| 41 | + empresa.setUbicadoEnPais("Francia"); |
| 42 | + |
| 43 | + pilaEmpresas.añadirALaPila(empresa); |
| 44 | + |
| 45 | + colaEmpresas.añadirALaCola(empresa); |
| 46 | + |
| 47 | + System.out.println(pilaEmpresas); |
| 48 | + |
| 49 | + System.out.println(colaEmpresas); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +class Empresa { |
| 55 | + |
| 56 | + private String nombre, ubicadoEnPais; |
| 57 | + private int cantidadEmpleados; |
| 58 | + private boolean esNueva; |
| 59 | + |
| 60 | + public Empresa(){ |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + public String getNombre() { |
| 65 | + return nombre; |
| 66 | + } |
| 67 | + |
| 68 | + public void setNombre(String nombre) { |
| 69 | + this.nombre = nombre; |
| 70 | + } |
| 71 | + |
| 72 | + public String getUbicadoEnPais() { |
| 73 | + return ubicadoEnPais; |
| 74 | + } |
| 75 | + |
| 76 | + public void setUbicadoEnPais(String ubicadoEnPais) { |
| 77 | + this.ubicadoEnPais = ubicadoEnPais; |
| 78 | + } |
| 79 | + |
| 80 | + public int getCantidadEmpleados() { |
| 81 | + return cantidadEmpleados; |
| 82 | + } |
| 83 | + |
| 84 | + public void setCantidadEmpleados(int cantidadEmpleados) { |
| 85 | + this.cantidadEmpleados = cantidadEmpleados; |
| 86 | + } |
| 87 | + |
| 88 | + public boolean isEsNueva() { |
| 89 | + return esNueva; |
| 90 | + } |
| 91 | + |
| 92 | + public void setEsNueva(boolean esNueva) { |
| 93 | + this.esNueva = esNueva; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public String toString(){ |
| 98 | + |
| 99 | + String nuevaVieja; |
| 100 | + |
| 101 | + if(isEsNueva()){ |
| 102 | + nuevaVieja = "si"; |
| 103 | + |
| 104 | + } else { |
| 105 | + nuevaVieja = "la empresa es antigua"; |
| 106 | + } |
| 107 | + |
| 108 | + return "Empresa {" + |
| 109 | + "nombre: " + nombre + |
| 110 | + " ubicada en: " + ubicadoEnPais + |
| 111 | + " cantidad de empleados: " + cantidadEmpleados + |
| 112 | + " la empresa es nueva:" + nuevaVieja + "}"; |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | +class Pila<T> { |
| 119 | + |
| 120 | + private Stack<T> pila; |
| 121 | + |
| 122 | + public Pila() { |
| 123 | + this.pila = new Stack<>(); |
| 124 | + } |
| 125 | + |
| 126 | + public int cantidadObjetos(){ |
| 127 | + return this.pila.size(); |
| 128 | + } |
| 129 | + |
| 130 | + public void añadirALaPila(T o){ |
| 131 | + this.pila.add(o); |
| 132 | + } |
| 133 | + |
| 134 | + public void eliminarDeLaPila(){ |
| 135 | + T eliminado = this.pila.pop(); |
| 136 | + |
| 137 | + System.out.println("El elemento eliminado es: " + eliminado); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public String toString(){ |
| 142 | + StringBuilder nombreEmpresas = new StringBuilder("\nContenido de la pila: \n"); |
| 143 | + |
| 144 | + @SuppressWarnings("unchecked") |
| 145 | + Stack<T> copiaPila = (Stack<T>) this.pila.clone(); |
| 146 | + |
| 147 | + while (!copiaPila.isEmpty()) { |
| 148 | + nombreEmpresas.append(copiaPila.pop().toString()).append("\n"); |
| 149 | + } |
| 150 | + |
| 151 | + return nombreEmpresas.toString(); |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +class Cola<T> { |
| 156 | + |
| 157 | + private Queue<T> cola; |
| 158 | + |
| 159 | + public Cola() { |
| 160 | + this.cola= new LinkedList<>(); |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + public int cantidadObjetos(){ |
| 165 | + return this.cola.size(); |
| 166 | + } |
| 167 | + |
| 168 | + public void añadirALaCola(T object){ |
| 169 | + this.cola.add(object); |
| 170 | + } |
| 171 | + |
| 172 | + public void eliminarDeLaCola(int index) throws IndexOutOfBoundsException{ |
| 173 | + if(this.cola.size() < index || index < 0){ |
| 174 | + throw new IndexOutOfBoundsException("El índice no puede ser superior al tamaño de la cola o negativo."); |
| 175 | + |
| 176 | + } else { |
| 177 | + this.cola.remove(index); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public String toString(){ |
| 183 | + StringBuilder sb = new StringBuilder("\nContenido de la cola:\n"); |
| 184 | + |
| 185 | + this.cola.forEach(objeto->sb.append(objeto).append("\n")); |
| 186 | + |
| 187 | + return sb.toString(); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + |
0 commit comments