Last active
August 24, 2016 16:14
-
-
Save alvareztech/6f3922f6492c036996fb4b6c60f61e35 to your computer and use it in GitHub Desktop.
Ejercicio 1: Pilas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez; | |
public class Libro { | |
private String titulo; | |
private String autor; | |
public Libro() { | |
this.titulo = ""; | |
this.autor = ""; | |
} | |
public Libro(String titulo, String autor) { | |
this.titulo = titulo; | |
this.autor = autor; | |
} | |
public String getTitulo() { | |
return titulo; | |
} | |
public void setTitulo(String titulo) { | |
this.titulo = titulo; | |
} | |
public String getAutor() { | |
return autor; | |
} | |
public void setAutor(String autor) { | |
this.autor = autor; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez; | |
import java.util.Scanner; | |
import java.util.Stack; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.print("Introduzca número de libros: "); | |
int n = Integer.parseInt(scanner.nextLine()); // convierte String a int (evita problema de lectura) | |
Stack<Libro> pila = new Stack<Libro>(); | |
for (int i = 0; i < n; i++) { | |
System.out.print(" Título: "); | |
String titulo = scanner.nextLine(); | |
System.out.print(" Autor: "); | |
String autor = scanner.nextLine(); | |
Libro li = new Libro(titulo, autor); | |
pila.push(li); | |
} | |
System.out.println("Pila"); | |
while (!pila.isEmpty()) { | |
Libro a = pila.pop(); | |
System.out.println(" Titulo: " + a.getTitulo()); | |
System.out.println(" Autor: " + a.getAutor()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment