Skip to content

Commit bba6735

Browse files
authored
Merge pull request mouredev#7638 from AFOXJONES/main
#3 - java
2 parents 50152e9 + 64ffea8 commit bba6735

File tree

1 file changed

+215
-0
lines changed

1 file changed

+215
-0
lines changed
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
2+
import java.util.*;
3+
public class AFOXJONES{
4+
// Mapa para almacenar los contactos (nombre -> número de teléfono)
5+
private static Map<String, String> contacts = new HashMap<>();
6+
private static final int MAX_PHONE_DIGITS = 11; // Límite de dígitos del número de teléfono
7+
8+
9+
static Scanner scanner = new Scanner(System.in);
10+
public static void main(String[] args){
11+
12+
// 1. Array
13+
System.out.println("Array:");
14+
int[] array = {5, 3, 8, 1};
15+
System.out.println("Original: " + Arrays.toString(array));
16+
array[1] = 10; // Actualización
17+
System.out.println("Actualizado: " + Arrays.toString(array));
18+
Arrays.sort(array); // Ordenación
19+
System.out.println("Ordenado: " + Arrays.toString(array));
20+
21+
// 2. ArrayList (Lista)
22+
System.out.println("\nArrayList:");
23+
List<Integer> arrayList = new ArrayList<>(Arrays.asList(5, 3, 8, 1));
24+
System.out.println("Original: " + arrayList);
25+
arrayList.add(7); // Inserción
26+
System.out.println("Añadido 7: " + arrayList);
27+
arrayList.remove(Integer.valueOf(3)); // Borrado
28+
System.out.println("Borrado 3: " + arrayList);
29+
arrayList.set(1, 10); // Actualización
30+
System.out.println("Actualizado: " + arrayList);
31+
arrayList.sort(Comparator.naturalOrder()); // Ordenación
32+
System.out.println("Ordenado: " + arrayList);
33+
34+
// 3. HashSet (Conjunto)
35+
System.out.println("\nHashSet:");
36+
Set<Integer> hashSet = new HashSet<>(Arrays.asList(5, 3, 8, 1));
37+
System.out.println("Original: " + hashSet);
38+
hashSet.add(7); // Inserción
39+
System.out.println("Añadido 7: " + hashSet);
40+
hashSet.remove(3); // Borrado
41+
System.out.println("Borrado 3: " + hashSet);
42+
// No admite ordenación directamente; convertir a lista
43+
List<Integer> sortedSet = new ArrayList<>(hashSet);
44+
Collections.sort(sortedSet);
45+
System.out.println("Ordenado: " + sortedSet);
46+
47+
// 4. HashMap (Mapa)
48+
System.out.println("\nHashMap:");
49+
Map<String, Integer> hashMap = new HashMap<>();
50+
hashMap.put("A", 5); // Inserción
51+
hashMap.put("B", 3);
52+
hashMap.put("C", 8);
53+
System.out.println("Original: " + hashMap);
54+
hashMap.put("B", 10); // Actualización
55+
System.out.println("Actualizado B: " + hashMap);
56+
hashMap.remove("A"); // Borrado
57+
System.out.println("Borrado A: " + hashMap);
58+
// No admite ordenación directamente; usar TreeMap
59+
Map<String, Integer> sortedMap = new TreeMap<>(hashMap);
60+
System.out.println("Ordenado: " + sortedMap);
61+
62+
// 5. Queue (Cola)
63+
System.out.println("\nQueue:");
64+
Queue<Integer> queue = new LinkedList<>(Arrays.asList(5, 3, 8, 1));
65+
System.out.println("Original: " + queue);
66+
queue.add(7); // Inserción
67+
System.out.println("Añadido 7: " + queue);
68+
queue.poll(); // Borrado (primer elemento)
69+
System.out.println("Borrado primer elemento: " + queue);
70+
71+
// 6. Stack (Pila)
72+
System.out.println("\nStack:");
73+
Stack<Integer> stack = new Stack<>();
74+
stack.push(5); // Inserción
75+
stack.push(3);
76+
stack.push(8);
77+
stack.push(1);
78+
System.out.println("Original: " + stack);
79+
stack.pop(); // Borrado (último elemento)
80+
System.out.println("Borrado último elemento: " + stack);
81+
stack.push(7); // Inserción
82+
System.out.println("Añadido 7: " + stack);
83+
84+
extra();
85+
86+
87+
}
88+
89+
public static void extra(){
90+
Scanner scanner = new Scanner(System.in);
91+
boolean running = true;
92+
93+
System.out.println("Bienvenido a la Agenda de Contactos");
94+
95+
while (running) {
96+
System.out.println("\n¿Qué deseas hacer?");
97+
System.out.println("1. Insertar contacto");
98+
System.out.println("2. Buscar contacto");
99+
System.out.println("3. Actualizar contacto");
100+
System.out.println("4. Eliminar contacto");
101+
System.out.println("5. Mostrar todos los contactos");
102+
System.out.println("6. Salir");
103+
System.out.print("Selecciona una opción: ");
104+
105+
String option = scanner.nextLine();
106+
107+
switch (option) {
108+
case "1":
109+
insertContact(scanner);
110+
break;
111+
case "2":
112+
searchContact(scanner);
113+
break;
114+
case "3":
115+
updateContact(scanner);
116+
break;
117+
case "4":
118+
deleteContact(scanner);
119+
break;
120+
case "5":
121+
showAllContacts();
122+
break;
123+
case "6":
124+
running = false;
125+
System.out.println("¡Hasta luego!");
126+
break;
127+
default:
128+
System.out.println("Opción no válida. Inténtalo de nuevo.");
129+
}
130+
}
131+
132+
scanner.close();
133+
}
134+
135+
// Método para insertar un contacto
136+
private static void insertContact(Scanner scanner) {
137+
System.out.print("Introduce el nombre del contacto: ");
138+
String name = scanner.nextLine().trim();
139+
140+
if (contacts.containsKey(name)) {
141+
System.out.println("El contacto ya existe.");
142+
return;
143+
}
144+
145+
System.out.print("Introduce el número de teléfono: ");
146+
String phone = scanner.nextLine().trim();
147+
148+
if (isValidPhone(phone)) {
149+
contacts.put(name, phone);
150+
System.out.println("Contacto agregado correctamente.");
151+
} else {
152+
System.out.println("Número de teléfono no válido. Debe ser numérico y tener hasta " + MAX_PHONE_DIGITS + " dígitos.");
153+
}
154+
}
155+
156+
// Método para buscar un contacto
157+
private static void searchContact(Scanner scanner) {
158+
System.out.print("Introduce el nombre del contacto a buscar: ");
159+
String name = scanner.nextLine().trim();
160+
161+
if (contacts.containsKey(name)) {
162+
System.out.println("Contacto encontrado: " + name + " -> " + contacts.get(name));
163+
} else {
164+
System.out.println("No se encontró ningún contacto con ese nombre.");
165+
}
166+
}
167+
168+
// Método para actualizar un contacto
169+
private static void updateContact(Scanner scanner) {
170+
System.out.print("Introduce el nombre del contacto a actualizar: ");
171+
String name = scanner.nextLine().trim();
172+
173+
if (contacts.containsKey(name)) {
174+
System.out.print("Introduce el nuevo número de teléfono: ");
175+
String newPhone = scanner.nextLine().trim();
176+
177+
if (isValidPhone(newPhone)) {
178+
contacts.put(name, newPhone);
179+
System.out.println("Contacto actualizado correctamente.");
180+
} else {
181+
System.out.println("Número de teléfono no válido.");
182+
}
183+
} else {
184+
System.out.println("No se encontró ningún contacto con ese nombre.");
185+
}
186+
}
187+
188+
// Método para eliminar un contacto
189+
private static void deleteContact(Scanner scanner) {
190+
System.out.print("Introduce el nombre del contacto a eliminar: ");
191+
String name = scanner.nextLine().trim();
192+
193+
if (contacts.containsKey(name)) {
194+
contacts.remove(name);
195+
System.out.println("Contacto eliminado correctamente.");
196+
} else {
197+
System.out.println("No se encontró ningún contacto con ese nombre.");
198+
}
199+
}
200+
201+
// Método para mostrar todos los contactos
202+
private static void showAllContacts() {
203+
if (contacts.isEmpty()) {
204+
System.out.println("No hay contactos en la agenda.");
205+
} else {
206+
System.out.println("Lista de contactos:");
207+
contacts.forEach((name, phone) -> System.out.println(name + " -> " + phone));
208+
}
209+
}
210+
211+
// Método para validar un número de teléfono
212+
private static boolean isValidPhone(String phone) {
213+
return phone.matches("\\d{1," + MAX_PHONE_DIGITS + "}");
214+
}
215+
}

0 commit comments

Comments
 (0)