Está en la página 1de 6

UNIVERSIDAD CENTRAL DEL ECUADOR

Estudiante: Pablo Sanchez Curso: SIS3-P1


Fecha: 22-12-2021
Generar una agenda de nombres y edades, usando arreglos paralelos de la misma dimensión
n, realizar:
1. ingreso de nuevos registros

2. Insertar por búsqueda un nuevo registro.


Código:
package tarea2;

import java.util.Scanner;

/**
* @author Pablo
*/
public class Tarea2 {

public static void main(String[] args) {


int n = 0;
int opcion, posicion;
String buscar;

Class_AccionesT2 f = new Class_AccionesT2();


n = f.Numero(n, "n");

int[] Anumeros = new int[n]; // declarar el arreglo


String nombres[] = new String[n];
f.Encerar(nombres, Anumeros);
f.NuevoElemento(nombres, Anumeros);
System.out.println();
f.SalidaPantallaCompletoN(nombres, Anumeros);
System.out.println("\n\n<<Menú de opciones: >>");
System.out.println("1. Insertar");
System.out.println("2. Modificar");
System.out.println("0. Salir");
System.out.print("Seleccionar la opción: ");
Scanner leer2 = new Scanner(System.in);
opcion = leer2.nextInt();

switch (opcion) {
case 1: {
//Insertar un elemento en el arreglo, buscando un elemento
System.out.println("\n<<Insertar un elemento en el arreglo>>");
System.out.print("Ingresar el número a buscar: ");
buscar = leer2.next();
posicion = f.BuscarElemento(nombres, Anumeros, buscar );
if (posicion >= 0) {
System.out.println("El número " + buscar + " está en ubicación: " +
(posicion + 1));
f.InsertarElemento(posicion, nombres, Anumeros);
f.SalidaPantallaCompletoN(nombres, Anumeros);
} else {
System.out.println(buscar + " no existe en arreglo");
}
}
}}}
***************************************************************************
***********
package tarea2;

import java.util.InputMismatchException;
import java.util.Scanner;

/*
* @author Pablo
*/
public class Class_AccionesT2 {

public int Numero(int num, String etiqueta) {


do {
try {
Scanner leer = new Scanner(System.in);
System.out.print("Introduzca el tamanio de la lista (" + etiqueta + "): ");
num = leer.nextInt();
if (num <= 0) {
System.out.println(" Error! debe ser número positivo y mayor a 0");
}
} catch (Exception e) {
System.out.println(" Error! debe ser número entero positivo");
}
} while (num <= 0);
return num;
}

public void Encerar(String[] nombres, int[] arreglo) {


for (int i = 0; i < arreglo.length; i++) //encerar o inicializar el arreglo
{
nombres[i] = "";
arreglo[i] = 0;

}
}

public void SalidaPantallaCompletoN(String[] nombres, int[] arreglo) {


for (int i = 0; i < nombres.length; i++) {
System.out.println(nombres[i] + " " + arreglo[i]);
}
}

public void NuevoElemento(String[] nombres, int[] numeros) {


int i = 0;
char siguiente;
do {
Scanner leer = new Scanner(System.in);
do {
try {
System.out.print("\nIngresar un Nombre: ");
nombres[i] = leer.next();
System.out.print("\nIngresar una edad: ");
numeros[i] = leer.nextInt();
} catch (InputMismatchException e) {
nombres[i] = "";
numeros[i] = 0;
if (numeros[i] <= 0 && nombres[i] == "") {
System.out.println("Error! esta entrada no es valida ");
}
}

} while (numeros[i] <= 0);


System.out.print("Ingresar otro elemento s/n: ");
siguiente = leer.next().charAt(0);
if (siguiente == 's' || siguiente == 'S') {
i++;
}

} while ((siguiente == 's' || siguiente == 'S') && (i < numeros.length));


}
public int BuscarElemento(String[] nombres, int[] numeros, String buscar) {
int i = 0, posicion = 0;
while (nombres[i] != null) {
if (!nombres[i].equalsIgnoreCase(buscar) || !numeros.equals(buscar)) {
posicion = numeros[i];
}else
break;

}
return posicion;
}

public void InsertarElemento(int posicion, String[] nombres, int[] numeros) {


// buscar si está libre el arreglo y tiene capacidad de almacenamiento
int i = 0;
int numinsertar;
boolean libre = false;
do {
if (numeros[i] == 0) {
libre = true;
break;
}
i++;
} while (i < numeros.length);

if (libre) {
do {
Scanner leer2 = new Scanner(System.in);
System.out.print("\nNúmero a insertar: ");
numinsertar = leer2.nextInt();
} while (numinsertar <= 0);

for (int j = i; j > posicion + 1; j--) {


numeros[j] = numeros[j - 1];
}
numeros[posicion + 1] = numinsertar;
} else {
System.out.println("El arreglo no tiene capacidad para insertar nuevos
elementos");
}
}

}
Capturas:

También podría gustarte