Roke98

Ejercicio3-Tp3-Unju

Oct 15th, 2022 (edited)
1,161
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.18 KB | None | 1 0
  1. package Practico3;
  2.  
  3. import java.util.Random;
  4.  
  5. public class Ejercicio3 {
  6.     static Random aleatorio = new Random();
  7.     public static void main(String[] args) {
  8.         // TODO Auto-generated method stub
  9.         principal();
  10.     }
  11.    
  12.     public static void principal() {
  13.         int opcion;
  14.         Queue<Integer> cola1 = null;
  15.         Queue<Character> cola2 = null;
  16.         Queue<Object> union = null;
  17.        
  18.         do {
  19.             cola1 = menuColaInt();
  20.             //System.out.println(cola1);
  21.             cola2 = menuColaChar();
  22.             //System.out.println(cola2);
  23.             union = new Queue<>(cola1.size() + cola2.size());
  24.             union = union(cola1, cola2);
  25.             System.out.println("\nLA UNION DE LA DOS COLAS ES:\n" + union.toString());
  26.             opcion = Helper.getPositiveInt("Desea cargar otras colas??(Si=1//No=2) ");
  27.         }while (opcion!=2);
  28.         System.out.println("Gracias :D");
  29.     }
  30.    
  31.     public static Queue<Object> union(Queue<Integer> cola1, Queue<Character> cola2){
  32.         Queue<Object> result = new Queue<>(cola1.size() + cola2.size());
  33.         boolean  aux = true;
  34.        
  35.         while (!cola1.isEmpty()||!cola2.isEmpty()) {
  36.             if(cola1.isEmpty()) {
  37.                 aux = false;
  38.             }
  39.            
  40.             if(cola2.isEmpty()) {
  41.                 aux = true;
  42.             }
  43.            
  44.             if(!cola1.isEmpty() && aux){
  45.                 if(!result.contains(cola1.peek())) {
  46.                     result.offer(cola1.pool());
  47.                     aux = false;
  48.                 }else {
  49.                     cola1.remove();
  50.                 }
  51.                 continue;  
  52.             }
  53.            
  54.             if(!cola2.isEmpty() && !aux){
  55.                 if (!result.contains(cola2.peek())){
  56.                     result.offer(cola2.pool());
  57.                     aux = true;
  58.                 }else {
  59.                     cola2.remove();
  60.                 }
  61.             }
  62.         }
  63.         return result;
  64.     }
  65.    
  66.     public static Queue<Integer> menuColaInt() {
  67.         Queue<Integer> colaAux =null;
  68.         int opcion;
  69.         do {
  70.             System.out.println("CARGAR COLA DE NUMEROS ENTEROS");
  71.             System.out.println("1) Cargar manualmente");
  72.             System.out.println("2) Generar valores aleatorios");
  73.             opcion = Helper.getPositiveInt("Ingrese una opcion");
  74.             switch(opcion) {
  75.             case 1:
  76.                 colaAux = new Queue<>(Helper.getPositiveInt("Ingrese el tamaΓ±o de la cola"));
  77.                 colaAux = cargarColaInt(colaAux);
  78.                 System.out.println("Cola de enteros cargada: " + colaAux.toString());
  79.                 break;
  80.             case 2:
  81.                 colaAux = new Queue<>();
  82.                 colaAux = generarColaInt(colaAux);
  83.                 System.out.println("Cola de enteros generada: " + colaAux.toString());
  84.                 break;
  85.             default:
  86.                 System.out.println("opcion no valida");
  87.             }
  88.         }while(opcion!=1 && opcion!=2);
  89.  
  90.         return colaAux;    
  91.     }
  92.    
  93.     public static Queue<Character> menuColaChar() {
  94.         Queue<Character> colaAux =null;
  95.         int opcion;
  96.         do {
  97.             System.out.println("CARGAR COLA DE LETRAS O CARACTERES");
  98.             System.out.println("1) Cargar manualmente");
  99.             System.out.println("2) Generar letras aleatorios");
  100.             opcion = Helper.getPositiveInt("Ingrese una opcion");
  101.             switch(opcion) {
  102.             case 1:
  103.                 colaAux = new Queue<>(Helper.getPositiveInt("Ingrese el tamaΓ±o de la cola"));
  104.                 colaAux = cargarColaChar(colaAux);
  105.                 System.out.println("Cola de caracteres cargada: " + colaAux.toString());
  106.                 break;
  107.             case 2:
  108.                 colaAux = new Queue<>();
  109.                 colaAux = generarColaChar(colaAux);
  110.                 System.out.println("Cola de caracteres generada: " + colaAux.toString());
  111.                 break;
  112.             default:
  113.                 System.out.println("opcion no valida");
  114.             }
  115.         }while(opcion!=1 && opcion!=2);
  116.  
  117.         return colaAux;
  118.     }
  119.    
  120.     public static Queue<Integer> cargarColaInt(Queue<Integer> queue){
  121.         do {
  122.             queue.offer(Helper.getPositiveInt("Ingrese un numero"));
  123.         }while(!queue.isFull());
  124.         return queue;
  125.     }
  126.    
  127.     public static Queue<Character> cargarColaChar(Queue<Character>queue){
  128.         do {
  129.             queue.offer(Helper.getCharacter("Ingrese un caracter"));
  130.         }while(!queue.isFull());
  131.         return queue;
  132.     }
  133.    
  134.     public static Queue<Integer> generarColaInt(Queue<Integer> queue){
  135.         do {
  136.             queue.offer(Helper.randomInt(100));
  137.         }while(!queue.isFull());
  138.         return queue;
  139.     }
  140.    
  141.     public static Queue<Character> generarColaChar(Queue<Character> queue){
  142.         do {
  143.             char aux = (char) (aleatorio.nextInt(26)+'a');
  144.             queue.offer(aux);
  145.         }while(!queue.isFull());
  146.         return queue;
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment