jtentor icon

LinkedList 1er parte - CasoEjemplo_a.java

jtentor | PRO | 10/17/20 11:54:40 AM UTC | 0 ⭐ | 1868 👁️ | Never ⏰ | []
Java |

4.19 KB

|

None

|

0 👍

/

0 👎

//
// Created by Julio Tentor <[email protected]>
//
 
import java.util.Iterator;
 
public class CasoEjemplo_a {
 
    private ILinkedList<Product> products;
 
    public void Run() {
 
        products = new SimpleLinkedList<>();
 
        getData();
        System.out.println("Lista de productos: " + products.toString());
 
//        System.out.println("\n\nLista de productos (Iterable): ");
//        for (Product p : products) {
//            System.out.println(p.toString());
//        }
 
//        System.out.println("\n\nLista de productos (Iterable en reversa): ");
//        Iterator<Product> it = products.iteratorBack();
//        while (it.hasNext()) {
//            System.out.println(it.next().toString());
//        }
 
    }
 
    private void getData() {
 
        Integer option;
        while (true) {
            System.out.println(
                    "\nTrabajo Práctico Nº 4 - Caso Ejempo a)\n" +
                            "\nOpciones" +
                            "\n 1. Ingresa valores por consola" +
                            "\n 2. Genera valores aleatorios"
            );
            option = Helper.getInteger("\nSu opción: ");
 
            switch (option) {
                case 1 :
                    consoleInput();
                    return;
                case 2 :
                    randomGenerate();
                    return;
            }
        }
    }
 
    private void consoleInput() {
        Integer code;
        String description;
        Float salePrice;
        int position;
 
        System.out.println("\nIngrese los datos de los productos");
        do {
            while (true) {
                code = Helper.getInteger("Código..........: ");
                if (code > 0) {
                    break;
                }
                System.out.println("Ingrese un código válido...");
            }
 
            while (true) {
                System.out.print("Descripción.....: ");
                description = Helper.scanner.nextLine();
                if (!description.isEmpty()) {
                    break;
                }
                System.out.println("Ingrese una descripción válida...");
            }
 
            while(true) {
                salePrice = Helper.getFloat("Precio de Venta.: ");
                if (salePrice > 0f) {
                    break;
                }
                System.out.println("Ingrese un precio válido ...");
            }
 
            while (true) {
                position  = Helper.getInteger("\nDonde agrega el producto [1. Adelante,  2. Final] ");
                if (position == 1) {
                    products.addFirst( new Product(code, description, salePrice) );
                    break;
                } else {
                    if (position == 2) {
                        products.addLast( new Product(code, description, salePrice) );
                        break;
                    }
                }
                System.out.println("Ingrese una posición correcta ...");
            }
 
        } while (Character.toUpperCase(Helper.getCharacter("Ingresa otro producto (S/N): ")) != 'N');
    }
 
    private void randomGenerate() {
        Integer code;
        String description;
        Float salePrice;
        int position;
 
        for (int count = Helper.random.nextInt(20) + 1; count > 0; --count) {
            code = Helper.random.nextInt(2000) + 1;
            description = descriptions[Helper.random.nextInt(descriptions.length)];
            salePrice = Helper.random.nextFloat() * 120;
            position = Helper.random.nextInt(2) + 1;
            if (position == 1) {
                products.addFirst( new Product(code, description, salePrice) );
            } else {
                if (position == 2) {
                    products.addLast( new Product(code, description, salePrice) );
                }
            }
        }
    }
 
    static String [] descriptions = new String[] {
            "Yoghurt firme",
            "Leche chocolatada",
            "Manteca",
            "Leche larga vida",
            "Dulce de leche",
            "Leche fortificada"
    };
 
}
 

Comments