Java - HELP_PROGRAMA EN JAVA NETBEANS

 
Vista:
sin imagen de perfil
Val: 18
Ha aumentado su posición en 508 puestos en Java (en relación al último mes)
Gráfica de Java

HELP_PROGRAMA EN JAVA NETBEANS

Publicado por Ulises Emmanuel (7 intervenciones) el 02/06/2021 21:42:51
Elaborar un programa que almacene los datos de múltiples personas en un arreglo de cadenas bidimensional inicializado con 1000 filas vacías con las columnas necesarias para almacenar los datos requeridos, y que permita efectuar las operaciones de alta, consulta, modificación y baja de un registro, proporcionando el número de fila de la matriz para localizar los datos respectivos cuando sea necesario. Deberá dividir el código en funciones y utilizar arreglo.

yo lo empecé a hacer como agenda telefónica pero solo logro conseguir esto


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public static void main(String[] args) {
        String Nombre;
        String Calle;
        String Numero;
        String Colonia;
        String Localidad;
        String Pais;
        String Tel;
        String Correo;
        String Fecha;
        String Trabajo;
        System.out.println("Escribe tu nombre: ");
        Scanner name= new Scanner(System.in);
        Nombre= name.nextLine();
        System.out.println("Escribe tu calle: ");
        Scanner street= new Scanner(System.in);
        Calle= street.nextLine();
        System.out.println("Escribe tu numero de casa: ");
        Scanner number= new Scanner(System.in);
        Numero= number.nextLine();
        System.out.println("Escribe tu colonia: ");
        Scanner suburb= new Scanner(System.in);
        Colonia= suburb.nextLine();
        System.out.println("Escribe tu localidad: ");
        Scanner loc= new Scanner(System.in);
        Localidad= loc.nextLine();
        System.out.println("Escribe tu Pais: ");
        Scanner country= new Scanner(System.in);
        Pais= country.nextLine();
        System.out.println("Escribe tu telefono: ");
        Scanner phone= new Scanner(System.in);
        Tel= phone.nextLine();
        System.out.println("Escribe tu correo: ");
        Scanner mail= new Scanner(System.in);
        Correo= mail.nextLine();
        System.out.println("Escribe tu fecha de nacimiento: ");
        Scanner date= new Scanner(System.in);
        Fecha= date.nextLine();
        System.out.println("Escribe tu lugar de trabajo: ");
        Scanner work= new Scanner(System.in);
        Trabajo= work.nextLine();
        System.out.println("Tu nombre es: " + Nombre);
        System.out.println("Tu dirección  es: " + Calle +"-"+ Numero+"-" + Colonia+"-" + Localidad+"-" + Pais);
        System.out.println("Tu telefono es: " + Tel);
        System.out.println("Tu correo es: " + Correo);
        System.out.println("Tu fecha de nacimiento es: " + Fecha);
        System.out.println("Tu lugar de trabajo es: " + Trabajo);
 
 
 
 
 
 
 
    }
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Billy Joel
Val: 2.665
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

HELP_PROGRAMA EN JAVA NETBEANS

Publicado por Billy Joel (874 intervenciones) el 03/06/2021 00:05:56
Yo lo resuelvo así:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Persona {
 
    private String nombre;
    private String calle;
    private String numero;
    private String colonia;
    private String localidad;
    private String pais;
    private String tel;
    private String correo;
    private String fecha;
    private String trabajo;
 
    public Persona(String nombre, String calle, String numero, String colonia, String localidad, String pais, String tel, String correo, String fecha, String trabajo) {
        this.nombre = nombre;
        this.calle = calle;
        this.numero = numero;
        this.colonia = colonia;
        this.localidad = localidad;
        this.pais = pais;
        this.tel = tel;
        this.correo = correo;
        this.fecha = fecha;
        this.trabajo = trabajo;
    }
 
    @Override
    public String toString() {
        return "Nombre: " + nombre
                + "\nCalle: " + calle
                + "\nNumero: " + numero
                + "\nColonia: " + colonia
                + "\nLocalidad: " + localidad
                + "\nPais: " + pais
                + "\nTel: " + tel
                + "\ncorreo: " + correo
                + "\nFecha: " + fecha
                + "\nTrabajo: " + trabajo;
    }
 
    /**
     * @return the nombre
     */
    public String getNombre() {
        return nombre;
    }
 
    /**
     * @param nombre the nombre to set
     */
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
 
    /**
     * @return the calle
     */
    public String getCalle() {
        return calle;
    }
 
    /**
     * @param calle the calle to set
     */
    public void setCalle(String calle) {
        this.calle = calle;
    }
 
    /**
     * @return the numero
     */
    public String getNumero() {
        return numero;
    }
 
    /**
     * @param numero the numero to set
     */
    public void setNumero(String numero) {
        this.numero = numero;
    }
 
    /**
     * @return the colonia
     */
    public String getColonia() {
        return colonia;
    }
 
    /**
     * @param colonia the colonia to set
     */
    public void setColonia(String colonia) {
        this.colonia = colonia;
    }
 
    /**
     * @return the localidad
     */
    public String getLocalidad() {
        return localidad;
    }
 
    /**
     * @param localidad the localidad to set
     */
    public void setLocalidad(String localidad) {
        this.localidad = localidad;
    }
 
    /**
     * @return the pais
     */
    public String getPais() {
        return pais;
    }
 
    /**
     * @param pais the pais to set
     */
    public void setPais(String pais) {
        this.pais = pais;
    }
 
    /**
     * @return the tel
     */
    public String getTel() {
        return tel;
    }
 
    /**
     * @param tel the tel to set
     */
    public void setTel(String tel) {
        this.tel = tel;
    }
 
    /**
     * @return the correo
     */
    public String getCorreo() {
        return correo;
    }
 
    /**
     * @param correo the correo to set
     */
    public void setCorreo(String correo) {
        this.correo = correo;
    }
 
    /**
     * @return the fecha
     */
    public String getFecha() {
        return fecha;
    }
 
    /**
     * @param fecha the fecha to set
     */
    public void setFecha(String fecha) {
        this.fecha = fecha;
    }
 
    /**
     * @return the trabajo
     */
    public String getTrabajo() {
        return trabajo;
    }
 
    /**
     * @param trabajo the trabajo to set
     */
    public void setTrabajo(String trabajo) {
        this.trabajo = trabajo;
    }
 
    ////////////////////////////////////////////////////////////////////////////
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
    public static void main(String[] args) throws IOException {
        Persona[] personas = new Persona[1000];
        int opcion;
        final int OPCION_ALTA = 1;
        final int OPCION_CONSULTA = 2;
        final int OPCION_MODIFICACION = 3;
        final int OPCION_SALIR = 0;
        do {
            System.out.print("Ingrese " + OPCION_ALTA + " para dar de alta"
                    + "\nIngrese " + OPCION_CONSULTA + " para consultar"
                    + "\nIngrese " + OPCION_MODIFICACION + " para modificar"
                    + "\nIngrese " + OPCION_SALIR + " para salir: ");
            opcion = Integer.parseInt(br.readLine());
            switch (opcion) {
                case OPCION_ALTA:
                    System.out.println("Alta...");
                    alta(personas);
                    break;
                case OPCION_CONSULTA:
                    System.out.println("Consulta...");
                    consulta(personas);
                    break;
                case OPCION_MODIFICACION:
                    System.out.println("Modificación...");
                    modificacion(personas);
                    break;
                case OPCION_SALIR:
                    System.out.println("Salir... Adios!!");
                    break;
                default:
                    System.out.println("Opción incorrecta");
                    break;
            }
        } while (opcion != OPCION_SALIR);
    }
 
    public static void alta(Persona[] personas) throws IOException {
        //Evaluamos que haya disponibilidad
        boolean hayEspacioDisponible = false;
        int indice = -1;
        for (int i = 0; i < personas.length; i++) {
            if (personas[i] == null) {
                hayEspacioDisponible = true;
                indice = i;
                break;
            }
        }
        if (hayEspacioDisponible) {
            personas[indice] = nuevaPersona();
            System.out.print("Persona registrada!!");
        } else {
            System.out.print("No hay espacio disponible");
        }
    }
 
    public static Persona nuevaPersona() throws IOException {
        String[] fields = {
            "nombre",
            "calle",
            "numero",
            "colonia",
            "localidad",
            "pais",
            "tel",
            "correo",
            "fecha",
            "trabajo"
        };
        String[] vals = new String[fields.length];
        for (int i = 0; i < vals.length; i++) {
            System.out.print("Ingrese " + fields[i] + ": ");
            vals[i] = br.readLine();
        }
        return new Persona(vals[0], vals[1], vals[2], vals[3], vals[4], vals[5], vals[6], vals[7], vals[8], vals[9]);
    }
 
    public static void consulta(Persona[] personas) throws IOException {
        System.out.print("Ingrese un número entre 0 y " + (personas.length - 1) + ": ");
        int indice = Integer.parseInt(br.readLine());
        if (indice < 0 || indice >= personas.length) {
            System.out.println("indice incorrecto");
        } else {
            if (personas[indice] == null) {
                System.out.println("persona no registrada\n");
            } else {
                System.out.println(personas[indice].toString() + "\n");
            }
        }
    }
 
    public static void modificacion(Persona[] personas) throws IOException {
        System.out.print("Ingrese un número entre 0 y " + (personas.length - 1) + ": ");
        int indice = Integer.parseInt(br.readLine());
        if (indice < 0 || indice >= personas.length) {
            System.out.println("indice incorrecto");
        } else {
            if (personas[indice] == null) {
                System.out.println("persona no registrada\n");
            } else {
                System.out.println(personas[indice].toString() + "\n");
                System.out.println("Ingrese los nuevos datos para esta persona");
                personas[indice] = nuevaPersona();
            }
        }
    }
}

Saludos,
Billy Joel
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
Imágen de perfil de Kabuto
Val: 3.428
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

HELP_PROGRAMA EN JAVA NETBEANS

Publicado por Kabuto (1378 intervenciones) el 03/06/2021 12:22:38
Por el enunciado entiendo que se ha de usar una matriz:
1
programa que almacene los datos de múltiples personas en un arreglo de cadenas bidimensional inicializado con 1000 filas vacías con las columnas necesarias para almacenar los datos requeridos

Así pues, en la matriz cada fila correspondería a una persona, y cada columna a uno de los datos de es persona.

Algo como esto:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
public class Agenda {
 
	private static String[][] agenda = new String[1000][10];
	/*
	 * Matriz de 1000 filas x 10 columnas.
	 * Acepta hasta 1000 personas, con 10 datos
	 * por cada persona:
	 * Nombre corresponde a columna [0]
	 * Calle corresponde a columna [1]
	 * Numero corresponde a columna [2]
	 * Colonia corresponde a columna [3]
	 * Localidad corresponde a columna [4]
	 * Pais corresponde a columna [5]
	 * Telefono corresponde a columna [6]
	 * Correo corresponde a columna [7]
	 * FechaNacimiento corresponde a columna [8]
	 * Lugar de Trabajo corresponde a columna [9]
	 */
	private static int indice = 0; //Apunta a la primera fila disponible para hacer nuevos registros
 
	private static Scanner teclado = new Scanner(System.in);
 
	public static void main(String[] args) {
 
		String opcion = "";
 
		while(!opcion.equals("5")) {
			System.out.println("\n\t\tAGENDA CONTACTOS");
			System.out.println("\t\t------ ---------\n");
			System.out.println("[1] -- ALTA Registro");
			System.out.println("[2] -- CONSULTAR Registro");
			System.out.println("[3] -- MODIFICAR Registro");
			System.out.println("[4] -- BORRAR Registro");
			System.out.println("[5] -- CERRAR PROGRAMA");
			System.out.print("Opcion: ");
			opcion = teclado.nextLine();
 
			switch(opcion) {
			case "1":
				altaRegistro();
				break;
			case "2":
				consultarRegistro();
				break;
			case "3":
				modificarRegistro();
				break;
			case "4":
				bajaRegistro();
				break;
			case "5":
				System.out.println("\n\t\tFIN DE PROGRAMA.");
				break;
			default:
				System.out.println("\nOpcion equivocada...\n");
			}
 
			System.out.println("\nPulse ENTER para continuar");
			teclado.nextLine();
		}
 
	}
 
	private static void altaRegistro() {
		System.out.println("\n\tNUEVO REGISTRO\n");
		//Comprobamos primero si hay espacio disponible en la agenda
		if (indice == 1000)
			System.out.println("La AGENDA esta llena. No se admiten nuevos registros");
		else {
			//Pedimos datos para cada columna de la matriz
			System.out.print("Nombre: ");
			agenda[indice][0] = teclado.nextLine();
			System.out.print("Calle: ");
			agenda[indice][1] = teclado.nextLine();
			System.out.print("Numero: ");
			agenda[indice][2] = teclado.nextLine();
			System.out.print("Colonia: ");
			agenda[indice][3] = teclado.nextLine();
			System.out.print("Localidad: ");
			agenda[indice][4] = teclado.nextLine();
			System.out.print("Pais: ");
			agenda[indice][5] = teclado.nextLine();
			System.out.print("Telefono: ");
			agenda[indice][6] = teclado.nextLine();
			System.out.print("Correo: ");
			agenda[indice][7] = teclado.nextLine();
			System.out.print("Fecha Nacimiento: ");
			agenda[indice][8] = teclado.nextLine();
			System.out.print("Lugar de Trabajo: ");
			agenda[indice][9] = teclado.nextLine();
 
			System.out.println("\n\tContacto registrado");
			indice++; //Incrementos indice de la agenda
		}
	}
 
	private static void consultarRegistro() {
		System.out.println("\n\tCONSULTAR REGISTRO\n");
		System.out.print("Indique fila del registro: ");
		int fila = Integer.parseInt(teclado.nextLine());
 
		//Comprobamos si hay registro en esa fila
		if (fila >= indice)
			System.out.println("\nNo existe registro en esa fila");
		else {
			//Mostramos datos del registro
			System.out.println("\nNombre: " + agenda[fila][0]);
			System.out.println("Calle: " + agenda[fila][1] + "\tNumero: " + agenda[fila][2]);
			System.out.println("Colonia: " + agenda[fila][3] + "\tLocalidad: " + agenda[fila][4]);
			System.out.println("Pais: " + agenda[fila][5]);
			System.out.println("Telefono: " + agenda[fila][6] + "\tCorreo: " + agenda[fila][7]);
			System.out.println("Fecha Nacim.: " + agenda[fila][8] + "\tLugar de  Trabajo: " + agenda[fila][9]);
		}
	}
 
	private static void modificarRegistro() {
		System.out.println("\n\tMODIFICAR REGISTRO\n");
		System.out.print("Indique fila del registro: ");
		int fila = Integer.parseInt(teclado.nextLine());
 
		//Comprobamos si hay registro en esa fila
		if (fila >= indice)
			System.out.println("\nNo existe registro en esa fila");
		else {
			System.out.println("\nIndique el dato que quiere modificar.");
			System.out.println("[0] -- Nombre");
			System.out.println("[1] -- Calle");
			System.out.println("[2] -- Numero");
			System.out.println("[3] -- Colonia");
			System.out.println("[4] -- Localidad");
			System.out.println("[5] -- Pais");
			System.out.println("[6] -- Telefono");
			System.out.println("[7] -- Correo");
			System.out.println("[8] -- Fecha Nacimiento");
			System.out.println("[9] -- Lugar de Trabajo");
			System.out.print("\nDato: ");
			int columna = Integer.parseInt(teclado.nextLine());
 
			if (columna < 0 || columna > 9)
				System.out.println("\nValor de dato incorrecto...");
			else {
				System.out.println("\nDato actual: " + agenda[fila][columna]);
				System.out.print("Nuevo dato: ");
				agenda[fila][columna] = teclado.nextLine();
 
				System.out.println("\n\tContacto modificado");
			}
 
		}
	}
 
	private static void bajaRegistro() {
		System.out.println("\n\tBORRAR REGISTRO\n");
		System.out.print("Indique fila del registro: ");
		int fila = Integer.parseInt(teclado.nextLine());
 
		//Comprobamos si hay registro en esa fila
		if (fila >= indice)
			System.out.println("\nNo existe registro en esa fila");
		else {
			/*
			 * Borrar un registro implica desplazar los registros posteriores
			 * una posición hacia atrás para que no queden huecos vacíos
			 * entre los registros.
			 * Para ello usaremos un bucle que recorra las posiciones
			 * que hay que desplazar y las mueva una posición
			 */
			for (int i = fila; i < (indice - 1); i++)
				agenda[i] = agenda[i + 1]; //Copiamos la fila posterior en la fila actual
			/*
			 * Tras realizar estos desplazamientos, el registro que se quería borrar
			 * ya no existe porque ha sido sustituido por el registro posterior.
			 * Los demás registros posteriores se han desplazado hacia atrás.
			 * Solo queda actualizar el índice de la agenda, decrementando una posición
			 */
			indice--;
			System.out.println("\n\tContacto eliminado");
		}
 
	}
 
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar