|
| 1 | + |
| 2 | +import java.util.ArrayList; |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.InputMismatchException; |
| 8 | +import java.util.LinkedList; |
| 9 | +import java.util.NoSuchElementException; |
| 10 | +import java.util.PriorityQueue; |
| 11 | +import java.util.Scanner; |
| 12 | +import java.util.TreeMap; |
| 13 | +import java.util.TreeSet; |
| 14 | +import java.util.Vector; |
| 15 | + |
| 16 | +public class reto_3 { |
| 17 | + |
| 18 | + private static HashMap<String, String> contacto = new HashMap(); |
| 19 | + |
| 20 | + public static void main(String[] args) { |
| 21 | + Scanner input = new Scanner(System.in); |
| 22 | + // Array |
| 23 | + int[] array = new int[5]; |
| 24 | + array[0] = 1; |
| 25 | + array[1] = 2; |
| 26 | + array[2] = 3; |
| 27 | + array[1] = 20; // Actualización |
| 28 | + array[2] = 0; // "Borrado" |
| 29 | + Arrays.sort(array); // Ordenación |
| 30 | + System.out.println("Array: " + Arrays.toString(array)); |
| 31 | + |
| 32 | + // Matriz |
| 33 | + int[][] matrix = new int[3][3]; |
| 34 | + matrix[0][0] = 1; |
| 35 | + matrix[0][1] = 2; |
| 36 | + matrix[0][2] = 3; |
| 37 | + matrix[1][0] = 4; |
| 38 | + matrix[1][1] = 5; |
| 39 | + matrix[1][2] = 6; |
| 40 | + matrix[2][0] = 7; |
| 41 | + matrix[2][1] = 8; |
| 42 | + matrix[2][2] = 9; |
| 43 | + matrix[1][1] = 50; // Actualización |
| 44 | + matrix[2][0] = 0; // "Borrado" |
| 45 | + |
| 46 | + // Ordenación por filas |
| 47 | + for (int i = 0; i < matrix.length; i++) { |
| 48 | + Arrays.sort(matrix[i]); |
| 49 | + } |
| 50 | + |
| 51 | + System.out.println("Matriz ordenada por filas:"); |
| 52 | + for (int i = 0; i < matrix.length; i++) { |
| 53 | + System.out.println(Arrays.toString(matrix[i])); |
| 54 | + } |
| 55 | + |
| 56 | + // ArrayList |
| 57 | + ArrayList<Integer> arrayList = new ArrayList<>(); |
| 58 | + arrayList.add(1); |
| 59 | + arrayList.add(2); |
| 60 | + arrayList.add(3); |
| 61 | + arrayList.set(1, 20); // Actualización |
| 62 | + arrayList.remove(Integer.valueOf(3)); // Borrado |
| 63 | + Collections.sort(arrayList); // Ordenación |
| 64 | + System.out.println("ArrayList: " + arrayList); |
| 65 | + |
| 66 | + // LinkedList |
| 67 | + LinkedList<String> linkedList = new LinkedList<>(); |
| 68 | + linkedList.add("A"); |
| 69 | + linkedList.add("B"); |
| 70 | + linkedList.add("C"); |
| 71 | + linkedList.set(1, "Z"); // Actualización |
| 72 | + linkedList.remove("C"); // Borrado |
| 73 | + Collections.sort(linkedList); // Ordenación |
| 74 | + System.out.println("LinkedList: " + linkedList); |
| 75 | + |
| 76 | + // HashMap |
| 77 | + HashMap<Integer, String> hashMap = new HashMap<>(); |
| 78 | + hashMap.put(1, "Uno"); |
| 79 | + hashMap.put(2, "Dos"); |
| 80 | + hashMap.put(3, "Tres"); |
| 81 | + hashMap.put(2, "Veinte"); // Actualización |
| 82 | + hashMap.remove(3); // Borrado |
| 83 | + System.out.println("HashMap: " + hashMap); |
| 84 | + |
| 85 | + // HashSet |
| 86 | + HashSet<String> hashSet = new HashSet<>(); |
| 87 | + hashSet.add("A"); |
| 88 | + hashSet.add("B"); |
| 89 | + hashSet.add("C"); |
| 90 | + hashSet.add("B"); // No se añadirá porque ya existe |
| 91 | + hashSet.remove("C"); // Borrado |
| 92 | + // Ordenación (conversión a lista y ordenación) |
| 93 | + ArrayList<String> sortedList = new ArrayList<>(hashSet); |
| 94 | + Collections.sort(sortedList); |
| 95 | + System.out.println("HashSet: " + sortedList); |
| 96 | + |
| 97 | + // TreeMap |
| 98 | + TreeMap<Integer, String> treeMap = new TreeMap<>(); |
| 99 | + treeMap.put(1, "Uno"); |
| 100 | + treeMap.put(2, "Dos"); |
| 101 | + treeMap.put(3, "Tres"); |
| 102 | + treeMap.put(2, "Veinte"); // Actualización |
| 103 | + treeMap.remove(3); // Borrado |
| 104 | + System.out.println("TreeMap: " + treeMap); |
| 105 | + |
| 106 | + // TreeSet |
| 107 | + TreeSet<String> treeSet = new TreeSet<>(); |
| 108 | + treeSet.add("A"); |
| 109 | + treeSet.add("B"); |
| 110 | + treeSet.add("C"); |
| 111 | + treeSet.add("B"); // No se añadirá porque ya existe |
| 112 | + treeSet.remove("C"); // Borrado |
| 113 | + System.out.println("TreeSet: " + treeSet); |
| 114 | + |
| 115 | + // Vector |
| 116 | + Vector<Integer> vector = new Vector<>(); |
| 117 | + vector.add(1); |
| 118 | + vector.add(2); |
| 119 | + vector.add(3); |
| 120 | + vector.set(1, 20); // Actualización |
| 121 | + vector.remove(Integer.valueOf(3)); // Borrado |
| 122 | + Collections.sort(vector); // Ordenación |
| 123 | + System.out.println("Vector: " + vector); |
| 124 | + |
| 125 | + // PriorityQueue |
| 126 | + PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(); |
| 127 | + priorityQueue.add(3); |
| 128 | + priorityQueue.add(1); |
| 129 | + priorityQueue.add(2); |
| 130 | + priorityQueue.add(20); // Inserción |
| 131 | + priorityQueue.remove(2); // Borrado |
| 132 | + System.out.println("PriorityQueue (ordenada automáticamente): "); |
| 133 | + while (!priorityQueue.isEmpty()) { |
| 134 | + System.out.print(priorityQueue.poll() + " \n\n"); |
| 135 | + |
| 136 | + } |
| 137 | + boolean exit = false; |
| 138 | + while (!exit) { |
| 139 | + try { |
| 140 | + System.out.println("*******Libreta de contactos******* \n" + // |
| 141 | + ""); |
| 142 | + System.out.println("***Ingresa el numero de la accion que deseas realizar*** \n" + // |
| 143 | + ""); |
| 144 | + |
| 145 | + System.out.println("1: Registrar contacto"); |
| 146 | + System.out.println("2: buscar contacto "); |
| 147 | + System.out.println("3: Actualizar contacto"); |
| 148 | + System.out.println("4: Eliminar contacto"); |
| 149 | + System.out.println("5: salir"); |
| 150 | + |
| 151 | + int option = input.nextInt(); |
| 152 | + |
| 153 | + switch (option) { |
| 154 | + case 1: |
| 155 | + register(); |
| 156 | + break; |
| 157 | + case 2: |
| 158 | + find(); |
| 159 | + break; |
| 160 | + case 3: |
| 161 | + update(); |
| 162 | + break; |
| 163 | + case 4: |
| 164 | + delete(); |
| 165 | + break; |
| 166 | + case 5: |
| 167 | + exit = true; |
| 168 | + break; |
| 169 | + default: |
| 170 | + break; |
| 171 | + } |
| 172 | + } catch (InputMismatchException e) { |
| 173 | + System.out.println("Ingrese una opcion valida"); |
| 174 | + input.nextLine(); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + } |
| 179 | + |
| 180 | + public static void register() { |
| 181 | + Scanner input = new Scanner(System.in); |
| 182 | + |
| 183 | + System.out.println("Nombre"); |
| 184 | + String name = input.nextLine(); |
| 185 | + |
| 186 | + System.out.println("Numero de telefono"); |
| 187 | + String numberOfPhone = input.nextLine(); |
| 188 | + |
| 189 | + while (numberOfPhone.length() > 11 || !esNumerico(numberOfPhone)) { |
| 190 | + |
| 191 | + System.out.print("Número de teléfono inválido. Ingresa un número maximo de 11 dígitos: "); |
| 192 | + numberOfPhone = input.nextLine(); |
| 193 | + |
| 194 | + break; |
| 195 | + |
| 196 | + } |
| 197 | + contacto.put(numberOfPhone, name); |
| 198 | + } |
| 199 | + |
| 200 | + private static String find() { |
| 201 | + Scanner input = new Scanner(System.in); |
| 202 | + |
| 203 | + try { |
| 204 | + System.out.println("Número de teléfono:"); |
| 205 | + String numberOfPhone = input.nextLine(); |
| 206 | + |
| 207 | + if (contacto.containsKey(numberOfPhone) && esNumerico(numberOfPhone)) { |
| 208 | + String name = contacto.get(numberOfPhone); |
| 209 | + System.out.println("Contacto encontrado:"); |
| 210 | + System.out.println("Nombre: " + name); |
| 211 | + System.out.println("Número de teléfono: " + numberOfPhone); |
| 212 | + return numberOfPhone; |
| 213 | + } else if (!esNumerico(numberOfPhone)) { |
| 214 | + System.out.println("Ingresa un número entero válido."); |
| 215 | + } else { |
| 216 | + System.out.println("No se encontró ningún contacto para el número " + numberOfPhone); |
| 217 | + } |
| 218 | + } catch (NoSuchElementException e) { |
| 219 | + System.out.println("Error: No se encontró ningún contacto."); |
| 220 | + } |
| 221 | + return null; |
| 222 | + } |
| 223 | + |
| 224 | + private static void update() { |
| 225 | + Scanner input = new Scanner(System.in); |
| 226 | + |
| 227 | + String currentNumberOfPhone = find(); |
| 228 | + String newNumberOfPhone = ""; |
| 229 | + |
| 230 | + if (currentNumberOfPhone != null) { |
| 231 | + System.out.println("Ingresa el nuevo numero de telefono"); |
| 232 | + newNumberOfPhone = input.nextLine(); |
| 233 | + |
| 234 | + while (newNumberOfPhone.length() > 11 || !esNumerico(newNumberOfPhone)) { |
| 235 | + |
| 236 | + System.out.print("Número de teléfono inválido. Ingresa un número maximo de 11 dígitos: "); |
| 237 | + newNumberOfPhone = input.nextLine(); |
| 238 | + |
| 239 | + break; |
| 240 | + |
| 241 | + } |
| 242 | + |
| 243 | + } |
| 244 | + |
| 245 | + System.out.println("Ingresa el nuevo nombre"); |
| 246 | + String newName = input.nextLine(); |
| 247 | + |
| 248 | + contacto.remove(currentNumberOfPhone); |
| 249 | + contacto.put(newNumberOfPhone, newName); |
| 250 | + System.out.println("Contacto actualizado con éxito."); |
| 251 | + |
| 252 | + } |
| 253 | + |
| 254 | + private static void delete() { |
| 255 | + String deleteNumberOfPhone = find(); |
| 256 | + |
| 257 | + contacto.remove(deleteNumberOfPhone); |
| 258 | + |
| 259 | + System.out.println("contacto eliminado con exito"); |
| 260 | + } |
| 261 | + |
| 262 | + private static boolean esNumerico(String cadena) { |
| 263 | + try { |
| 264 | + Long.parseLong(cadena); |
| 265 | + return true; |
| 266 | + } catch (NumberFormatException e) { |
| 267 | + return false; |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | +} |
0 commit comments