poo 4 arraylist_implem

12
POO – COLECCIONES - ARRAYLIST POO-4 COLECCIONES J. Mancilla M. A continuación se presenta el uso de Array List en un sistema tipo Agenda de datos. La aplicación es de tipo Grafica (AWT) y la compone una clase principal, una con elementos gráficos y el patrón persona: Inicio.java,Persona.java y Agenda.java Implementación grafica con ArrayList

Upload: jlmanmons

Post on 26-May-2015

243 views

Category:

Documents


4 download

DESCRIPTION

Implementacion de aplicacion usando coleccion tipo ArrayList

TRANSCRIPT

Page 1: Poo 4 arraylist_implem

POO – COLECCIONES - ARRAYLIST

POO-4COLECCIONES

J. Mancilla M.

A continuación se presenta el uso de Array List en un sistema tipo Agenda de datos. La aplicación es de tipo Grafica (AWT) y la compone una clase principal, una con elementos gráficos y el patrón persona: Inicio.java,Persona.java y Agenda.java

Implementación grafica con ArrayList

Page 2: Poo 4 arraylist_implem

POO – COLECCIONES - ARRAYLIST

import java.awt.*;import javax.swing.*;import java.util.*;import java.io.*;public class Inicio extends JPanel { JButton entrar = new JButton("Ingresar!"); JPanel pan = new JPanel(); JPanel panelprincipal = new JPanel(); JTextField usu=new JTextField(); JPasswordField contra=new JPasswordField(); int i=0; String usuario=""; String contrax=""; JFrame ve = new JFrame("Ventana Principal"); public Inicio() //Constructor { panelprincipal.setLayout(null);

add(panelprincipal);addComponent(panelprincipal, new JLabel("Usuario: "), 20,20,50,20);addComponent(panelprincipal, usu, 100,20,120,20); addComponent(panelprincipal, new JLabel("Contraseña: "), 20,70,80,20);addComponent(panelprincipal, contra, 100,70,120,20);

addComponent(panelprincipal,entrar,10,100,130,20);

Ejemplo Aplicación-GUI con ArrayList

Page 3: Poo 4 arraylist_implem

POO – COLECCIONES - ARRAYLIST

ve.getContentPane().add(panelprincipal,BorderLayout.CENTER); ve.setTitle("ACCESO RESTRINGIDO"); ve.setSize(300,300); ve.setLocation(200,100); ve.setVisible( true ); entrar.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){

acceso(); }}); usu.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){

acceso(); }}); contra.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){acceso(); }});

}//Cierre Constructor public void addComponent(Container container,Component c,int x,int y,int width,int height) { c.setBounds(x,y,width,height); container.add(c); }

public static void main (String []args) { new Inicio(); }

Ejemplo Aplicación-GUI con ArrayList

Page 4: Poo 4 arraylist_implem

POO – COLECCIONES - ARRAYLIST

public void acceso() {usuario=String.valueOf(usu.getText()); contrax=String.valueOf(contra.getText());String usux="usuario";//hardcore de usuarioString conx="usuario";//hardcore de clave

if((usuario.equals(usux))&&(contrax.equals(conx)) &&((!usuario.equals(""))&&(!contrax.equals("")))) { i=0; //ingreso a la Interfaz de la Agenda

Agenda agenda=new Agenda();usu.setText(""); contra.setText("");

} else { i=i+1; if (i==3) { JOptionPane.showMessageDialog(null,"Sistema se Cerrará!", "*Bye*",

JOptionPane.INFORMATION_MESSAGE); System.exit(0);

}getToolkit().beep();

JOptionPane.showMessageDialog(null,"USUARIO NO VALIDO \n INTENTE DE NUEVO ("+i+")", "*ATENCION*", JOptionPane.INFORMATION_MESSAGE); usu.requestFocus();

} } }//clase inicio

Ejemplo Aplicación-GUI con ArrayList

Page 5: Poo 4 arraylist_implem

POO – COLECCIONES - ARRAYLIST

class Persona{ private String rut; private String nombre; private String direc; private String prof; private String fono; private int edad; public Persona(String rut,String nombre,String direc,String prof,String fono, int edad) { this.rut=rut; this.nombre = nombre; this.direc=direc; this.prof = prof; this.fono = fono; this.edad = edad; } //Accesadores public String getRut() { return rut; } public String getNombre() { return nombre; } public String getDirec() { return direc; } public String getProf() { return prof; } public String getFono() { return fono; } public int getEdad() { return edad; } public void ImprimirAgenda() { //CustomizedSystem.out.println("RUT: "+getRut());System.out.println("NOMBRE: "+getNombre());System.out.println("DIRECCION: "+getDirec());System.out.println("PROFESION: "+getProf());System.out.println("TELEFONO: "+getFono());System.out.println("EDAD: "+getEdad()); }}

Ejemplo Aplicación-GUI con ArrayList

Page 6: Poo 4 arraylist_implem

POO – COLECCIONES - ARRAYLIST

//Interfaz de la Agenda de contactosimport java.awt.*;import java.util.*;import javax.swing.*;

public class Agenda extends WindowAdapter implements ActionListener{ //generar colecciones con ArrayList private ArrayList listado; private int pos; private Label lblRut; private Label lblNombre; private Label lblDirec; private Label lblProf; private Label lblFono; private Label lblEdad; private Label lblMensaje; private TextField txtRut; private TextField txtNombre; private TextField txtDirec; private TextField txtProf; private TextField txtFono; private TextField txtEdad; private Button btnAceptar; private Button btnDesplegar; private Button btnEliminar; private Button btnSalir; private Frame frmAgenda; private Dialog dlgAviso; private Panel frmPanel; private Panel dlgPanel; private JRadioButton rb1; private JRadioButton rb2; private JRadioButton rb3; private ButtonGroup grupo; private Persona pers;

Ejemplo Aplicación-GUI con ArrayList

Page 7: Poo 4 arraylist_implem

POO – COLECCIONES - ARRAYLIST

//Interfaz de la Agenda de contactospublic Agenda() {

listado=new ArrayList();frmAgenda=new Frame("Lista de Contactos"); frmAgenda.setBounds(10,10,400,300); frmAgenda.addWindowListener(this); frmPanel=new Panel(); frmPanel.setLayout(null); JRadioButton rb1=new JRadioButton(); JRadioButton rb2=new JRadioButton(); JRadioButton rb3=new JRadioButton(); ButtonGroup grupo= new ButtonGroup(); //grupo.setBounds(120,240,80,20);

lblRut=new Label("RUT"); lblRut.setBounds(30,30,80,20); lblNombre=new Label("NOMBRE"); lblNombre.setBounds(30,60,80,20); lblDirec=new Label("DIRECCION"); lblDirec.setBounds(30,90,80,20); lblProf=new Label("PROFESION"); lblProf.setBounds(30,120,80,20); lblFono=new Label("TELEFONO"); lblFono.setBounds(30,150,80,20); lblEdad=new Label("EDAD"); lblEdad.setBounds(30,180,80,20); txtRut=new TextField(); txtRut.setBounds(120,30,80,20); txtNombre=new TextField(); txtNombre.setBounds(120,60,80,20); txtDirec=new TextField(); txtDirec.setBounds(120,90,80,20); txtProf=new TextField(); txtProf.setBounds(120,120,80,20); txtFono=new TextField(); txtFono.setBounds(120,150,80,20); txtEdad=new TextField(); txtEdad.setBounds(120,180,80,20);

Ejemplo Aplicación-GUI con ArrayList

Page 8: Poo 4 arraylist_implem

POO – COLECCIONES - ARRAYLIST

btnAceptar=new Button("Aceptar"); btnAceptar.setBounds(30,210,80,20); btnAceptar.addActionListener(this); btnDesplegar=new Button("Desplegar"); btnDesplegar.setBounds(120,210,80,20); btnDesplegar.addActionListener(this); btnEliminar=new Button("Eliminar"); btnEliminar.setBounds(30,240,80,20); btnEliminar.addActionListener(this); btnSalir=new Button("Salir"); btnSalir.setBounds(120,240,80,20); btnSalir.addActionListener(this); //integrar labels al pane frmPanel.add(lblRut); frmPanel.add(lblNombre); frmPanel.add(lblDirec); frmPanel.add(lblProf); frmPanel.add(lblFono); frmPanel.add(lblEdad); //integrar text al panel frmPanel.add(txtRut); frmPanel.add(txtNombre);frmPanel.add(txtDirec); frmPanel.add(txtProf); frmPanel.add(txtFono); frmPanel.add(txtEdad); //integrar botones al panel frmPanel.add(btnAceptar); frmPanel.add(btnDesplegar); frmPanel.add(btnEliminar); frmPanel.add(btnSalir); frmAgenda.add(frmPanel); dlgAviso=new Dialog(frmAgenda,"Confirmacion de Datos Contactos"); dlgAviso.setBounds(10,10,300,100); dlgAviso.addWindowListener(this); dlgPanel=new Panel(); dlgPanel.setLayout(null); lblMensaje=new Label(); lblMensaje.setBounds(10,10,300,20); dlgPanel.add(lblMensaje);dlgAviso.add(dlgPanel); frmAgenda.setVisible(true); } //Agenda

Ejemplo Aplicación-GUI con ArrayList

Page 9: Poo 4 arraylist_implem

POO – COLECCIONES - ARRAYLIST

public void windowClosing(WindowEvent ev) { if(ev.getSource()==frmAgenda) { frmAgenda.setVisible(false); } if(ev.getSource()==dlgAviso) { dlgAviso.setVisible(false); } } public void actionPerformed(ActionEvent ev) { if(ev.getSource()==btnSalir) { frmAgenda.setVisible(false); frmAgenda.dispose(); } if(ev.getSource()==btnAceptar) { ingresarContacto(); } if(ev.getSource()==btnEliminar) { eliminarContacto(); } if(ev.getSource()==btnDesplegar) { desplegarAgenda(); }}public boolean validaDatos() { boolean valida=true; int edad; try { if(txtRut.getText().length()==0) { valida=false; } if(txtNombre.getText().length()==0) { valida=false; }

if(txtDirec.getText().length()==0) { valida=false; }if(txtProf.getText().length()==0) { valida=false; } if(txtFono.getText().length()==0) { valida=false; }

edad=Integer.parseInt(txtEdad.getText());return valida;}

catch(Exception error) { valida=false; return valida; }}

Ejemplo Aplicación-GUI con ArrayList

Page 10: Poo 4 arraylist_implem

POO – COLECCIONES - ARRAYLIST

public boolean buscaRut(String xRut) { boolean busca=false; String nombre; for(int i=0;i<listado.size();i++) { if( xRut.equals( ((Persona)( listado.get(i) )) .getRut() ) ) { pos=i; busca=true;

lblMensaje.setText("El Contacto existe nombre: "+ ((Persona)(listado.get(pos))) .getNombre()+ " Verifique" ); } }//for return busca;

} public void ingresarContacto() { if(validaDatos()) {

if(!buscaRut(txtRut.getText())) { pers=new Persona(txtRut.getText(),txtNombre.getText(),txtDirec.getText(), txtProf.getText(),txtFono.getText(),Integer.parseInt(txtEdad.getText()));

listado.add(pers); pers=null; lblMensaje.setText("el Contacto fue ingresado"); dlgAviso.setVisible(true);

}else { dlgAviso.setVisible(true); }

} else { lblMensaje.setText("error en ingreso"); dlgAviso.setVisible(true); } limpiarTextos(); }

Ejemplo Aplicación-GUI con ArrayList

Page 11: Poo 4 arraylist_implem

POO – COLECCIONES - ARRAYLIST

public void eliminarContacto(){ if (buscaRut(txtRut.getText())) { lblMensaje.setText("Contacto encontrado y eliminado"); dlgAviso.setVisible(true); listado.remove(pos); } else System.out.println("Coche no encontrado");} public void desplegarAgenda() { if (!listado.isEmpty()) { Iterator it=listado.iterator(); while(it.hasNext()) { ((Persona)(it.next())).ImprimirAgenda(); } } else { lblMensaje.setText("No se han ingresado Contactos"); dlgAviso.setVisible(true); } }public void limpiarTextos() {

txtRut.setText(""); txtNombre.setText(""); txtDirec.setText("");txtProf.setText(""); txtFono.setText(""); txtEdad.setText("");

}} //clase

Ejemplo Aplicación-GUI con ArrayList

Page 12: Poo 4 arraylist_implem

POO – COLECCIONES - ARRAYLIST

POO-4

COLECCIONES

Implementación grafica con ArrayList

Fin Presentacion