EjercicioAgenda Solucion

Descargar como pdf o txt
Descargar como pdf o txt
Está en la página 1de 8

Ejercicios de Programación

Tema 7. Programación Orientada a Objetos

22 de diciembre de 2013

1. Haz una clase llamada Agenda para manejar una agenda de teléfonos. Los objetos instanciados
a partir de esa clase tendrán la siguiente funcionalidad:
Construir una agenda a partir de un número máximo de contactos posibles (constructor)
Añadir un contacto nuevo
Listar todos los contactos de la agenda
Buscar un contacto por nombre
Buscar un contacto a partir de un número de teléfono
Borrar un teléfono dado un nombre
Modificar un número de telefono dado un nombre
Puedes añadir más métodos si lo consideras adecuado.
Las entradas de la agenda se modelarán mediante objetos de la clase Contacto que habrá que
crear. Para cada Contacto de la agenda se almacenará la siguiente información:

Nombre
Dirección
Código Postal
Teléfono

Puedes añadir los métodos que consideres adecuados.


Para probar tus clases hay que escribir un programa TestAgenda.java donde se instacie un objeto
de la clase Agenda y mediante un menú se pueda probar sus funcionalidades. En el menú se tiene
que dar también la posibilidad de leer y escribir la agenda desde un fichero binario, pidiendo el
nombre y de crear una nueva agenda.
Trucos:
Recuerda que para guardar un objeto usando WriteObject, el objeto, y todos los objetos
que use el primero, han de ser serializable. Para ello has de añadir a la cabecera de la classe
implements Serializable.

Por ej. public class NombreDeLaClase implements Serializable {

1
Solución
2. Clase Contacto:

1 import j a v a . i o . ∗ ;
2

3 public c l a s s Contacto implements S e r i a l i z a b l e {


4 // A t r i b u t o s
5 S t r i n g nombre ;
6 String direccion ;
7 int c o d i g o P o s t a l ;
8 int t e l e f o n o ;
9

10 // Metodos
11 public Contacto ( S t r i n g nombre , S t r i n g d i r e c c i o n , int c o d i g o P o s t a l ,
int t e l e f o n o ) {
12 t h i s . nombre = nombre ;
13 this . d i r e c c i o n = d i r e c c i o n ;
14 this . codigoPostal = codigoPostal ;
15 this . t e l e f o n o = t e l e f o n o ;
16 }
17 public S t r i n g t o S t r i n g ( ) {
18 S t r i n g r e s=" Nombre : "+ nombre+ "\n" + " Direccion "+
d i r e c c i o n +"\n" +" Codigo postal : "+ c o d i g o P o s t a l+"\n"+"
Telefono : "+t e l e f o n o +"\n" ;
19 return r e s ;
20 }
21 public S t r i n g getNombre ( ) {
22 return nombre ;
23 }
24

25 public int g e t T e l e f o n o ( ) {
26 return t e l e f o n o ;
27 }
28

29 public void s e t T e l e f o n o ( int t e l e f o n o ) {


30 t h i s . t e l e f o n o=t e l e f o n o ;
31 }
32 }

2
3. Clase Agenda:

1 import j a v a . i o . ∗ ;
2

3 public c l a s s Agenda implements S e r i a l i z a b l e {


4 // A t r i b u t o s
5 Contacto [ ] c o n t a c t o s=null ;
6

7 // Metodos
8 public Agenda ( int maxCont ) {
9 c o n t a c t o s = new Contacto [ maxCont ] ;
10 }
11

12 public boolean a n y a d i r C o n t a c t o ( S t r i n g nombre , S t r i n g d i r e c c i o n ,


int c o d i g o P o s t a l , int t e l e f o n o ) {
13 f o r ( int i = 0 ; i < c o n t a c t o s . l e n g t h ; i ++) {
14 i f ( c o n t a c t o s [ i ] == null ) { // s i t i o l i b r e
15 c o n t a c t o s [ i ] = new Contacto ( nombre , d i r e c c i o n ,
codigoPostal , telefono ) ;
16 return true ;
17 }
18 }
19 return f a l s e ;
20 }
21

22 public S t r i n g t o S t r i n g ( ) {
23 S t r i n g r e s="" ;
24 int c o n t =1;
25 f o r ( int i = 0 ; i < c o n t a c t o s . l e n g t h ; i ++) {
26 i f ( c o n t a c t o s [ i ] != null ) { // c o n t a c t o e n c o n t r a d o
27 r e s +="\ nContacto "+c o n t + "\n" + c o n t a c t o s [ i ] .
toString () ;
28 c o n t++;
29 }
30 }
31 return r e s ;
32 }
33

34 public Contacto buscarPorNombre ( S t r i n g nombre ) {


35 f o r ( int i = 0 ; i < c o n t a c t o s . l e n g t h ; i ++) {
36 i f ( c o n t a c t o s [ i ] != null ) { // hay c o n t a c t o
37 S t r i n g nom = c o n t a c t o s [ i ] . getNombre ( ) ;
38 i f (nom . e q u a l s ( nombre ) ) { // e n c o n t r a d o
39 return c o n t a c t o s [ i ] ;
40 }
41 }
42 }
43 return null ;
44 }
45

46 public Contacto b u s c a r P o r T e l e f o n o ( int t e l e f o n o ) {


47 f o r ( int i = 0 ; i < c o n t a c t o s . l e n g t h ; i ++) {
48 i f ( c o n t a c t o s [ i ] != null ) { // hay c o n t a c t o
49 int t e l= c o n t a c t o s [ i ] . g e t T e l e f o n o ( ) ;
3
50 i f ( t e l == t e l e f o n o ) { // e n c o n t r a d o
51 return c o n t a c t o s [ i ] ;
52 }
53 }
54 }
55 return null ;
56 }
57

58 public boolean borrarPorNombre ( S t r i n g nombre ) {


59 f o r ( int i = 0 ; i < c o n t a c t o s . l e n g t h ; i ++) {
60 i f ( c o n t a c t o s [ i ] != null ) { // hay c o n t a c t o
61 S t r i n g nom = c o n t a c t o s [ i ] . getNombre ( ) ;
62 i f (nom . e q u a l s ( nombre ) ) { // e n c o n t r a d o
63 c o n t a c t o s [ i ]= null ;
64 return true ;
65 }
66 }
67 }
68 return f a l s e ;
69 }
70

71 public boolean modificarPorNombre ( S t r i n g nombre , int n u e v o T e l e f o n o


){
72 f o r ( int i = 0 ; i < c o n t a c t o s . l e n g t h ; i ++) {
73 i f ( c o n t a c t o s [ i ] != null ) { // hay c o n t a c t o
74 S t r i n g nom = c o n t a c t o s [ i ] . getNombre ( ) ;
75 i f (nom . e q u a l s ( nombre ) ) { // e n c o n t r a d o
76 contactos [ i ] . se tTele fono ( nuevoTelefono ) ;
77 return true ;
78 }
79 }
80 }
81 return f a l s e ;
82 }
83 }

4
4. Programa de prueba:

1 import j a v a . i o . ∗ ;
2 import j a v a . u t i l . ∗ ;
3

4 public c l a s s TestAgenda {
5 s t a t i c Scanner t e c = new Scanner ( System . i n ) . u s e L o c a l e ( L o c a l e . US) ;
6 s t a t i c Agenda miAgenda=null ;
7

8 public s t a t i c void main ( S t r i n g [ ] a r g s ) {


9 int op =0;
10 do {
11 op=menu ( ) ;
12 switch ( op ) {
13 case 1 :
14 System . out . p r i n t ( " Numero maximo de contactos ? " ) ;
15 int maxCont = t e c . n e x t I n t ( ) ;
16 miAgenda = new Agenda ( maxCont ) ;
17 break ;
18 case 2 :
19 {
20 System . out . p r i n t ( " Fichero a leer? " ) ;
21 S t r i n g nombreFich = t e c . n e x t L i n e ( ) ;
22 i f ( l e e r ( nombreFich ) )
23 System . out . p r i n t l n ( " Agenda leida " ) ;
24 }
25 break ;
26 case 3 :
27 {
28 System . out . p r i n t ( " Nombre ? " ) ;
29 S t r i n g nombre = t e c . n e x t L i n e ( ) ;
30 System . out . p r i n t ( " Direccion ? " ) ;
31 String d i r e c c i o n = tec . nextLine ( ) ;
32 System . out . p r i n t ( " Codigo postal ? " ) ;
33 int c o d i g o P o s t a l = t e c . n e x t I n t ( ) ;
34 System . out . p r i n t ( " telefono ? " ) ;
35 int t e l e f o n o = t e c . n e x t I n t ( ) ;
36 i f ( miAgenda . a n y a d i r C o n t a c t o ( nombre , d i r e c c i o n ,
codigoPostal , telefono ) )
37 System . out . p r i n t l n ( " Contacto anyadido " ) ;
38 else
39 System . out . p r i n t l n ( "No se ha podido realizar
la operacion " ) ;
40 }
41 break ;
42 case 4 :
43 System . out . p r i n t l n ( " Listado de la agenda " ) ;
44 System . out . p r i n t l n ( miAgenda ) ;
45 break ;
46 case 5 :
47 {
48 System . out . p r i n t ( " Nombre a buscar ? " ) ;
49 S t r i n g nombre=t e c . n e x t L i n e ( ) ;
50 Contacto c o n t=miAgenda . buscarPorNombre ( nombre ) ;
5
51 i f ( c o n t != null )
52 System . out . p r i n t l n ( c o n t ) ;
53 else
54 System . out . p r i n t l n ( " Contacto no encontrado " ) ;
55 }
56 break ;
57 case 6 :
58 {
59 System . out . p r i n t ( " Telefono a buscar ? " ) ;
60 int t e l e f o n o=t e c . n e x t I n t ( ) ;
61 Contacto c o n t=miAgenda . b u s c a r P o r T e l e f o n o ( t e l e f o n o )
;
62 i f ( c o n t != null )
63 System . out . p r i n t l n ( c o n t ) ;
64 else
65 System . out . p r i n t l n ( " Contacto no encontrado " ) ;
66 }
67 break ;
68 case 7 :
69 {
70 System . out . p r i n t ( " Nombre a borrar ? " ) ;
71 S t r i n g nombre=t e c . n e x t L i n e ( ) ;
72 i f ( miAgenda . borrarPorNombre ( nombre ) )
73 System . out . p r i n t l n ( " Contacto borrado " ) ;
74 else
75 System . out . p r i n t l n ( " Contacto no encontrado " ) ;
76 }
77 break ;
78 case 8 :
79 {
80 System . out . p r i n t l n ( " Nombre a buscar ? " ) ;
81 S t r i n g nombre = t e c . n e x t L i n e ( ) ;
82 System . out . p r i n t ( " Nuevo telefono ? " ) ;
83 int t e l e f o n o = t e c . n e x t I n t ( ) ;
84 i f ( miAgenda . modificarPorNombre ( nombre , t e l e f o n o ) )
85 System . out . p r i n t l n ( " Telefono cambiado " ) ;
86 else
87 System . out . p r i n t l n ( "No se encontro el contacto
") ;
88 }
89 break ;
90 case 9 :
91 {
92 System . out . p r i n t ( " Nombre del fichero destino ? " ) ;
93 S t r i n g nombreFichero = t e c . n e x t L i n e ( ) ;
94 i f ( g u a r d a r ( nombreFichero ) )
95 System . out . p r i n t l n ( " Hecho " ) ;
96 else
97 System . out . p r i n t l n ( " Problemas escribiendo el
fichero " ) ;
98 }
99 break ;
100 case 0 :
101 System . out . p r i n t l n ( "BYE! " ) ;
6
102 break ;
103 default : System . out . p r i n t l n ( " Opcion incorrecta " ) ;
104 }
105

106 } while ( op !=0) ;


107 }
108

109 public s t a t i c int menu ( ) {


110 System . out . p r i n t l n ( "" ) ;
111

112 System . out . p r i n t l n ( "1.- Crear una agenda nueva " ) ;


113 System . out . p r i n t l n ( "2.- Leer agenda desde fichero " ) ;
114 System . out . p r i n t l n ( "3.- Anyadir contacto " ) ;
115 System . out . p r i n t l n ( "4.- Listar agenda " ) ;
116 System . out . p r i n t l n ( "5.- Buscar un contacto por nombre " ) ;
117 System . out . p r i n t l n ( "6.- Buscar un contacto por telefono " ) ;
118 System . out . p r i n t l n ( "7.- Borrar un contacto por nombre " ) ;
119 System . out . p r i n t l n ( "8.- Modificar un telefono por nombre " ) ;
120 System . out . p r i n t l n ( "9.- Guardar la agenda en un fichero " ) ;
121 System . out . p r i n t l n ( "0.- SALIR " ) ;
122

123 System . out . p r i n t l n ( "\ tElige opcion ?" ) ;


124 int op = t e c . n e x t I n t ( ) ;
125 t e c . n e x t L i n e ( ) ; // para e v i t a r e l problema con e l \n f i n a l
126 return op ;
127 }
128

129

130 public s t a t i c boolean g u a r d a r ( S t r i n g nombreFichero ) {


131 try {
132 ObjectOutputStream f i c h = new ObjectOutputStream (new
FileOutputStream ( nombreFichero ) ) ;
133 f i c h . w r i t e O b j e c t ( miAgenda ) ;
134 fich . close () ;
135 return true ;
136 } catch ( FileNotFoundException e ) {
137 System . out . p r i n t ( "El fichero \""+nombreFichero+"\" no
puede crearse " ) ;
138 return f a l s e ;
139 } catch ( IOException e ) {
140 System . out . p r i n t ( "El fichero \""+nombreFichero+"\" no puede
crearse " ) ;
141 return f a l s e ;
142 }
143 }
144

145 public s t a t i c boolean l e e r ( S t r i n g nombreFichero ) {


146 try {
147 ObjectInputStream f i c h = new ObjectInputStream (new
F i l e I n p u t S t r e a m ( nombreFichero ) ) ;
148 miAgenda=(Agenda ) f i c h . r e a d O b j e c t ( ) ;
149 fich . close () ;
150 return true ;
151 } catch ( FileNotFoundException e ) {
7
152 System . out . p r i n t l n ( "El fichero \""+nombreFichero+"\" no
puede crearse " ) ;
153 return f a l s e ;
154 } catch ( IOException e ) {
155 System . out . p r i n t l n ( " Problemas escribiendo el fichero \""+
nombreFichero+"\"" ) ;
156 return f a l s e ;
157 } catch ( ClassNotFoundException e ) {
158 System . out . p r i n t l n ( " Problemas escribiendo el fichero \""+
nombreFichero+"\"" ) ;
159 return f a l s e ;
160 }
161 }
162 }

Piensa: para qué se han puesto los parentesis en algunos case ?

También podría gustarte