Roke98

Ejercicio4-Tp3-Unju

Oct 11th, 2022
767
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 KB | None | 0 0
  1. package Practico3;
  2. import java.util.Scanner;
  3.  
  4. public class Ejercicio4 {
  5.     static Scanner sn = new Scanner(System.in);
  6.     public static void main(String[] args) {
  7.         // TODO Auto-generated method stub
  8.         menu();
  9.     }
  10.    
  11.     public static void menu() {
  12.         Queue<Character> cola = null;
  13.         Queue<Character> colaCif = null;
  14.         Queue<Character> colaDec = null;
  15.         int opcion;
  16.         String mensaje;
  17.         do {
  18.             System.out.println("MENU PRINCPAL");
  19.             System.out.println("-------------------------------------");
  20.             System.out.println("1- Ingresar un Mensaje o texto");
  21.             System.out.println("2- Generar un Mensaje o texto");
  22.             System.out.println("3- Finalizar");
  23.             System.out.println("-------------------------------------");
  24.             opcion = Helper.getInteger("Ingrese una opcion");
  25.             switch(opcion) {
  26.             case 1:
  27.                 System.out.println("Ingrese un mensaje para cifrar o decifrar");
  28.                 mensaje = sn.nextLine();
  29.                 cola = new Queue<>(mensaje.length());
  30.                 colaCif = new Queue<>(mensaje.length());
  31.                 colaDec = new Queue<>(mensaje.length());
  32.                 cola = cargarCola(cola, mensaje);
  33.                 System.out.println("El mensaje ingresado es " + "[" + mensaje + "]");
  34.                
  35.                 menu2(cola, colaCif, colaDec,mensaje);
  36.                 break;
  37.             case 2:
  38.                 mensaje = generarMsj();
  39.                 cola = new Queue<>(mensaje.length());
  40.                 colaCif = new Queue<>(mensaje.length());
  41.                 colaDec = new Queue<>(mensaje.length());
  42.                 cola = cargarCola(cola, mensaje);
  43.                 System.out.println("El mensaje generado es " + "[" + mensaje + "]");
  44.                
  45.                 menu2(cola, colaCif, colaDec, mensaje);
  46.                 break;
  47.             case 3:
  48.                 break;
  49.             default:
  50.                 System.out.println("opcion no valida");
  51.             }
  52.            
  53.         }while(opcion!=3);
  54.         System.out.println("gracias :D");
  55.     }
  56.    
  57.     public static void menu2(Queue<Character> cola1, Queue<Character> cola2, Queue<Character>cola3, String msj) {
  58.         int opcion;
  59.         String msjCif, msjDec;
  60.         System.out.println("..............................");
  61.         System.out.println("1) Cifrar (codificar)");
  62.         System.out.println("2) Decifrar (decodificar)");
  63.         System.out.println("..............................");
  64.         opcion = Helper.getPositiveInt("Ingrese una opcion");
  65.            
  66.         switch(opcion){
  67.         case 1:
  68.             msjCif = cifrar(cola1, cola2);
  69.             System.out.println("El mensaje cifrado es " + "[" + msjCif + "]");
  70.             espera();
  71.             break;
  72.         case 2:
  73.             msjDec = decifrar(cola1, cola3);
  74.             System.out.println("El mensaje decifrado es " + "[" + msjDec + "]");
  75.             espera();
  76.             break;
  77.         default:
  78.             System.out.println("Opcion no valida");
  79.         }
  80.     }
  81.    
  82.     public static String decifrar(Queue<Character> cola1, Queue<Character> cola3){
  83.         char aux1, aux2;
  84.         String msjDec="";
  85.         do {
  86.             aux1 = cola1.poll();
  87.             aux2 = (char)((int)aux1-3);
  88.             cola3.offer(aux2);
  89.         }while(!cola3.isFull());
  90.        
  91.         while(!cola3.isEmpty()) {
  92.             msjDec += cola3.poll();
  93.         }
  94.        
  95.         return msjDec;
  96.     }
  97.    
  98.     public static String cifrar(Queue<Character> cola1, Queue<Character> cola2){
  99.         char aux1, aux2;
  100.         String msjCif ="";
  101.         do {
  102.             aux1 = cola1.poll();
  103.             aux2 = (char)((int)aux1+3);
  104.             cola2.offer(aux2);
  105.         }while(!cola2.isFull());
  106.        
  107.         while(!cola2.isEmpty()) {
  108.             msjCif += cola2.poll();
  109.         }
  110.         return msjCif;
  111.     }
  112.    
  113.     public static Queue<Character> cargarCola(Queue<Character> cola, String msj){
  114.         for (int i = 0; i < msj.length(); i++) {
  115.             cola.offer(msj.charAt(i));
  116.         }
  117.        
  118.         return cola;
  119.     }
  120.    
  121.     public static String generarMsj() {
  122.         String[] msj = {"Hola mundo", "Cara o cruz", "Frqghqdgr#d#pxhuwh", "Krod#pxqgr",
  123.                         "El tesoro esta en ...", "Kd|#doerqgljdv#sdud#od#fhqd", "Od#wduwd#hv#xqd#phqwlud",
  124.                         "El precio no es negociable", "Vdfulilflrv#hq#srv#gh#xq#elhq#pd|ru#ÂR#qrB"};
  125.         int i = Helper.randomInt(msj.length);
  126.         return msj[i];
  127.     }
  128.    
  129.     @SuppressWarnings("resource")
  130.     public static void espera (){ //Este modulo detiene el proceso hasta que el ususario precione "enter"
  131.         Scanner waitForKeypress = new Scanner(System.in);
  132.         System.out.print("Presiona la tecla Enter para continuar....");
  133.         waitForKeypress.nextLine();
  134.     }
  135. }
  136.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment