Está en la página 1de 20

UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE

INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS


INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ
Uso de métodos, arreglos y menú de opciones
1.-Escribir un programa que presente un menú en el que se permita seleccionar alguna
de las siguientes tareas a realizar:
1.-Ingresar datos (nombre: de tipo String, edad : de tipo entero y sexo: de tipo char)
de N alumnos.
2.-Reportar todos los datos
3.-Consultar datos en base al nombre
4.-Terminar
El programa se debe ejecutar mientras no se escoge la opción terminar.

Inicio menu()
Variables nom[50],ed[50],sex[50],N=0,opc Variables opc
opc = menu( ) Escribir: “Menu“
opc Escribir:”[1]Ingresar”
1 2 3 4 Escribir:”[2]Reportar”
consultar(nom,ed,sex,N)

Escribir:”[3]Consultar”
reportar(nom,ed,sex,N)
ingresar(nom,ed,sex,N)

Escribir:”[4]Terminar”
Escribir:”Escoger opción[1-4]”
N=leenum()

terminar( )

Leer opc
opc <1 V opc>4
retornar opc
opc ≠ 4
Fin

leenum( ) ingresar(nom[ ], ed[ ], sex[ ], N)


Variables N Variables i
Leer N
i=0; i<N ; i = i +1
N ≤ 0 V N > 50
retornar N Leer nom[i]

Leer ed[i]
reportar(nom[ ], ed[ ], sex[ ], N) ed[i] ≤ 0
Variables i
Leer sex[i]
I=0; i<N ; i = I +1
Escribir: nom[i], ed[i], sex[ i ] sex[i] ≠ ‘M’ Λ sex[i] ≠ ‘F’

retornar retornar
1
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS
INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ

consultar (nom[ ], ed[ ], sex[ ], N) terminar( )


variables i, nomaux, pos = -1 Variables
Leer nomaux
Escribir: “Fin del programa”
i=0; i<N ; i = i +1
V nom[i] = nomaux F retornar

pos = i
i=N

V pos=-1 F
Escribir: “Dato no Escribir: nom[pos]
Existe” ed[pos], sex[pos]
retornar

Código en java

import java.io.*;
public class ejem1
{ static BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

public static void main(String [] args) throws IOException


{ String nom[ ] = new String[50];
int ed [ ] = new int[50];
char sex [ ] = new char[50];
int N= 0, opc;
do { opc= menu ( );
switch( opc )
{ case 1 : N=leenum();
ingresar(nom,ed,sex,N);
break;
case 2 : reportar(nom,ed,sex,N);
break;
case 3 : consultar(nom,ed,sex,N);
break;
case 4 : terminar();
break;
}
}
while(opc!=4);
} // llave fin de main

2
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS
INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ

static int menu() throws IOException


{ int opc;
do{ System.out.println("\n\nMENU");
System.out.println("[1]Ingresar datos");
System.out.println("[2]Reportar datos");
System.out.println("[3]Consultar datos");
System.out.println("[4]Terminar");
System.out.println("Escoger opcion [1 - 4 ]");
opc=Integer.parseInt(br.readLine());
}
while(opc<1 || opc>4);
return opc;
}

static int leenum() throws IOException


{ int N;
do{ System.out.print("Ingrese No. de datos:");
N=Integer.parseInt(br.readLine());
}
while(N<=0 || N>50) ;
return N;
}

static void ingresar(String nom[], int ed[], char sex[], int N) throws IOException
{int i;
String cad;
System.out.println("\nIngreso de datos\n");
for(i=0; i<N; i++)
{System.out.println("Nombre["+i+"] :");
nom[i]=br.readLine();
nom[i]=nom[i].toUpperCase();
do{System.out.println("Edad["+i+"] :");
ed[i]=Integer.parseInt(br.readLine());
}
while(ed[i]<=0) ;
do{System.out.println("Sexo["+i+"] :");
cad=br.readLine();
cad=cad.toUpperCase();
sex[i]=cad.charAt(0);
}
while(sex[i]!='M' &&sex[i]!='F') ;
}
return;
}

3
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS
INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ

static void reportar(String nom[], int ed[], char sex[], int N) throws IOException
{int i;
System.out.println("\nReporte de datos\n");
for(i=0; i<N; i++)
{System.out.println("\t"+ nom[i]+"\t"+ ed[i]+"\t"+sex[i]);
}
return;
}

static void consultar(String nom[], int ed[], char sex[], int N) throws IOException
{ int i, pos = -1;
String nomaux;
System.out.println("\nConsulta de datos\n");
System.out.println("\nIngrese nombre a buscar : ");
nomaux=br.readLine();
nomaux=nomaux.toUpperCase();
for(i=0; i<N; i++)
{ if(nom[i].compareTo(nomaux)==0)
{ pos =i;
break;
}
}
if(pos == -1)
{System.out.println("\nDato buscado no existe\n");
}
else
{System.out.println("\nDato encontrado\n");
System.out.println("\t"+ nom[pos]+"\t"+ed[pos]+"\t"+sex[pos]);
}
return;
}

static void terminar() throws IOException


{System.out.println("\nFin del programa\n");
return;
}

} // llave fin de la clase

4
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS
INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ
2.- Escribir un programa que presente un menú en el que se permita seleccionar alguna
de las siguientes tareas a realizar:
1.-Ingresar datos (nombre de producto : de tipo String , precio de producto : de tipo
real y tipo de producto : de tipo char) de N productos.
El tipo de producto puede ser: ‘N’=Nacional o ‘I’=Importado.
2.-Reportar todos los datos
3.-Agregar datos
4.-Modificar datos
5.-Terminar
El programa se debe ejecutar mientras no se escoge la opción terminar.

Inicio menu()
Variables nom[50],pre[50], tip[50], N=0, opc Variables opc
opc = menu( ) Escribir: “Menu“
opc Escribir:”[1] Ingresar”
1 2 3 4 5 Escribir:”[2] Reportar”
modificar(nom,pre,tip,N)

Escribir:”[3] Agregar”
reportar(nom,pre,tip,N)
ingresar(nom,pre,tip,N)

agregar(nom,pre,tip,N)

Escribir:”[4] Modificar”
Escribir:”[5] Terminar”
N = lenum()

Escribir:”Escoger opción[1-5]”
terminar( )

Leer opc
opc <1 V opc>5
opc ≠ 5 retornar opc
Fin

leenum( )
ingresar (nom[ ], pre[ ], tip[ ], N)
Variables N
Variables i
Leer N
N ≤ 0 V N > 50 i=0; i<N ; i = i +1
retornar N Leer nom[i]

Leer pre[i]

reportar (nom[ ], pre[ ], tip[ ], N) pre[i] ≤ 0

Variables i Leer tip[i]


i=0; i<N ; i = I +1
tip[i] ≠ ‘N’ Λ tip[i] ≠ ‘I’
Escribir: nom[i], pre[i], tip[ i ]

retornar retornar
5
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS
INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ

modificar (nom[ ], pre[ ], tip[ ], N) agregar (nom[ ], pre[ ], tip[ ], N)


variables i, nomaux, pos = -1 variables
Leer nomaux V N<50 F
i=0; i<N ; i = i +1 leer nom[N] Escribir:
V nom[i] = nomaux F leer pre[N] “Arreglo
pos = i pre[N] ≤ 0 lleno”
i=N Leer tip[N]
tip[N]≠ ‘N Λ tip[N]≠‘I’
V pos=-1 F N=N+1
Escribir: leer nom[pos] retornar N
“Dato No Existe” leer pre[pos]
pre[pos] ≤ 0 terminar( )

leer tip[pos] Variables

tip[pos]≠ ‘N Λ tip[pos]≠‘I’ Escribir: “Fin del programa”

retornar retornar

Código en java
import java.io.*;
public class ejem2
{ static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String [] args) throws IOException
{ String nom[ ] = new String[50];
double pre [ ] = new double[50];
char tip [ ] = new char[50];
int N=0 , opc;
do { opc= menu ( );
switch( opc )
{ case 1 : N=leenum();
ingresar(nom,pre,tip,N);
break;
case 2 : reportar(nom,pre,tip,N);
break;
case 3 : N=agregar(nom,pre,tip,N);
break;
case 4 : modificar(nom,pre,tip,N);
break;
case 5 : terminar();
break;
}
}while(opc!=5);
} // llave fin de main
6
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS
INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ

static int menu() throws IOException


{ int opc;
do{ System.out.println("\n\nMENU");
System.out.println("[1]Ingresar datos");
System.out.println("[2]Reportar datos");
System.out.println("[3]Agregar datos");
System.out.println("[4]Modificar datos");
System.out.println("[5]Terminar");
System.out.println("Escoger opcion [1 - 5 ]");
opc=Integer.parseInt(br.readLine());
}
while(opc<1 || opc>5);
return opc;
}

static int leenum() throws IOException


{ int N;
do{ System.out.print("Ingrese No. de datos:");
N=Integer.parseInt(br.readLine());
}
while(N<=0 || N>50) ;
return N;
}

static void ingresar(String nom[], double pre[], char tip[], int N) throws IOException
{int i;
String cad;
System.out.println("\nIngreso de datos\n");
for(i=0; i<N; i++)
{System.out.print("Nombre["+i+"] :");
nom[i]=br.readLine();
nom[i]=nom[i].toUpperCase();
do{System.out.print("Precio["+i+"] :");
pre[i]=Double.parseDouble(br.readLine());
}
while(pre[i]<=0) ;
do{System.out.print("Tipo["+i+"] :");
cad=br.readLine();
cad=cad.toUpperCase();
tip[i]=cad.charAt(0);
}
while(tip[i]!='N' && tip[i]!='I') ;
}
return;
}

7
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS
INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ

static void reportar(String nom[], double pre[], char tip[], int N) throws IOException
{int i;
System.out.println("\nReporte de datos\n");
for(i=0; i<N; i++)
{System.out.println("\t"+ nom[i]+"\t"+ pre[i]+"\t"+tip[i]);
}
return;
}

static int agregar(String nom[], double pre[], char tip[], int N) throws IOException
{ String cad;
System.out.println("\nAgregar datos\n");
if(N<50)
{ System.out.print("Nuevo Nombre["+N+"] :");
nom[N]=br.readLine();
nom[N]=nom[N].toUpperCase();
do{ System.out.print("Nuevo Precio["+N+"] :");
pre[N]=Double.parseDouble(br.readLine());
}
while(pre[N]<=0) ;
do{System.out.print("Nuevo Tipo["+N+"] :");
cad=br.readLine();
cad=cad.toUpperCase();
tip[N]=cad.charAt(0);
}
while(tip[N]!='N' && tip[N]!='I') ;
N++;
System.out.println("Los datos han sido agregados…!");
}
else{System.out.println("\nArreglo lleno....!");
}
return N;
}
static void modificar(String nom[], double pre[], char tip[], int N) throws IOException
{ int i, pos = -1;
String nomaux , cad;
System.out.println("\nModificación de datos\n");
System.out.println("\nIngrese nombre a buscar : ");
nomaux=br.readLine();
nomaux=nomaux.toUpperCase();
for(i=0; i<N; i++)
{ if (nom[i].compareTo(nomaux)==0)
{ pos =i;
break;
}
}

8
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS
INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ

if(pos== -1)
{System.out.println("\nDato buscado no existe\n");
}
else
{ System.out.println("\nDatos Actuales encontrados\n");
System.out.println("\t"+ nom[pos]+"\t"+pre[pos]+"\t"+tip[pos]);
System.out.print("Nuevo Nombre["+pos+"] :");
nom[pos]=br.readLine();
nom[pos]=nom[pos].toUpperCase();
do{ System.out.print("Nuevo Precio["+pos+"] :");
pre[pos]=Double.parseDouble(br.readLine());
}
while(pre[pos]<=0) ;
do{ System.out.print("Nuevo Tipo["+pos+"] :");
cad=br.readLine();
cad=cad.toUpperCase();
tip[pos]=cad.charAt(0);
}
while(tip[pos]!='N' && tip[pos]!='I') ;
}
return;
}

static void terminar() throws IOException


{System.out.println("\nFin del programa\n");
return;
}

} // llave fin de la clase

9
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS
INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ
3.- Escribir un programa que presente un menú en el que se permita seleccionar alguna
de las siguientes tareas a realizar:
a.-Ingresar datos (nombre de producto : de tipo String , precio de producto : de tipo
real y tipo de producto : de tipo char) de N productos.
El tipo de producto puede ser: ‘N’=Nacional o ‘I’=Importado.
b.-Reportar todos los datos
c.-Eliminar datos
d.-Ordenar datos alfabéticamente
e.-Insertar datos en la lista ordenada
f.-Terminar
El programa se debe ejecutar mientras no se escoge la opción terminar.

Inicio menu()
Variables nom[50],pre[50], tip[50], N=0, opc Variables opc
opc = menu( ) Escribir: “Menu“
opc Escribir:”[A] Ingresar”
A B C D E F Escribir:”[B] Reportar”
Escribir:”[C] Eliminar”
reportar(nom,p re, tip, N)

eliminar(nom, pre, tip, N)

ordenar(nom, pre, tip, N)

insertar(nom, pre, tip, N)


Ingrsar(nom,pre, tip, N)

Escribir:”[D] Ordenar”
Escribir:”[E] Insertar”
Escribir:”[F] Terminar”
N=leenum()

terminar( )

Escribir:”Escoger opción[A-F]”
Leer opc
opc ≠ ‘F’ opc <’A’ V opc >´F´
Fin retornar opc

leenum( ) ingresar (nom[ ], pre[ ], tip[ ], N)


Variables N Variables i
Leer N
i=0; i<N ; i = i +1
N ≤ 0 V N > 50
retornar N Leer nom[i]

Leer pre[i]

reportar (nom[ ], pre[ ], tip[ ], N) pre[i] ≤ 0

Variables i Leer tip[i]


i=0; i<N ; i = I +1
tip[i] ≠ ‘N’ Λ tip[i] ≠ ‘I’
Escribir: nom[i], pre[i], tip[ i ]

retornar
retornar 10
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS
INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ
eliminar (nom[ ], pre[ ], tip[ ], N) insertar(nom[ ], pre[ ], tip[ ], N)
variables i, nomaux, pos = -1 variables i, nomaux, pos = -1
Leer nomaux Leer nomaux
i=0; i<N ; i = i +1 i=0; i<N ; i = i +1
V nom[i] = nomaux F V nom[i] > nomaux F
pos = i pos = i
i=N i=N

V pos=-1 F V pos = -1 F
Escribir: nom[pos] = nom[N-1] pos = N
“Dato No Existe” pre[pos] = pre[N-1] i = N ; i > pos ; i = i – 1
tip[pos] = tip[N-1] nom[i] = nom[i-1]
N=N-1 pre[i] = pre[i-1]
retornar N tip [i] = tip[i-1]

nom[pos] = nomaux
Leer pre[pos]
ordenar (nom[ ], pre[ ], tip[ ], N)
pre[pos] ≤ 0
variables i, j, nomaux, preaux, tipaux
i=0; i<N ; i = i +1 Leer tip[pos]
j= i +1; j<N; j=j+1 tip[pos] ≠ ‘N’ Λ tip[pos] ≠ ‘I’
V nom[i] > nom[j] F N = N +1
nomaux=nom[j]; retornar N
nom[j] =nom[i];
nom[i] =nomaux;
preaux =pre[j];
pre[j] =pre[i]; terminar( )
pre[i] =preaux;
Variables
tipaux =tip[j];
Escribir: “Fin del programa”
tip[j] =tip[i];
tip[i] =tipaux; retornar

retornar

11
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS
INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ
Código en java
import java.io.*;
import java.io.*;
public class ejem3
public class ejem3
{ static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
{ static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String [] args) throws IOException
{ String nom[ ] = new String[50];
double pre [ ] = new double[50];
char tip [ ] = new char[50];
int N=0 ;
char opc;
do { opc= menu ( );
switch( opc )
{ case 'A' : N=leenum();
ingresar(nom,pre,tip,N);
break;
case 'B' : reportar(nom,pre,tip,N);
break;
case 'C' : N=eliminar(nom,pre,tip,N);
break;
case 'D' : ordenar(nom,pre,tip,N);
break;
case 'E' : N = insertar(nom,pre,tip,N);
break;
case 'F' : terminar();
break;
}
}
while(opc!='F');
} // llave fin de main

static char menu() throws IOException


{ char opc;
String cad;
do{ System.out.println("\n\nMENU");
System.out.println("[A]Ingresar datos");
System.out.println("[B]Reportar datos");
System.out.println("[C]Eliminar datos");
System.out.println("[D]Ordenas datos");
System.out.println("[E]Insertar datos");
System.out.println("[F]Terminar");
System.out.println("Escoger opcion [A - F]");
cad=br.readLine();
cad=cad.toUpperCase();
opc=cad.charAt(0);
}
while(opc<'A' || opc>'F');
return opc;
}
12
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS
INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ

static int leenum() throws IOException


{ int N;
do{ System.out.print("Ingrese No. de datos:");
N=Integer.parseInt(br.readLine());
}
while(N<=0 || N>50) ;
return N;
}

static void ingresar(String nom[], double pre[], char tip[], int N) throws IOException
{int i;
String cad;
System.out.println("\nIngreso de datos\n");
for(i=0; i<N; i++)
{System.out.print("Nombre["+i+"] :");
nom[i]=br.readLine();
nom[i]=nom[i].toUpperCase();
do{System.out.print("Precio["+i+"] :");
pre[i]=Double.parseDouble(br.readLine());
}
while(pre[i]<=0) ;
do{System.out.print("Tipo["+i+"] :");
cad=br.readLine();
cad=cad.toUpperCase();
tip[i]=cad.charAt(0);
}
while(tip[i]!='N' && tip[i]!='I') ;
}
return;
}

static void reportar(String nom[], double pre[], char tip[], int N) throws IOException
{int i;
System.out.println("\nReporte de datos\n");
for(i=0; i<N; i++)
{System.out.println("\t"+ nom[i]+"\t"+ pre[i]+"\t"+tip[i]);
}
return;
}
static int eliminar(String nom[], double pre[], char tip[], int N) throws IOException
{ int i, pos = -1;
String nomaux;
System.out.println("\nEliminacion de datos\n");
System.out.println("\nIngrese nombre a eliminar : ");
nomaux=br.readLine();
nomaux=nomaux.toUpperCase();
13
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS
INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ

for(i=0; i<N; i++)


{ if(nom[i].compareTo(nomaux)==0)
{ pos =i;
break;
}
}
if(pos== -1)
{System.out.println("\nDato buscado no existe\n");
}
else
{ System.out.println("\nDatos Actuales encontrados\n");
System.out.println("\t"+ nom[pos]+"\t"+pre[pos]+"\t"+tip[pos]);
nom[pos]=nom[N-1];
pre[pos]=pre[N-1];
tip[pos]=tip[N-1];
System.out.println("Los datos han sido borrados....!");
N=N -1;
}
return N;
}
static void ordenar(String nom[], double pre[], char tip[], int N) throws IOException
{ int i, j;
String nomaux ;
double preaux;
char tipaux;

for(i=0; i<N; i++)


{ for(j=i+1; j<N; j++)
{ if(nom[i].compareTo(nom[j])>0)
{nomaux=nom[j];
nom[j]=nom[i];
nom[i]=nomaux;
preaux=pre[j];
pre[j]=pre[i];
pre[i]=preaux;
tipaux=tip[j];
tip[j]=tip[i];
tip[i]=tipaux;
}
}
}
System.out.println("\nDatos ordenados\n");

return ;

14
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS
INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ

static int insertar(String nom[], double pre[], char tip[], int N) throws IOException
{ int i, j, pos = -1;
String nomaux , cad;
ordenar(nom,pre,tip,N); // por si los datos están desordenados, se ordenan
System.out.println("\nInsercion de datos \n");
System.out.println("\nIngrese nombre a insertar: ");
nomaux=br.readLine();
nomaux=nomaux.toUpperCase();
/***primero se busca el lugar donde se insertara el dato*/
for(i=0; i<N; i++)
{ if(nom[i].compareTo(nomaux)>0)
{ pos =i;
break;
}
}
if(pos== -1) // si se tiene que colocar en la última posición
{pos=N;
}
/*se desplazan todos los datos una posición hacia abajo, para insertar el dato*/
for(i=N; i>pos; i--)
{ nom[i] = nom[i-1];
pre[i] = pre[i-1];
tip[i] = tip[i-1];
}
/*se procede a insertar el dato */
nom[pos]= nomaux;
do{ System.out.print("Nuevo Precio["+pos+"] :");
pre[pos]=Double.parseDouble(br.readLine());
}
while(pre[pos]<=0) ;
do{ System.out.print("Nuevo Tipo["+pos+"] :");
cad=br.readLine();
cad=cad.toUpperCase();
tip[pos]=cad.charAt(0);
}
while( tip[pos]!='N' && tip[pos]!='I' ) ;
System.out.print("Los datos han sido insertados");
N++;
return N;
}
static void terminar() throws IOException
{System.out.println("\nFin del programa\n");
return;
}
} // llave fin de la clase

15
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS
INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ
4.-Escribir un programa que presente un menú que permita ejecutar alguna de las
siguientes opciones.
1.-Ingresar datos (se ejecuta una sola vez)
2.-Reportar datos (se ejecuta sólo si hay datos)
3.-Consultar Datos (se ejecuta sólo si hay datos)
4.-Eliminar Datos (se ejecuta sólo si hay datos)
5.-Terminar (siempre que se desee)
El programa se debe ejecutar mientras no se escoja la opción 5 (terminar).
Los datos a ingresar son: nombre (String), autor (String) y código (int de 4 dígitos) de
N libros.
Inicio
Variables nom[50],aut[50], cod[50], N=0, opc
opc = menu( )
opc
1 2 3 4 5
N=0 N>0 N>0 N>0
V F V F V F V F
reportar(nom, aut, cod,N)

consultar(nom,aut,cod,N)

eliminar(nom,aut,cod,N)
ingresar(nom,aut,cod,N)

Escribir: “No existen

Escribir: “No existen

Escribir: “No existen


Escribir: “Ya existen
N=leenum()

termina( )
datos”

datos”

datos”

datos”

opc ≠ 5
Fin

menu()
Variables opc
Escribir: “Menu“
Escribir:”[1] Ingresar”
Escribir:”[2] Reportar”
Escribir:”[3] Consultar”
leenum( )
Escribir:”[4] Eliminar”
Variables N
Escribir:”[5] Terminar”
Leer N
Escribir:”Escoger opción[1-5]”
N ≤ 0 V N > 50
Leer opc
retornar N
opc < 1 V opc > 5
retornar opc
16
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS
INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ

ingresar (nom[ ], aut[ ], cod[ ], N) reportar (nom[ ], aut[ ], cod[ ], N)

Variables i Variables i

i=0; i<N ; i = i +1 i=0; i<N ; i = I +1

Leer nom[i] Escribir: nom[i], aut[i], cod[ i ]

Leer aut[i]
Leer cod[i] retornar

cod[i]<1000 V cod[i] >9999

eliminar (nom[ ], aut[ ], cod[ ], N)


retornar variables i, nomaux, pos = -1
Leer nomaux
i=0; i<N ; i = i +1
V nom[i] = nomaux F
consultar (nom[ ], aut[ ], cod[ ], N) pos = i
variables i, nomaux, pos = -1 i=N
Leer nomaux
i=0; i<N ; i = i +1 V pos=-1 F
V nom[i] = nomaux F Escribir: nom[pos] = nom[N-1]
pos = i “Dato No Existe” aut[pos] = aut[N-1]
i=N cod[pos] = cod[N-1]
N=N-1
V pos=-1 F retornar N
Escribir: “Dato no Escribir: nom[pos]
Existe” aut[pos], cod[pos]
retornar

terminar( )

Variables

Escribir: “Fin del programa”

retornar

17
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS
INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ
Código en java
import java.io.*;
public class ejem4
{ static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

public static void main(String [] args) throws IOException


{ String nom[ ] = new String[50];
String aut [ ] = new String[50];
int cod [ ] = new int[50];
int N=0, opc;
do { opc= menu ( );
switch( opc )
{ case 1 : if(N==0) { N=leenum();
ingresar(nom,aut,cod,N);
}
else {System.out.println("\nYa existen datos..!");
}
break;
case 2 : if(N>0) { reportar(nom,aut,cod,N);
}
else{System.out.println("\nNo existen datos..!");
}
break;
case 3 : if(N>0) {consultar(nom,aut,cod,N);
}
else{System.out.println("\nNo existen datos..!");
}
break;
case 4 : if(N>0) {N=eliminar(nom,aut,cod,N);
}
else{System.out.println("\nNo existen datos..!");
}
break;
case 5 : terminar();
break;
}
}
while(opc!=5);
} // llave fin de main

static int leenum() throws IOException


{ int N;
do{ System.out.print("Ingrese No. de datos:");
N=Integer.parseInt(br.readLine());
}
while(N<=0 || N>50) ;
return N;
}
18
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS
INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ

static int menu() throws IOException


{ int opc;
do{ System.out.println("\n\nMENU");
System.out.println("[1]Ingresar datos");
System.out.println("[2]Reportar datos");
System.out.println("[3]Consultar datos");
System.out.println("[4]Eliminar datos");
System.out.println("[5]Terminar");
System.out.print("Escoger opcion [1 - 5 ] :");
opc=Integer.parseInt(br.readLine());
}
while(opc<1 || opc>5);
return opc;
}
static void ingresar(String nom[], String aut[], int cod[], int N) throws IOException
{int i;
String cad;
System.out.println("\nIngreso de datos\n");
for(i=0; i<N; i++)
{System.out.print("Nombre["+i+"] :");
nom[i]=br.readLine();
nom[i]=nom[i].toUpperCase();
System.out.print("Autor["+i+"] :");
aut[i]=br.readLine();
aut[i]=aut[i].toUpperCase();
do{System.out.print("Codigo["+i+"] :");
cod[i]=Integer.parseInt(br.readLine());
}
while(cod[i]<1000 || cod[i]>9999) ;
}
return;
}
static void reportar(String nom[], String aut[], int cod[], int N) throws IOException
{int i;
System.out.println("\nReporte de datos\n");
for(i=0; i<N; i++)
{System.out.println("\t"+ nom[i]+"\t"+ aut[i]+"\t"+cod[i]);
}
return;
}

static void consultar(String nom[], String aut[], int cod[], int N) throws IOException
{ int i, pos = -1;
String nomaux;
System.out.println("\nConsulta de datos\n");
System.out.print("\nIngrese nombre a buscar : ");
nomaux=br.readLine();
nomaux=nomaux.toUpperCase();
19
UNIVERSIDAD PRIVADA ANTENOR ORREGO WALTER LAZO AGUIRRE
INGENIERÍA DE COMPUTACIÓN Y SISTEMAS ARREGLOS-MÉTODOS
INTRODUCCIÓN A LA PROGRAMACIÓN MENÚ

for(i=0; i<N; i++)


{ if(nom[i].compareTo(nomaux)==0)
{ pos =i;
break;
}
}
if(pos== -1)
{System.out.println("\nDato buscado no existe\n");
}
else
{System.out.println("\nDato encontrado\n");
System.out.println("\t"+ nom[pos]+"\t"+aut[pos]+"\t"+cod[pos]);
}
return;
}

static int eliminar(String nom[], String aut[], int cod[], int N) throws IOException
{ int i, pos = -1;
String nomaux;
System.out.println("\nEliminacion de datos\n");
System.out.print("\nIngrese nombre a eliminar : ");
nomaux=br.readLine();
nomaux=nomaux.toUpperCase();
for(i=0; i<N; i++)
{ if(nom[i].compareTo(nomaux)==0)
{ pos =i;
break;
}
}
if(pos== -1)
{System.out.println("\nDato buscado no existe\n");
}
else
{ System.out.println("\nDatos Actuales encontrados\n");
System.out.println("\t"+ nom[pos]+"\t"+aut[pos]+"\t"+cod[pos]);
nom[pos]=nom[N-1];
aut[pos]=aut[N-1];
cod[pos]=cod[N-1];
System.out.println("Los datos han sido borrados....!");
N=N -1;
}
return N;
}

static void terminar() throws IOException


{System.out.println("\nFin del programa\n");
return;
}
} // llave fin de la clase
20

También podría gustarte