Advertisement
jtentor

LinkedList 1er parte - CasoEjemplo_a.java

Oct 17th, 2020
1,230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.19 KB | None | 0 0
  1. //
  2. // Created by Julio Tentor <jtentor@fi.unju.edu.ar>
  3. //
  4.  
  5. import java.util.Iterator;
  6.  
  7. public class CasoEjemplo_a {
  8.  
  9.     private ILinkedList<Product> products;
  10.  
  11.     public void Run() {
  12.  
  13.         products = new SimpleLinkedList<>();
  14.  
  15.         getData();
  16.         System.out.println("Lista de productos: " + products.toString());
  17.  
  18. //        System.out.println("\n\nLista de productos (Iterable): ");
  19. //        for (Product p : products) {
  20. //            System.out.println(p.toString());
  21. //        }
  22.  
  23. //        System.out.println("\n\nLista de productos (Iterable en reversa): ");
  24. //        Iterator<Product> it = products.iteratorBack();
  25. //        while (it.hasNext()) {
  26. //            System.out.println(it.next().toString());
  27. //        }
  28.  
  29.     }
  30.  
  31.     private void getData() {
  32.  
  33.         Integer option;
  34.         while (true) {
  35.             System.out.println(
  36.                     "\nTrabajo Práctico Nº 4 - Caso Ejempo a)\n" +
  37.                             "\nOpciones" +
  38.                             "\n 1. Ingresa valores por consola" +
  39.                             "\n 2. Genera valores aleatorios"
  40.             );
  41.             option = Helper.getInteger("\nSu opción: ");
  42.  
  43.             switch (option) {
  44.                 case 1 :
  45.                     consoleInput();
  46.                     return;
  47.                 case 2 :
  48.                     randomGenerate();
  49.                     return;
  50.             }
  51.         }
  52.     }
  53.  
  54.     private void consoleInput() {
  55.         Integer code;
  56.         String description;
  57.         Float salePrice;
  58.         int position;
  59.  
  60.         System.out.println("\nIngrese los datos de los productos");
  61.         do {
  62.             while (true) {
  63.                 code = Helper.getInteger("Código..........: ");
  64.                 if (code > 0) {
  65.                     break;
  66.                 }
  67.                 System.out.println("Ingrese un código válido...");
  68.             }
  69.  
  70.             while (true) {
  71.                 System.out.print("Descripción.....: ");
  72.                 description = Helper.scanner.nextLine();
  73.                 if (!description.isEmpty()) {
  74.                     break;
  75.                 }
  76.                 System.out.println("Ingrese una descripción válida...");
  77.             }
  78.  
  79.             while(true) {
  80.                 salePrice = Helper.getFloat("Precio de Venta.: ");
  81.                 if (salePrice > 0f) {
  82.                     break;
  83.                 }
  84.                 System.out.println("Ingrese un precio válido ...");
  85.             }
  86.  
  87.             while (true) {
  88.                 position  = Helper.getInteger("\nDonde agrega el producto [1. Adelante,  2. Final] ");
  89.                 if (position == 1) {
  90.                     products.addFirst( new Product(code, description, salePrice) );
  91.                     break;
  92.                 } else {
  93.                     if (position == 2) {
  94.                         products.addLast( new Product(code, description, salePrice) );
  95.                         break;
  96.                     }
  97.                 }
  98.                 System.out.println("Ingrese una posición correcta ...");
  99.             }
  100.  
  101.         } while (Character.toUpperCase(Helper.getCharacter("Ingresa otro producto (S/N): ")) != 'N');
  102.     }
  103.  
  104.     private void randomGenerate() {
  105.         Integer code;
  106.         String description;
  107.         Float salePrice;
  108.         int position;
  109.  
  110.         for (int count = Helper.random.nextInt(20) + 1; count > 0; --count) {
  111.             code = Helper.random.nextInt(2000) + 1;
  112.             description = descriptions[Helper.random.nextInt(descriptions.length)];
  113.             salePrice = Helper.random.nextFloat() * 120;
  114.             position = Helper.random.nextInt(2) + 1;
  115.             if (position == 1) {
  116.                 products.addFirst( new Product(code, description, salePrice) );
  117.             } else {
  118.                 if (position == 2) {
  119.                     products.addLast( new Product(code, description, salePrice) );
  120.                 }
  121.             }
  122.         }
  123.     }
  124.  
  125.     static String [] descriptions = new String[] {
  126.             "Yoghurt firme",
  127.             "Leche chocolatada",
  128.             "Manteca",
  129.             "Leche larga vida",
  130.             "Dulce de leche",
  131.             "Leche fortificada"
  132.     };
  133.  
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement