Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Iterator;
- import java.util.Scanner;
- public class Tp4Ej5 {
- public static void main(String[] args) {
- SimpleLinkedList <Contacto> lista = new SimpleLinkedList<Contacto>();
- Scanner entrada = new Scanner(System.in);
- int opcion =0,contador=0; String buscar;
- while(opcion!=4){
- switch(opcion){
- case 1:
- cargarContacto(entrada,lista);
- break;
- case 2:
- if (lista.count!=0){
- buscar = Helper.cargarLetra(entrada, "ingrese nombre a buscar");
- for (Contacto n: lista){
- if(n.getNombre().contains(buscar)){
- System.out.println(n);
- }
- }
- }
- break;
- case 3:
- if(lista.count!=0){
- buscar=mostrarContacto(entrada,lista);
- for (Contacto n: lista){
- if(n.getTrabajo().equalsIgnoreCase(buscar)){
- contador++;
- }
- }
- System.out.println("Trabajo: "+buscar+" Empleados: "+contador);
- }
- break;
- }
- opcion = Helper.validarNumero(entrada, "Elegir Opcion\n1-cargar contactos\n2-buscar contacto\n3-mostrar informacion de contacto\n4-salir ");
- }
- }
- //Selecciona Opcion para cargar Manual o Random
- public static void cargarContacto(Scanner entrada, SimpleLinkedList lista){
- int opcion=0;
- while(opcion!=3){
- switch(opcion){
- case 1:
- cargarContactoManual(entrada, lista);
- break;
- case 2:
- cargarContactoRandom(entrada, lista);
- break;
- }
- opcion = Helper.validarNumero(entrada, "Elegir Opcion\n1-manual\n2-random\n3-salir ");
- }
- Iterator <Contacto> iter = lista.iterator();
- while(iter.hasNext()){
- System.out.println(iter.next().getNombre());
- }
- }
- //carga Manual
- public static void cargarContactoManual(Scanner entrada, SimpleLinkedList lista) {
- Contacto contacto;
- String nombre,trabajo,telefono,correo;
- int continuar=0;
- while(continuar!=2){
- nombre = Helper.cargarLetra(entrada, "cargar nombre");
- trabajo = Helper.cargarLetra(entrada, "cargar trabajo");
- telefono = cargarTelefono(entrada,lista);
- correo = cargarCorreo(entrada, lista);
- contacto = new Contacto(nombre, trabajo, telefono, correo);
- lista.addFirst(contacto);
- continuar = Helper.validarNumero(entrada,"¿seguir cargando contacto?\n1-si\n2-no");
- }
- }
- //carga Random
- public static void cargarContactoRandom(Scanner entrada, SimpleLinkedList lista) {
- Contacto contacto;
- String nombre,trabajo,telefono,correo;
- int continuar=0;
- while(continuar!=2){
- nombre = Helper.nombreRandom();
- trabajo = trabajoRandom();
- telefono = Integer.toString((int) (Math.random() * 1000000000));
- correo = Helper.correoRandom();
- contacto = new Contacto(nombre, trabajo, telefono, correo);
- lista.addFirst(contacto);
- continuar = Helper.validarNumero(entrada,"¿seguir cargando contacto?\n1-si\n2-no");
- }
- }
- //Metodo que valida si el telefono se repite
- public static String cargarTelefono(Scanner entrada, SimpleLinkedList lista) {
- String numeroDeTelefono;
- while(true){
- numeroDeTelefono= cargarNumeroTelefono(entrada, "ingresar numero de telefono");
- if (lista.count==0){
- return numeroDeTelefono;
- }
- Iterator <Contacto> iter = lista.iterator();
- while(iter.hasNext()){
- if (iter.next().getTelefono().equals(numeroDeTelefono)){
- System.out.println("el numero ya esta registrado");
- break;
- }
- return numeroDeTelefono;
- }
- }
- }
- //Metodo que validar que se ingrese solamente numeros(tipo String) primera parte
- public static String cargarNumeroTelefono(Scanner entrada,String mensaje){
- System.out.println(mensaje);
- String linea=entrada.nextLine();
- while((validarNumeroTelefono(linea)) && (linea.length()<12)){
- System.out.println("''ERROR'' No Ingresar Letras");
- System.out.println(mensaje);
- linea=entrada.nextLine();
- }
- return linea;
- }
- //Metodo que validar que se ingrese solamente numeros(tipo String) segunda parte
- public static boolean validarNumeroTelefono(String linea) {
- for (int x = 0; x < linea.length(); x++) {
- char c = linea.charAt(x);
- if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == ' ') {
- return true;
- }
- }
- return false;
- }
- //Metodo que valida si el correo se repite
- public static String cargarCorreo(Scanner entrada, SimpleLinkedList lista) {
- String correo;
- while(true){
- correo= validarCorreo(entrada);
- if (lista.count==0){
- return correo;
- }
- System.out.println(lista);
- Iterator <Contacto> iter = lista.iterator();
- while(iter.hasNext()){
- if (iter.next().getCorreo().equals(correo)){
- System.out.println("el correo ya esta registrado");
- break;
- }
- return correo;
- }
- }
- }
- //Metod que valida si el correo contiene @ y .
- public static String validarCorreo(Scanner entrada) {
- String correo;
- System.out.println("ingresar correo con @ y .");
- correo = entrada.nextLine();
- while (!(correo.contains("@") && correo.contains("."))){
- System.out.println("ingresar correo con @ y .");
- correo = entrada.nextLine();
- }
- return correo;
- }
- //Metodo que ingresa un trabajo de manera random
- public static String trabajoRandom(){
- int random;
- String [] trabajo ={"panadero","cocinero","doctor","ingeniero",
- "policia","comerciante","actor","secretario","municipal","politico"};
- random = (int) (Math.random() * trabajo.length);
- return trabajo[random];
- }
- //Metodo que muestra la lista
- public static String mostrarContacto(Scanner entrada, SimpleLinkedList lista){
- String trabajo;
- Iterator <Documento> iter = lista.iterator();
- System.out.println("''''''");
- while(iter.hasNext()){
- System.out.println(iter.next());
- }
- System.out.println("''''''");
- trabajo = Helper.cargarLetra(entrada, "ingrese trabajo");
- return trabajo;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment