Advertisement
jtentor

TP1 - Caso Ejemplo d) - CasoEjemplo_d.Java

Sep 3rd, 2020
3,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.79 KB | None | 0 0
  1. // Created by Julio Tentor <jtentor@fi.unju.edu.ar>
  2. //
  3.  
  4. import java.util.Random;
  5. import java.util.Scanner;
  6.  
  7. public class CasoEjemplo_d {
  8.  
  9.     private static Scanner scanner = new Scanner(System.in);
  10.  
  11.  
  12.     public static void main(String[] args) {
  13.         CasoEjemplo_d ejemplo = new CasoEjemplo_d();
  14.         ejemplo.Ejecutar();
  15.     }
  16.  
  17.     public static Character getCharacter(Scanner scanner, String message) {
  18.         Character ch;
  19.         while (true) {
  20.             System.out.print(message);
  21.             try {
  22.                 ch = scanner.nextLine().charAt(0);
  23.                 return ch;
  24.             } catch (Exception exception) {
  25.             }
  26.         }
  27.     }
  28.  
  29.     public void Ejecutar() {
  30.         Libro libro;
  31.         Character option;
  32.  
  33.         System.out.println("Opciones");
  34.         System.out.println(" 1. Ingresa por consola");
  35.         System.out.println(" 2. Generador aleatorio");
  36.         option = Character.toUpperCase(getCharacter(scanner, "\nSu opción: "));
  37.  
  38.         switch (option) {
  39.             case '1':
  40.             case '2':
  41.                 do {
  42.                     libro = (option == '1') ? consoleCreate() : randomCreate();
  43.                     System.out.println(libro);
  44.  
  45.                 } while (Character.toUpperCase(getCharacter(scanner, "\nCrea otro libro (S/N): ")) != 'N');
  46.                 break;
  47.         }
  48.     }
  49.  
  50.     public Libro consoleCreate() {
  51.         String titulo;
  52.         String autor;
  53.         String editorial;
  54.         Integer anioPublicacion;
  55.         Double precio;
  56.  
  57.  
  58.         System.out.print("Ingrese la información del libro\n");
  59.         System.out.print("Título.............: ");
  60.         titulo = scanner.nextLine();
  61.  
  62.         System.out.print("Autor..............: ");
  63.         autor = scanner.nextLine();
  64.  
  65.         System.out.print("Editorial..........: ");
  66.         editorial = scanner.nextLine();
  67.  
  68.         while (true) {
  69.             try {
  70.                 System.out.print("Año de Publicación.: ");
  71.                 anioPublicacion = Integer.parseInt(scanner.nextLine());
  72.                 if (anioPublicacion < 0) {
  73.                     System.out.println("Ingrese un año correcto");
  74.                     continue;
  75.                 }
  76.                 break;
  77.             }
  78.             catch (Exception exception) {
  79.                 System.out.println("Ingrese un año correcto");
  80.             }
  81.         }
  82.  
  83.         while (true) {
  84.             try {
  85.                 System.out.print("Precio.............: ");
  86.                 precio = Double.parseDouble(scanner.nextLine());
  87.                 if (precio < 0.0) {
  88.                     System.out.println("Ingrese un precio correcto");
  89.                     continue;
  90.                 }
  91.                 break;
  92.             }
  93.             catch (Exception exception) {
  94.                 System.out.println("Ingrese un precio correcto");
  95.             }
  96.         }
  97.  
  98.         Libro libro = new Libro(titulo, autor, editorial, anioPublicacion, precio);
  99.  
  100.         return libro;
  101.     }
  102.  
  103.  
  104.     public Libro randomCreate() {
  105.         String titulo;
  106.         String autor;
  107.         String editorial;
  108.         Integer anioPublicacion;
  109.         Double precio;
  110.  
  111.         Random random = new Random();
  112.  
  113.         titulo = algunosTitulos[random.nextInt(algunosTitulos.length)];
  114.         autor = algunosAutores[random.nextInt(algunosAutores.length)];
  115.         editorial = algunasEditoriales[random.nextInt(algunasEditoriales.length)];
  116.         anioPublicacion = random.nextInt(2020);
  117.         precio = random.nextDouble() * 1000;
  118.  
  119.         Libro libro = new Libro(titulo, autor, editorial, anioPublicacion, precio);
  120.  
  121.         return libro;
  122.     }
  123.  
  124.     private static String[] algunosTitulos = {
  125.             "Foundations of Computer Science. C Edition",
  126.             "Estructura de Datos y Algoritmos",
  127.             "Python no Muerde Yo Sí",
  128.             "Curso Python para Principiantes",
  129.             "Organization and Maintenance of Large Ordered Indexes",
  130.             "Algoritmos y Estructura de Datos",
  131.             "Algorithmics Theory and Practice",
  132.             "Fundamentals of Algorithmics",
  133.             "Introduction to Algorithms",
  134.             "Data Structures and Algorithms in Python",
  135.             "C++ How to Progrmam Instructor's Manual Contents",
  136.             "Cómo Programar en C, C++ y Java",
  137.             "Cómo Programar en C#",
  138.             "Java Cómo Programar",
  139.             "Java How to Program",
  140.             "Data Structures and Algorithms in C++",
  141.             "Thinking in C++, Volume 1",
  142.             "Thinking in C++, Volume 2",
  143.             "Estructuras de datos en Java",
  144.             "Estructura de datos en C++",
  145.             "Algorithm Design",
  146.             "The Art of Computer Programming Volume 1 Fundamental Algorithms 2nd Edition",
  147.             "Learning Python",
  148.             "Programming Python",
  149.             "Introduction to Theory of Computation",
  150.             "Fundamentos de Programación en Java",
  151.             "Handbook of Data Structures and Applications",
  152.             "Open Data Structures (in Java)",
  153.             "The C++ Programming Language",
  154.             "Python Tutorial. Release 3.2.3",
  155.             "El tutorial de Python"
  156.     };
  157.  
  158.     private static String[] algunosAutores = {
  159.             "Aho, Alfred V",
  160.             "2Ullman, Jeffrey D",
  161.             "Alsina, Roberto",
  162.             "Bahit, Eugenia",
  163.             "Bayer, Rudolf",
  164.             "Brassard, Gilles",
  165.             "Bratley, Paul",
  166.             "Brucker, Peter",
  167.             "Cabanes, Nacho",
  168.             "Christian Charras",
  169.             "Cormen, Thomas H",
  170.             "Deitel, Harvey M",
  171.             "Deitel, Paul J",
  172.             "Drozdek, Adam",
  173.             "Eckel, Bruce",
  174.             "Joyanes Aguilar, Luis",
  175.             "Zahonero Martínez, Ignacio",
  176.             "Knuth Donald E",
  177.             "Lutz, Mark",
  178.             "Morin, Pat",
  179.             "Sedgewick, Robert",
  180.             "Flajolet, Philippe",
  181.             "Skiena, Steven S",
  182.             "Stroustrup, Bjarne",
  183.             "van Rossum, Guido",
  184.             "Wachenchauzer, Rosita"
  185.     };
  186.  
  187.     private static String[] algunasEditoriales = {
  188.             "Alianza Editorial",
  189.             "Milky Way Edici",
  190.             "Planeta Cómic",
  191.             "Ivrea España",
  192.             "Astiberri Edici",
  193.             "Blackie Books",
  194.             "Editorial Anagr",
  195.             "Nova",
  196.             "Austral Editorial",
  197.             "Alfaguara",
  198.             "Nórdica Libros",
  199.             "Roca Editorial",
  200.             "Alpha Decay",
  201.             "Ediciones Kiwi",
  202.             "Periscopi",
  203.             "Editorial Minú",
  204.             "Zorro Rojo",
  205.             "Gallo Nero Edic",
  206.             "Oreilly",
  207.             "Apress",
  208.             "Packt Publishing",
  209.             "Wrox",
  210.             "Manning Press",
  211.             "Pragmatic BookShelf",
  212.             "SitePoint"
  213.     };
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement