1+ import java .util .Arrays ;
2+ import java .util .Collections ;
3+ import java .util .InputMismatchException ;
4+ import java .util .LinkedList ;
5+ import java .util .List ;
6+ import java .util .Queue ;
7+ import java .util .Scanner ;
8+ import java .util .Stack ;
9+
10+ /**
11+ *
12+ * EJERCICIO:
13+ * Implementa los mecanismos de introducción y recuperación de elementos propios de las
14+ * pilas (stacks - LIFO) y las colas (queue - FIFO) utilizando una estructura de array
15+ * o lista (dependiendo de las posibilidades de tu lenguaje).
16+ *
17+ * @version v1.0
18+ *
19+ * @since 09/07/2024
20+ *
21+ * @author GlossyPath
22+ */
23+
24+ public class GlossyPath {
25+
26+
27+ public static void main (String [] args ) {
28+
29+ System .out .println ("---------PILAS(STACK)--------" ); //Last In, First Out
30+ Stack <String > frutas = new Stack <>();
31+
32+ frutas .push ("melon" );
33+ frutas .push ("fresa" );
34+ frutas .push ("manzana" );
35+
36+ frutas .remove (0 );
37+
38+ String elementoCima = frutas .pop ();
39+
40+ System .out .println (frutas .peek ());
41+
42+ frutas .set (0 , "Melon griego" );
43+
44+ frutas .stream ().forEach (System .out ::println );
45+
46+ System .out .println (frutas .peek ());
47+
48+ Collections .sort (frutas ); //ordenar los objetos
49+
50+ frutas .stream ().forEach (System .out ::println );
51+
52+ System .out .println ("---------COLAS(QUEUE)---------" );//First In, First Out
53+
54+ List <String > nom = Arrays .asList ("Laura" , "Maria" , "Paco" , "Luis" );
55+
56+ Queue <String > nombres = new LinkedList <>();
57+
58+ nombres .addAll (nom );
59+
60+ nombres .add ("Jose" );
61+
62+ System .out .println ("La lista de nombres esta vacia? " + nombres .isEmpty ());
63+
64+ nombres .remove ("Laura" );
65+
66+ while (!nombres .isEmpty ()){
67+ System .out .println (nombres .poll ());
68+ }
69+
70+ /**
71+ * DIFICULTAD EXTRA (opcional):
72+ * - Utilizando la implementación de pila y cadenas de texto, simula el mecanismo adelante/atrás
73+ * de un navegador web. Crea un programa en el que puedas navegar a una página o indicarle
74+ * que te quieres desplazar adelante o atrás, mostrando en cada caso el nombre de la web.
75+ * Las palabras "adelante", "atrás" desencadenan esta acción, el resto se interpreta como
76+ * el nombre de una nueva web.
77+ * - Utilizando la implementación de cola y cadenas de texto, simula el mecanismo de una
78+ * impresora compartida que recibe documentos y los imprime cuando así se le indica.
79+ * La palabra "imprimir" imprime un elemento de la cola, el resto de palabras se
80+ * interpretan como nombres de documentos.
81+ */
82+
83+ System .out .println ("-------DIFICULTAD EXTRA----------" );
84+
85+ Scanner sc = new Scanner (System .in );
86+ int opcion = -1 ;
87+
88+ do {
89+ System .out .println ("Escoge la opción que queires realizar(1,2,3): " +
90+ "\n 1. Navegar por internet." +
91+ "\n 2. Impresora" +
92+ "\n 3. Salir" );
93+
94+ try {
95+ opcion = sc .nextInt ();
96+ sc .nextLine ();
97+
98+ switch (opcion ) {
99+ case 1 :
100+ navegador (sc );
101+ break ;
102+
103+ case 2 :
104+ impresora (sc );
105+ break ;
106+
107+ case 3 :
108+ System .out .println ("Saliendo..." );
109+ break ;
110+
111+ default :
112+ System .out .println ("Opción no válida. Inténtalo de nuevo." );
113+ break ;
114+ }
115+
116+ } catch (InputMismatchException e ) {
117+ System .out .println ("Introduce una opción válida" );
118+ sc .nextLine (); // Limpiar el buffer
119+ }
120+
121+ } while (opcion != 3 );
122+
123+ sc .close ();
124+ }
125+
126+ public static void navegador (Scanner sc ) {
127+
128+ Navegador navegador = new Navegador ();
129+ String input ;
130+
131+ do {
132+ System .out .println ("Introduce la acción que quieres realizar en el navegador (URL, adelante, atras o salir)" );
133+
134+ System .out .print ("URL: " );
135+ input = sc .nextLine ();
136+
137+ if (input .equalsIgnoreCase ("adelante" )) {
138+ navegador .adelante ();
139+
140+ } else if (input .equalsIgnoreCase ("atras" )) {
141+ navegador .atras ();
142+
143+ } else if (!input .equalsIgnoreCase ("salir" )) {
144+ navegador .nuevaPagina (input );
145+ }
146+
147+ navegador .mostrarEstado ();
148+
149+ } while (!input .equalsIgnoreCase ("salir" ));
150+ }
151+
152+ static class Navegador {
153+
154+ private Stack <String > pilaAdelante ;
155+ private Stack <String > pilaAtras ;
156+ private String paginaActual ;
157+
158+ public Navegador () {
159+ pilaAdelante = new Stack <>();
160+ pilaAtras = new Stack <>();
161+ paginaActual = "" ;
162+ }
163+
164+ public boolean hayPaginaDelante () {
165+ return !pilaAdelante .isEmpty ();
166+ }
167+
168+ public boolean hayPaginaDetras () {
169+ return !pilaAtras .isEmpty ();
170+ }
171+
172+ public void nuevaPagina (String url ) {
173+ if (!paginaActual .isEmpty ()) {
174+ pilaAtras .push (paginaActual );
175+ }
176+ paginaActual = url ;
177+ pilaAdelante .clear ();
178+ System .out .println ("Navegando a: " + url );
179+ }
180+
181+ public void adelante () {
182+ if (hayPaginaDelante ()) {
183+ pilaAtras .push (paginaActual );
184+ paginaActual = pilaAdelante .pop ();
185+ System .out .println ("Navegando a: " + paginaActual );
186+ } else {
187+ System .out .println ("No hay páginas adelante." );
188+ }
189+ }
190+
191+ public void atras () {
192+ if (hayPaginaDetras ()) {
193+ pilaAdelante .push (paginaActual );
194+ paginaActual = pilaAtras .pop ();
195+ System .out .println ("Navegando a: " + paginaActual );
196+ } else {
197+ System .out .println ("No hay páginas atrás." );
198+ }
199+ }
200+
201+ public void mostrarEstado () {
202+ System .out .println ("Página actual: " + paginaActual );
203+ System .out .println ("Páginas atrás: " + pilaAtras );
204+ System .out .println ("Páginas adelante: " + pilaAdelante );
205+ }
206+ }
207+
208+ public static void impresora (Scanner sc ){
209+
210+ String accion ;
211+
212+ Impresora impresora = new Impresora ();
213+
214+ do {
215+ System .out .println ("Introduce la acción que quieres realizar en la impresora (Imprimir o salir)" );
216+ accion = sc .nextLine ();
217+
218+ if (accion .equalsIgnoreCase ("Imprimir" )){
219+ impresora .imprimir ();
220+
221+ } else if (!accion .equalsIgnoreCase ("salir" )){
222+ impresora .añadirDocumento (accion );
223+ }
224+
225+ impresora .mostrarTodo ();
226+
227+ } while (!accion .equalsIgnoreCase ("salir" ));
228+ }
229+
230+ static class Impresora {
231+ private Queue <String > colaImpresion ;
232+
233+
234+ public Impresora (){
235+ colaImpresion = new LinkedList <>();
236+ }
237+
238+
239+ public void imprimir (){
240+ if (hayDocumento ()){
241+ String impresion = this .colaImpresion .remove ();
242+ System .out .println ("Imprimiendo: " + impresion );
243+ }
244+ }
245+
246+
247+ public boolean hayDocumento (){
248+ return !colaImpresion .isEmpty ();
249+ }
250+
251+
252+ public void añadirDocumento (String documento ){
253+ colaImpresion .add (documento );
254+ }
255+
256+ public void mostrarTodo (){
257+
258+ System .out .println ("Cola de impresion" );
259+ for (String s : this .colaImpresion ){
260+ System .out .println (s );
261+ }
262+
263+
264+
265+ }
266+ }
267+ }
0 commit comments