Leonel_G

Tp4Ej5

Oct 24th, 2021
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.17 KB | None | 0 0
  1. import java.util.Iterator;
  2. import java.util.Scanner;
  3. public class Tp4Ej5 {
  4.     public static void main(String[] args) {
  5.         SimpleLinkedList <Contacto> lista = new SimpleLinkedList<Contacto>();
  6.         Scanner entrada = new Scanner(System.in);
  7.         int opcion =0,contador=0; String buscar;
  8.         while(opcion!=4){
  9.             switch(opcion){
  10.                 case 1:
  11.                     cargarContacto(entrada,lista);
  12.                    
  13.                     break;
  14.                 case 2:
  15.                     if (lista.count!=0){
  16.                         buscar = Helper.cargarLetra(entrada, "ingrese nombre a buscar");
  17.                         for (Contacto n: lista){
  18.                             if(n.getNombre().contains(buscar)){
  19.                                 System.out.println(n);
  20.                             }
  21.                         }
  22.                     }
  23.                     break;
  24.                 case 3:
  25.                     if(lista.count!=0){
  26.                         buscar=mostrarContacto(entrada,lista);
  27.                         for (Contacto n: lista){
  28.                             if(n.getTrabajo().equalsIgnoreCase(buscar)){
  29.                                 contador++;
  30.                             }
  31.                         }
  32.                     System.out.println("Trabajo: "+buscar+" Empleados: "+contador);    
  33.                     }
  34.                     break;
  35.             }
  36.             opcion = Helper.validarNumero(entrada, "Elegir Opcion\n1-cargar contactos\n2-buscar contacto\n3-mostrar informacion de contacto\n4-salir ");
  37.         }
  38.     }
  39.     //Selecciona Opcion para cargar Manual o Random
  40.     public static void cargarContacto(Scanner entrada, SimpleLinkedList lista){
  41.         int opcion=0;
  42.         while(opcion!=3){
  43.             switch(opcion){
  44.                 case 1:
  45.                     cargarContactoManual(entrada, lista);
  46.                     break;
  47.                 case 2:
  48.                     cargarContactoRandom(entrada, lista);
  49.                     break;
  50.             }
  51.             opcion = Helper.validarNumero(entrada, "Elegir Opcion\n1-manual\n2-random\n3-salir ");
  52.         }
  53.         Iterator <Contacto> iter = lista.iterator();
  54.         while(iter.hasNext()){
  55.             System.out.println(iter.next().getNombre());
  56.         }
  57.     }
  58.     //carga Manual
  59.     public static void cargarContactoManual(Scanner entrada, SimpleLinkedList lista) {
  60.         Contacto contacto;
  61.         String nombre,trabajo,telefono,correo;
  62.         int continuar=0;
  63.         while(continuar!=2){
  64.             nombre = Helper.cargarLetra(entrada, "cargar nombre");
  65.             trabajo = Helper.cargarLetra(entrada, "cargar trabajo");
  66.             telefono = cargarTelefono(entrada,lista);
  67.             correo = cargarCorreo(entrada, lista);
  68.             contacto = new Contacto(nombre, trabajo, telefono, correo);
  69.             lista.addFirst(contacto);
  70.             continuar = Helper.validarNumero(entrada,"¿seguir cargando contacto?\n1-si\n2-no");
  71.         }
  72.        
  73.     }
  74.     //carga Random
  75.     public static void cargarContactoRandom(Scanner entrada, SimpleLinkedList lista) {
  76.         Contacto contacto;
  77.         String nombre,trabajo,telefono,correo;
  78.         int continuar=0;
  79.         while(continuar!=2){
  80.             nombre = Helper.nombreRandom();
  81.             trabajo = trabajoRandom();
  82.             telefono = Integer.toString((int) (Math.random() * 1000000000));
  83.             correo = Helper.correoRandom();
  84.             contacto = new Contacto(nombre, trabajo, telefono, correo);
  85.             lista.addFirst(contacto);
  86.             continuar = Helper.validarNumero(entrada,"¿seguir cargando contacto?\n1-si\n2-no");
  87.         }
  88.        
  89.     }
  90.     //Metodo que valida si el telefono se repite
  91.     public static String cargarTelefono(Scanner entrada, SimpleLinkedList lista) {
  92.         String numeroDeTelefono;
  93.         while(true){
  94.             numeroDeTelefono= cargarNumeroTelefono(entrada, "ingresar numero de telefono");
  95.             if (lista.count==0){
  96.                 return numeroDeTelefono;
  97.             }
  98.             Iterator <Contacto> iter = lista.iterator();
  99.             while(iter.hasNext()){
  100.                 if (iter.next().getTelefono().equals(numeroDeTelefono)){
  101.                     System.out.println("el numero ya esta registrado");
  102.                     break;
  103.                 }
  104.             return numeroDeTelefono;
  105.         }
  106.         }
  107.     }
  108.     //Metodo que validar que se ingrese solamente numeros(tipo String) primera parte
  109.     public static String cargarNumeroTelefono(Scanner entrada,String mensaje){
  110.         System.out.println(mensaje);
  111.         String linea=entrada.nextLine();
  112.         while((validarNumeroTelefono(linea)) && (linea.length()<12)){
  113.             System.out.println("''ERROR'' No Ingresar Letras");
  114.             System.out.println(mensaje);
  115.             linea=entrada.nextLine();
  116.         }    
  117.         return linea;
  118.     }
  119.     //Metodo que validar que se ingrese solamente numeros(tipo String) segunda parte
  120.     public static boolean validarNumeroTelefono(String linea) {
  121.         for (int x = 0; x < linea.length(); x++) {
  122.             char c = linea.charAt(x);
  123.             if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == ' ') {
  124.                 return true;
  125.             }
  126.         }
  127.         return false;
  128.     }
  129.     //Metodo que valida si el correo se repite
  130.     public static String cargarCorreo(Scanner entrada, SimpleLinkedList lista) {
  131.         String correo;
  132.         while(true){
  133.             correo= validarCorreo(entrada);
  134.             if (lista.count==0){
  135.                 return correo;
  136.             }
  137.             System.out.println(lista);
  138.             Iterator <Contacto> iter = lista.iterator();
  139.             while(iter.hasNext()){
  140.                 if (iter.next().getCorreo().equals(correo)){
  141.                     System.out.println("el correo ya esta registrado");
  142.                     break;
  143.                 }
  144.             return correo;
  145.         }
  146.         }
  147.     }
  148.     //Metod que valida si el correo contiene @ y .
  149.     public static String validarCorreo(Scanner entrada) {
  150.         String correo;
  151.         System.out.println("ingresar correo con @ y .");
  152.         correo = entrada.nextLine();
  153.         while (!(correo.contains("@") && correo.contains("."))){
  154.             System.out.println("ingresar correo con @ y .");
  155.             correo = entrada.nextLine();
  156.         }
  157.         return correo;
  158.     }
  159.     //Metodo que ingresa un trabajo de manera random
  160.     public static String trabajoRandom(){
  161.         int random;
  162.         String [] trabajo ={"panadero","cocinero","doctor","ingeniero",
  163.         "policia","comerciante","actor","secretario","municipal","politico"};
  164.         random = (int) (Math.random() * trabajo.length);
  165.         return trabajo[random];
  166.     }
  167.     //Metodo que muestra la lista
  168.     public static String mostrarContacto(Scanner entrada, SimpleLinkedList lista){
  169.         String trabajo;
  170.         Iterator <Documento> iter = lista.iterator();
  171.         System.out.println("''''''");
  172.         while(iter.hasNext()){
  173.             System.out.println(iter.next());
  174.         }
  175.         System.out.println("''''''");
  176.         trabajo = Helper.cargarLetra(entrada, "ingrese trabajo");
  177.         return trabajo;
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment