Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
RubricEnric Java - rubricaRelase2.java

rubricaRelase2.java

Caricato da: Mutelg
Scarica il programma completo

  1. /**
  2.  * Autore :Alenadri Enrico
  3.  * Classe : III G
  4.  * Data :  6/02/2009
  5.  * Nome File:  rubrica.java
  6.  */
  7.  
  8.  import javax.swing.*;
  9.  import java.awt.*;
  10.  import java.awt.event.*;
  11.  import java.io.*;
  12.  
  13.  
  14.  public class rubrica{
  15.  
  16.         public static void main(String[] args)
  17.      {
  18.          int xLoc=400,   //posizione x dei frame nello schermo
  19.              yLoc=400;   //posizione y dei frame nello schermo
  20.  
  21.  
  22.      framePri frame = new framePri(xLoc,yLoc);
  23.      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24.      frame.show();
  25.      }
  26.  }
  27.  
  28.  
  29.  class panelInserimento extends JPanel implements ActionListener
  30.  {
  31.    private boolean exist=true;
  32.    private int x=0,
  33.                num=0,
  34.                        linee=0,
  35.                        lineeStm=0,
  36.                        i,
  37.                        cont,
  38.                        con;
  39.    private String nameRub,
  40.                   linea,
  41.                   stringa,
  42.                   risCerca="";
  43.    private TextField inNome,
  44.                      inCognome,
  45.                      inNumero,
  46.                      inIndirizzo,
  47.                      inNameRub,
  48.                      inCerca;
  49.  
  50.    private FileOutputStream fileS;
  51.    private FileReader fileL;
  52.    private File exFile;
  53.    private PrintStream scrittura;
  54.    private BufferedReader lettura;
  55.    private TextArea risultati;
  56.    private int[] cerca = new int[100];
  57.  
  58.    private JLabel testoIns,
  59.                   testoStp,
  60.                   nome,
  61.                   Congome,
  62.                   Numero,
  63.                   Indirizzo,
  64.                   index,
  65.                   spazio,
  66.                   stmNome,
  67.                   stmCognome,
  68.                   stmNumero,
  69.                   stmIndirizzo,
  70.                   infoCerca;
  71.    private JButton ok,
  72.                    reset,
  73.                    savExit,
  74.                    invio,
  75.                    invio_,
  76.                    indIns,
  77.                    indStp,
  78.                    indCer,
  79.                    avanti,
  80.                    back,
  81.                    indietro,
  82.                    exit,
  83.                    Cerca;
  84.    public panelInserimento()
  85.     {
  86.  
  87.      exit = new JButton("esci");
  88.      spazio= new JLabel("                              ");
  89.  
  90.      //Componenti Index
  91.          index = new JLabel("       Cosa vuoi fare?                                    ");
  92.          indIns = new JButton("Crea rubrica");
  93.          indStp = new JButton("Visualizza contatti");
  94.          indCer = new JButton("Cerca Contatti");
  95.  
  96.  
  97.  
  98.  
  99.      //Componenti inserimento nome Rubrica-inserimento scherda/stampa scheda
  100.          testoIns = new JLabel("Inserisci il nome con cui salvare la rubrica        ");
  101.          testoStp = new JLabel("Inserisci il nome della rubrica        ");
  102.          inNameRub = new TextField(10);
  103.          invio = new JButton("Invio");
  104.          invio_ = new JButton("Invio ");
  105.  
  106.          //Componenti Cerca
  107.          Cerca = new JButton("Cerca");
  108.          infoCerca = new JLabel("Puoi cercare per Nome-Cognome-Numero-indirizzo");
  109.          inCerca = new TextField(10);
  110.          risultati = new TextArea(10,35);
  111.          risultati.setEditable(false);
  112.          risultati.setCaretPosition(0);
  113.  
  114.  
  115.  
  116.          //Componenti inserimento scheda
  117.           nome = new JLabel("                Nome");
  118.           Congome = new JLabel("                Cognome");
  119.           Numero = new JLabel("                Numero Casa");
  120.           Indirizzo = new JLabel("                Indirizzo");
  121.           ok = new JButton("Add");
  122.           reset = new JButton("Reset");
  123.           savExit = new JButton("Save & Back");
  124.           inNome = new TextField(17);
  125.           inCognome = new TextField(17);
  126.           inNumero = new TextField(17);
  127.           inIndirizzo = new TextField(17);
  128.  
  129.      //Componenti stampa Scheda
  130.      stmNome = new JLabel("Nome:");
  131.      stmCognome = new JLabel("Cognome:");
  132.      stmNumero = new JLabel("Numero:");
  133.      stmIndirizzo = new JLabel("Indirizzo");
  134.      avanti = new JButton(">>");
  135.      back = new JButton("Indietro");
  136.      indietro = new JButton("<<");
  137.  
  138.  
  139.  
  140.      nameRub = inNameRub.getText();
  141.  
  142.       //Aggiungo componenti index
  143.       add(index);
  144.       add(indIns);
  145.       add(indStp);
  146.       add(indCer);
  147.       add(exit);
  148.  
  149.       indIns.addActionListener(this);
  150.       indStp.addActionListener(this);
  151.       indCer.addActionListener(this);
  152.       exit.addActionListener(this);
  153.  
  154.       //Aggiungo componenti Inserimento rubrica
  155.       add(testoStp);
  156.           add(testoIns);
  157.           add(inNameRub);
  158.           add(invio);
  159.           add(invio_);
  160.  
  161.           invio.addActionListener(this);
  162.           invio_.addActionListener(this);
  163.  
  164.  
  165.      //Aggiungo componenti Cerca
  166.       add(risultati);
  167.       add(infoCerca);
  168.       add(inCerca);
  169.       add(Cerca);
  170.  
  171.       Cerca.addActionListener(this);
  172.  
  173.     ///Aggiungo componenti Input contatti
  174.           add(nome);
  175.           add(inNome);
  176.           add(Congome);
  177.           add(inCognome);
  178.           add(Numero);
  179.           add(inNumero);
  180.           add(Indirizzo);
  181.           add(inIndirizzo);
  182.           add(ok);
  183.           add(reset);
  184.           add(savExit);
  185.  
  186.           ok.addActionListener(this);
  187.           reset.addActionListener(this);
  188.           savExit.addActionListener(this);
  189.  
  190.  
  191.       //Aggiungo componenti Visualizzazione contatti
  192.       add(stmNome);
  193.       add(stmCognome);
  194.       add(stmNumero);
  195.       add(stmIndirizzo);
  196.       add(indietro);
  197.       add(back);
  198.       add(avanti);
  199.       indietro.setEnabled(false);
  200.  
  201.       avanti.addActionListener(this);
  202.       back.addActionListener(this);
  203.       indietro.addActionListener(this);
  204.  
  205.  
  206.  
  207.       //Setto i componenti per visualizzare la scherata di index
  208.           pannelloInput(false);
  209.           pannelloOutput(false);
  210.           pannelloRubIns(false);
  211.           pannelloCerca(false);
  212.           pannelloRisultati(false);
  213.  
  214.  
  215.  
  216.          }
  217.     public void actionPerformed(ActionEvent evt)
  218.                      {
  219.                            String nome = inNome.getText();
  220.                            String cognome = inCognome.getText();
  221.                            String numero = inNumero.getText();
  222.                            String indirizzo = inIndirizzo.getText();
  223.                            String nameRub = inNameRub.getText();
  224.                boolean test;
  225.  
  226.                            String command = evt.getActionCommand();
  227.                            command = command.toLowerCase();
  228.                          if(command.equals("<<"))
  229.                                                     {
  230.                                                           if(num>0)
  231.                                                             {
  232.                                                                  num--;
  233.                                                                  avanti.setEnabled(true);
  234.                                                             }
  235.  
  236.                                                       if(num==0)
  237.                                                          indietro.setEnabled(false);
  238.  
  239.                                                            letturaFile(nameRub);
  240.                                                        stampa();
  241.                                 }
  242.                          if(command.equals(">>"))
  243.                             {
  244.                                   if((num+1)*4<lineeStm)
  245.                                     {
  246.                                          num++;
  247.                                          indietro.setEnabled(true);
  248.                                     }
  249.                               if((num+1)*4==lineeStm)
  250.                                  avanti.setEnabled(false);
  251.  
  252.                                          letturaFile(nameRub);
  253.                                  stampa();
  254.                                 }
  255.                          if(command.equals("indietro"))
  256.                             {
  257.                  num=0;
  258.                  risCerca="";
  259.                  testoStp.setText("Inserisci il nome della rubrica        ");
  260.                  testoIns.setText("Inserisci il nome con cui salvare la rubrica        ");
  261.                  avanti.setEnabled(true);
  262.                  indietro.setEnabled(false);
  263.                                  pannelloIndex(true);
  264.                                  pannelloInput(false);
  265.                      pannelloOutput(false);
  266.                                  pannelloRubIns(false);
  267.                                  pannelloRubStp(false);
  268.                                  pannelloCerca(false);
  269.                                  pannelloRisultati(false);
  270.  
  271.                                 }
  272.                  if(command.equals("add"))
  273.                     {
  274.                                 try{
  275.                                  scrittura.println(nome);
  276.                                  scrittura.println(cognome);
  277.                                  scrittura.println(numero);
  278.                                  scrittura.println(indirizzo);
  279.                   }catch(NullPointerException ex)
  280.                    {
  281.                         System.out.println("File insesistente...                        ");
  282.                      }
  283.                      reset();
  284.  
  285.                             }
  286.  
  287.                           if(command.equals("invio"))
  288.                                                     {
  289.                                                                 test=true;
  290.  
  291.                                               if(nameRub.equals(""))
  292.                                                    test=false;
  293.                                                      exFile = new File(nameRub);
  294.                                                      if( ((exFile).exists()) || (test==false))
  295.                                                        {
  296.                                                         inNameRub.setText("");
  297.                                                         if(test==false)
  298.                                                            testoIns.setText("Nome File non corretto.Riprova                        ");
  299.                                                         else
  300.  
  301.                                                            testoIns.setText("File già esistente. Riprova                         ");
  302.                                                                     }
  303.                                                                  else
  304.                                                                    {
  305.  
  306.                                                                    pannelloRubIns(false);
  307.                                                    pannelloInput(true);
  308.                                                     try{
  309.                                                                              fileS = new FileOutputStream(nameRub);
  310.                                                                              scrittura = new PrintStream(fileS);
  311.                                                                         }catch(FileNotFoundException ex)
  312.                                                                         {
  313.                                                                            System.out.println("Errore in lettura File...");
  314.                                                         }
  315.  
  316.                                                                    }
  317.  
  318.  
  319.                                 }
  320.  
  321.                                 if(command.equals("invio "))
  322.                                                             {
  323.                                                                         test=true;
  324.  
  325.                                                     if(nameRub.equals(""))
  326.                                                          test=false;
  327.                                                              exFile = new File(nameRub);
  328.                                                              if( ((exFile).exists()) && (test==true))
  329.                                                                {
  330.                                                                                 letturaFile(nameRub);
  331.                                                                                 stampa();
  332.                                                                             pannelloRubStp(false);
  333.                                                             pannelloOutput(true);
  334.  
  335.                                                                 }
  336.                                                                          else
  337.                                                                            {
  338.                                                                                 inNameRub.setText("");
  339.                                                                 if(test==false)
  340.                                                                    testoStp.setText("Nome File non corretto.Riprova!                       ");
  341.                                                                 else
  342.  
  343.                                                                    testoStp.setText("File non esistente. Riprova!                        ");
  344.                                                                             }
  345.  
  346.  
  347.                                 }
  348.  
  349.                       if(command.equals("reset"))
  350.                         {
  351.                                  reset();
  352.                                 }
  353.                      if(command.equals("save & back"))
  354.                         {
  355.                      pannelloInput(false);
  356.                      pannelloRubIns(false);
  357.                      pannelloRubStp(false);
  358.                      pannelloIndex(true);
  359.                      pannelloOutput(false);
  360.                             }
  361.                          if(command.equals("cerca"))
  362.                            {
  363.                                  test=true;
  364.                                  if(nameRub.equals(""))
  365.                    test=false;
  366.                          exFile = new File(nameRub);
  367.                  if( ((exFile).exists()) && (test==true))
  368.                                     {
  369.                                      stringa = inCerca.getText();
  370.                                          cont=0;
  371.                                          con=0;
  372.                                          linee=0;
  373.                                          cerca = new int[100];
  374.                                      cerca(stringa,nameRub);
  375.                                      i=0;
  376.                      if(cerca[0]!=0)
  377.                        {
  378.                                             do{
  379.                                                stampaRisultati(cerca[i]-1,nameRub);
  380.                                                i++;
  381.                             }while(cerca[i]!=0);
  382.                                          }else{
  383.                                            risCerca="Nessun contatto trovato!!";
  384.                                      }
  385.                          risultati.setText(risCerca);
  386.                          pannelloCerca(false);
  387.                          pannelloRisultati(true);
  388.                                  }else{
  389.                                          inNameRub.setText("");
  390.                                          if(test==false)
  391.                                             testoStp.setText("Nome File non corretto.Riprova!                       ");
  392.                                          else
  393.                                             testoStp.setText("File non esistente. Riprova!                        ");
  394.                              }
  395.  
  396.  
  397.                            }
  398.                      if(command.equals("esci"))
  399.                         {
  400.                      System.exit(0);
  401.                             }
  402.                           if(command.equals("crea rubrica"))
  403.                             {
  404.                      pannelloRubIns(true);
  405.                      pannelloIndex(false);
  406.                                 }
  407.                           if(command.equals("visualizza contatti"))
  408.                             {
  409.                 pannelloRubStp(true);
  410.                 pannelloIndex(false);
  411.                                 }
  412.               if(command.equals("cerca contatti"))
  413.                {
  414.                                  pannelloIndex(false);
  415.                                  pannelloCerca(true);
  416.                            }
  417.                          }
  418.     void letturaFile(String nomeFile)
  419.        {
  420.                   int cont=0;
  421.                   lineeStm=0;
  422.  
  423.       try{
  424.          fileL = new FileReader(nomeFile);
  425.          lettura = new BufferedReader(fileL);
  426.  
  427.          while( (linea = lettura.readLine()) != null )
  428.                 {
  429.                          lineeStm++;
  430.                 }
  431.  
  432.          fileL = new FileReader(nomeFile);
  433.          lettura = new BufferedReader(fileL);
  434.  
  435.             while( cont<num*4 )
  436.                   {
  437.                            cont++;
  438.                            linea = lettura.readLine();
  439.                 }
  440.  
  441.      }catch(FileNotFoundException ex)
  442.          {
  443.           System.out.println("Errore in lettura file, Nessun file trovato!");
  444.          }
  445.          catch(IOException ex)
  446.          {
  447.           System.out.println("Errore in lettura file..");
  448.          }
  449.  
  450.            }
  451.  
  452.      void cerca(String stringRicerca,String nomeFile)
  453.      {
  454.       i=0;
  455.           try{
  456.               fileL = new FileReader(nomeFile);
  457.                   lettura= new BufferedReader(fileL);
  458.                   while( (linea = lettura.readLine()) != null )
  459.                     {
  460.                      linee++;
  461.                     }
  462.                   fileL = new FileReader(nomeFile);
  463.                   lettura = new BufferedReader(fileL);
  464.                   num=1;
  465.                   do{
  466.                      while( cont<num*4 )
  467.                        {
  468.                         cont++;
  469.                             linea = lettura.readLine();
  470.                             if(linea.equals(stringRicerca))
  471.                                   {
  472.                                    cerca[i]=num;
  473.                                    i++;
  474.                               }
  475.                    }
  476.                    num++;
  477.                  }while(cont<linee);
  478.          }catch(FileNotFoundException ex)
  479.      {
  480.       System.out.println("Errore in lettura file..");
  481.      }catch(IOException ex)
  482.      {
  483.       System.out.println("Errore in lettura file..");
  484.      }
  485.         }
  486.  
  487.  
  488.     void stampaRisultati(int nume,String nomeFile)
  489.     {
  490.         cont=0;
  491.         con=0;
  492.         linee=0;
  493.             try{
  494.                   fileL = new FileReader(nomeFile);
  495.                   lettura = new BufferedReader(fileL);
  496.  
  497.                    while( (linea = lettura.readLine()) != null )
  498.                          {
  499.                                   linee++;
  500.                           }
  501.                    fileL = new FileReader(nomeFile);
  502.                    lettura = new BufferedReader(fileL);
  503.                    while(cont<nume*4){
  504.                                    linea = lettura.readLine();
  505.                                    cont++;
  506.                           }
  507.  
  508.                                 linea = lettura.readLine();
  509.                                   risCerca+="Nome: "+linea;
  510.  
  511.                                 linea = lettura.readLine();
  512.                                   risCerca+="\nCognome: "+linea;
  513.  
  514.                                 linea = lettura.readLine();
  515.                                   risCerca+="\nNumero Casa: "+linea;
  516.  
  517.                                 linea = lettura.readLine();
  518.                                   risCerca+="\nIndirizzo: "+linea+"\n\n";
  519.  
  520.               }catch(FileNotFoundException ex)
  521.                  {
  522.                   System.out.println("Errore in lettura file..");
  523.                  }
  524.                  catch(IOException ex)
  525.                  {
  526.                   System.out.println("Errore in lettura file..");
  527.               }
  528.  
  529.         }
  530.  
  531.         void stampa()
  532.           {
  533.         try{
  534.           linea = lettura.readLine();
  535.         stmNome.setText("                        Nome :  "+linea+"                                            ");
  536.  
  537.                   linea = lettura.readLine();
  538.             stmCognome.setText("                        Cognome :  "+linea+"                                  ");
  539.  
  540.                   linea = lettura.readLine();
  541.         stmNumero.setText("                         Numero :  "+linea+"                                  ");
  542.  
  543.                   linea = lettura.readLine();
  544.         stmIndirizzo.setText("                        Indirizzo :  "+linea+"                                  ");
  545.     }catch(IOException ex)
  546.     {
  547.           System.out.println("Errore in stampa Dati File.....");
  548.         }catch(NullPointerException ex)
  549.         {
  550.       System.out.println("Nessun file ricevuto...");
  551.         }
  552.  
  553.           }
  554.  
  555.     void reset()
  556.       {
  557.                 inNome.setText("");
  558.                 inCognome.setText("");
  559.                 inNumero.setText("");
  560.                 inIndirizzo.setText("");
  561.           }
  562.  
  563.          void pannelloCerca(boolean att)
  564.            {
  565.                 testoStp.setVisible(att);
  566.                 inNameRub.setVisible(att);
  567.                 infoCerca.setVisible(att);
  568.                 inCerca.setVisible(att);
  569.                 Cerca.setVisible(att);
  570.                 back.setVisible(att);
  571.  
  572.            }
  573.     void pannelloRisultati(boolean att)
  574.       {
  575.            risultati.setVisible(att);
  576.            back.setVisible(att);
  577.           }
  578.          void pannelloInput(boolean att)
  579.           {
  580.           nome.setVisible(att);
  581.           inNome.setVisible(att);
  582.           Congome.setVisible(att);
  583.           inCognome.setVisible(att);
  584.           Numero.setVisible(att);
  585.           inNumero.setVisible(att);
  586.           Indirizzo.setVisible(att);
  587.           inIndirizzo.setVisible(att);
  588.  
  589.           ok.setVisible(att);
  590.           reset.setVisible(att);
  591.           savExit.setVisible(att);
  592.  
  593.           }
  594.          void pannelloOutput(boolean att)
  595.           {
  596.        stmNome.setVisible(att);
  597.            stmCognome.setVisible(att);
  598.        stmNumero.setVisible(att);
  599.            stmIndirizzo.setVisible(att);
  600.            avanti.setVisible(att);
  601.            back.setVisible(att);
  602.        indietro.setVisible(att);
  603.           }
  604.           void pannelloRubIns(boolean att)
  605.            {
  606.                 testoStp.setVisible(false);
  607.             testoIns.setVisible(att);
  608.             inNameRub.setVisible(att);
  609.             invio.setVisible(att);
  610.             invio_.setVisible(false);
  611.            }
  612.  
  613.           void pannelloRubStp(boolean att)
  614.            {
  615.                 testoIns.setVisible(false);
  616.             testoStp.setVisible(att);
  617.             inNameRub.setVisible(att);
  618.             invio.setVisible(false);
  619.             invio_.setVisible(att);
  620.             back.setVisible(att);
  621.            }
  622.           void pannelloIndex(boolean att)
  623.            {
  624.  
  625.          index.setVisible(att);
  626.          indIns.setVisible(att);
  627.          indStp.setVisible(att);
  628.          indCer.setVisible(att);
  629.          exit.setVisible(att);
  630.          spazio.setVisible(att);
  631.        }
  632.  
  633.  }
  634.  
  635.  
  636.  
  637.  
  638. class framePri extends JFrame
  639.  {
  640.     public framePri(int xLoc,int yLoc)
  641.     {
  642.  
  643.         setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
  644.         setTitle("Rubrica 1.0");
  645.         setResizable(true);
  646.         setLocation(xLoc,yLoc);
  647.         panelInserimento panelIns = new panelInserimento();
  648.         Container contentPane = getContentPane();
  649.         contentPane.add(panelIns);
  650.     }
  651.      public static final int DEFAULT_WIDTH =300 ;
  652.      public static final int DEFAULT_HEIGHT =245 ;
  653. }