Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Practico3;
- import java.util.Scanner;
- public class Ejercicio4 {
- static Scanner sn = new Scanner(System.in);
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- menu();
- }
- public static void menu() {
- Queue<Character> cola = null;
- Queue<Character> colaCif = null;
- Queue<Character> colaDec = null;
- int opcion;
- String mensaje;
- do {
- System.out.println("MENU PRINCPAL");
- System.out.println("-------------------------------------");
- System.out.println("1- Ingresar un Mensaje o texto");
- System.out.println("2- Generar un Mensaje o texto");
- System.out.println("3- Finalizar");
- System.out.println("-------------------------------------");
- opcion = Helper.getInteger("Ingrese una opcion");
- switch(opcion) {
- case 1:
- System.out.println("Ingrese un mensaje para cifrar o decifrar");
- mensaje = sn.nextLine();
- cola = new Queue<>(mensaje.length());
- colaCif = new Queue<>(mensaje.length());
- colaDec = new Queue<>(mensaje.length());
- cola = cargarCola(cola, mensaje);
- System.out.println("El mensaje ingresado es " + "[" + mensaje + "]");
- menu2(cola, colaCif, colaDec,mensaje);
- break;
- case 2:
- mensaje = generarMsj();
- cola = new Queue<>(mensaje.length());
- colaCif = new Queue<>(mensaje.length());
- colaDec = new Queue<>(mensaje.length());
- cola = cargarCola(cola, mensaje);
- System.out.println("El mensaje generado es " + "[" + mensaje + "]");
- menu2(cola, colaCif, colaDec, mensaje);
- break;
- case 3:
- break;
- default:
- System.out.println("opcion no valida");
- }
- }while(opcion!=3);
- System.out.println("gracias :D");
- }
- public static void menu2(Queue<Character> cola1, Queue<Character> cola2, Queue<Character>cola3, String msj) {
- int opcion;
- String msjCif, msjDec;
- System.out.println("..............................");
- System.out.println("1) Cifrar (codificar)");
- System.out.println("2) Decifrar (decodificar)");
- System.out.println("..............................");
- opcion = Helper.getPositiveInt("Ingrese una opcion");
- switch(opcion){
- case 1:
- msjCif = cifrar(cola1, cola2);
- System.out.println("El mensaje cifrado es " + "[" + msjCif + "]");
- espera();
- break;
- case 2:
- msjDec = decifrar(cola1, cola3);
- System.out.println("El mensaje decifrado es " + "[" + msjDec + "]");
- espera();
- break;
- default:
- System.out.println("Opcion no valida");
- }
- }
- public static String decifrar(Queue<Character> cola1, Queue<Character> cola3){
- char aux1, aux2;
- String msjDec="";
- do {
- aux1 = cola1.poll();
- aux2 = (char)((int)aux1-3);
- cola3.offer(aux2);
- }while(!cola3.isFull());
- while(!cola3.isEmpty()) {
- msjDec += cola3.poll();
- }
- return msjDec;
- }
- public static String cifrar(Queue<Character> cola1, Queue<Character> cola2){
- char aux1, aux2;
- String msjCif ="";
- do {
- aux1 = cola1.poll();
- aux2 = (char)((int)aux1+3);
- cola2.offer(aux2);
- }while(!cola2.isFull());
- while(!cola2.isEmpty()) {
- msjCif += cola2.poll();
- }
- return msjCif;
- }
- public static Queue<Character> cargarCola(Queue<Character> cola, String msj){
- for (int i = 0; i < msj.length(); i++) {
- cola.offer(msj.charAt(i));
- }
- return cola;
- }
- public static String generarMsj() {
- String[] msj = {"Hola mundo", "Cara o cruz", "Frqghqdgr#d#pxhuwh", "Krod#pxqgr",
- "El tesoro esta en ...", "Kd|#doerqgljdv#sdud#od#fhqd", "Od#wduwd#hv#xqd#phqwlud",
- "El precio no es negociable", "Vdfulilflrv#hq#srv#gh#xq#elhq#pd|ru#ÂR#qrB"};
- int i = Helper.randomInt(msj.length);
- return msj[i];
- }
- @SuppressWarnings("resource")
- public static void espera (){ //Este modulo detiene el proceso hasta que el ususario precione "enter"
- Scanner waitForKeypress = new Scanner(System.in);
- System.out.print("Presiona la tecla Enter para continuar....");
- waitForKeypress.nextLine();
- }
- }
Advertisement